Spring MVC – Multiple Submit Buttons Example
Let us see an example code today for Spring MVC that has multiple submit buttons in one form. This is Second spring mvc tutotial. (Spring MVC example1 is here).
In this example, We are going to have 2 submit buttons. “Success” and “Failure”. When user clicks “Success” button it should pass to Controller and do some functions and take the user to respective page. Similarly when user clicks “Failure” it should go to Controller and then pass it to respective page.
com.javainfinite.pojo
employee.java
public class employee { private String uname,password,id; public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getId() { return id; } public void setId(String id) { this.id = id; } }
page1.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="s"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Sample Spring</title> </head> <body> <s:form method="POST"> <table> <tr> <td> Username: </td> <td><input type="text" name="uname"/></td> </tr> <tr> <td> ID: </td> <td><input type="text" name="id"/></td> </tr> <tr> <td> Password: </td> <td><input type="password" name="password"/></td> </tr> <tr> <td colspan="2"><input type="submit" name="Success" value="Success"/></td> <td colspan="2"><input type="submit" name="Failure" value="Failure"/></td> </tr> </table> </s:form> </body> </html>
page2.jsp (Success click page)
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Success</title> </head> <body> <h1>This page is from Success click ${uname}</h1> </body> </html>
page3.jsp (Failure click page)
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>Failure click page ${id}</h1> </body> </html>
com.javainfinite.controller
econtroller.java
package com.javainfinite.controller; import com.javainfinite.pojo.employee; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.portlet.ModelAndView; @Controller @RequestMapping(value="/page1.htm") public class econtroller { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); @RequestMapping(method=RequestMethod.GET) public ModelAndView first() { return new ModelAndView("Register"); } @RequestMapping(params="Success", method=RequestMethod.POST) public String buttoncheck(@ModelAttribute ("employee") employee emp, ModelMap modelmap) { String uname=emp.getUname(); modelmap.put("uname",uname); return "page2"; } @RequestMapping(params="Failure", method=RequestMethod.POST) public String buttoncheck1(@ModelAttribute ("employee") employee emp, ModelMap modelmap) { String id=emp.getId(); modelmap.put("id",id); return "page3"; } }
applicationContext.xml
<?xml version='1.0' encoding='UTF-8' ?> <!-- was: <?xml version="1.0" encoding="UTF-8"?> --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <bean id="employee" class="com.javainfinite.pojo.employee"/> </beans>
dispatcher-servlet.xml
<?xml version='1.0' encoding='UTF-8' ?> <!-- was: <?xml version="1.0" encoding="UTF-8"?> --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd "> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> <context:component-scan base-package="com.javainfinite"/> <mvc:annotation-driven/> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="index.htm">indexController</prop> </props> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> <!-- The index controller. --> <bean name="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController" p:viewName="index" /> </beans>
On Clicking Success Button,
On clicking Failure Button,
[…] Session Attributes in Spring MVC Here is an example of how to use session.setAttribute and session.getAttribute, the same way we use in Servlets. (for Spring mvc simple form handling example-click here and for Multiple submit buttons in spring mvc form – click here). […]
Thank you, this is the most straightforward example I found.