1. Spring project structure:
2. Basic configuration file:
(1) web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
(2) spring-mvc-servlet.xml (the name of this xml is the same as the servlet-name of dispatchServlet in web.xml. Of course, if you want to be different, you can configure different xml configuration file names under web.xml for the initial configuration of DispatcherServlet use)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.cisco.eStore" /> <context:annotation-config /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
3. MVC document path:
Controller is under com.xx.estore.controller, Model is under com.xx.estore.model, View is under the configuration path of spring-mvc-servlet.xml
(1) Controller:
package com.cisco.eStore.controller; import com.cisco.eStore.model.User; 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.servlet.ModelAndView; @Controller @RequestMapping("/admin") public class HelloWorldController { @RequestMapping(value = "/user", method = RequestMethod.GET) public ModelAndView user(){ return new ModelAndView("user", "command", new User()); } @RequestMapping(value = "/addUser", method = RequestMethod.POST) public String addUser(@ModelAttribute("SpringWeb")User user, ModelMap model){ model.addAttribute("name", user.getName()); model.addAttribute("password", user.getPassword()); model.addAttribute("email", user.getEmail()); return "result"; } }
(2) Model:
package com.cisco.eStore.model; public class User { private String name; private String password; private String email; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
(3) view:
user.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>User registration page</title> </head> <body> <h1>This is Add User Page</h1> <form:form method="POST" action="/admin/addUser"> <table> <tr> <td>Name</td> <td><form:input path="name"/></td> </tr> <tr> <td>Password</td> <td><form:input path="password"/> </td> </tr> <tr> <td>Email</td> <td><form:input path="email"/></td> </tr> <tr> <td colspan="2"> <input type="submit" value="Submit"/> </td> </tr> </table> </form:form> </body> </html>
result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Results page</title> <style> label{ margin-right:20px; } </style> </head> <body> <h1>This is SpringMVC Result Page/h1> <label>name:</label><span>${name}</span><br/> <label>password:</label><span>${password}</span><br/> <label>email:</label><span>${email}</span><br/> </body> </html>
4. Demo function:
(1) Jump to the registered user page:
The Controller.user() method in the above will jump to user.jsp, and at the same time create a new user object in jsp(2) Add users:
The above Controller.addUser() method will fill the name, password, email information from the page into the modelMap for use by result.jsp
(3) Display adding user information:
After the above Controller.addUser(), return "result" will directly jump to result.jsp, and use the ${var} method in the page to get the corresponding parameters