Spring MVC Example – Simple Form Handling:
Let us see an example of Spring MVC (form handling method). This application is tested using NetBeans.
Files to be Created:
Login.jsp
success.jsp
employeeLogin.java
employeeController.java
Login.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>Login Page</title> </head> <body> <s:form name="login" method="post"> <table> <tr> <td> Username </td> <td> <input type="text" name="ename"> </td> </tr> <tr> <td> Password </td> <td> <input type="password" name="epassword"> </td> </tr> <tr> <td colspan="2"> <input type="submit" name="Login"> </td> </tr> </table> <p><span style="color:red">${error}</span></p> </s:form> </body> </html>
success.jsp
<%@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>Welcome ${username}, You have successfully Logged-in!</h1> </body> </html>
employeeController.java
package com; 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; @Controller @RequestMapping(value="/Login.htm") public class employeeController { @RequestMapping(method = RequestMethod.GET) public String method1(ModelMap modelmap) { modelmap.put("Welcome", "This is spring mvc"); return "Login"; } @RequestMapping(method = RequestMethod.POST) public String access(@ModelAttribute ("employeeLogin")employeeLogin emplogin, ModelMap modelmap) { if(emplogin.getEname().equals("Alpha")&&emplogin.getEpassword().equals("123456")) { modelmap.addAttribute("username", emplogin.getEname()); return "success"; } else { modelmap.put("error", "Invalid Credentials"); return "Login"; } } }
employeeLogin.java
public class employeeLogin { String ename; String epassword; public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getEpassword() { return epassword; } public void setEpassword(String epassword) { this.epassword = epassword; } }
applicationContext.xml
<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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> </beans>
dispatcher-servlet.xml
<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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> <!-- Most controllers will use the ControllerClassNameHandlerMapping above, but for the index controller we are using ParameterizableViewController, so we must define an explicit mapping for it. --> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="index.htm">indexController</prop> </props> </property> </bean> <context:component-scan base-package="com" /> <mvc:annotation-driven /> <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>
Output:
[…] 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). […]
[…] 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 […]