code show as below:
1.SpringMVC web.xml file: (DispatcherServlet configuration)
<?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" id="WebApp_ID" version="3.0"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
2.SpringMVC configuration file (springmvc-servlet.xml)
<?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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd " > <-! automatically scan packages -> < context: Scan-Component Base-Package =" com.xbf.controller " / > <! - static resource filter processing SpringMVC not static resources -> < MVC: the servlet-default-Handler /> <! - annotation driving -> < MVC: annotation-driven /> <-! view resolver -> < the bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > < Property name = "prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
3.Controller layer write:
Package com.xbf.controller; Import com.xbf.pojo.User; Import org.springframework.stereotype.Controller; Import org.springframework.ui.Model; Import org.springframework.web.bind.annotation.PathVariable; Import ORG. springframework.web.bind.annotation.RequestMapping; Import org.springframework.web.bind.annotation.RequestParam; @Controller public class the UserController { @RequestMapping ( "/ T1 / {name}" ) // RESTful style // distal request parameters and parameter names when processing the same // can take over directly with the // parameter name String name is the front-end request public String test1 (@PathVariable String name, the Model Model) { model.addAttribute ( "AAA" , name); return "User" ; } @RequestMapping ( "/ T2" ) // distal parameter name and pass over the process controller the parameter name is inconsistent // used for explanation @RequestPara annotation @ username: front end of the passed parameter names @ name: the name of the controller processing parameters public String test2 (@RequestParam (value = "username" ) String name , the Model Model) { model.addAttribute ( "AAA" , name); return "User" ; } @RequestMapping("/ T3" ) // pass the research object // using the set method to set the value of the property, if no value is passed to the default value, set value before determining whether it is consistent attribute name? ? ? // will transfer from the front end of the value of the object property to the object automatic packaging (User) public String Test3 (the User User, the Model Model) { System.out.println (User); model.addAttribute ( "User" , User) ; return "User" ; } @RequestMapping ( "/ T333 / {ID} / {name} / {} Age" ) // RESTful style public String test33 (the User User, the Model Model) { System.out.println (User) ; model.addAttribute ( "the User" , the User); ; } @RequestMapping("/t33/{id}/{name}/{age}") //restful风格 public String test33(@PathVariable int id,@PathVariable String name,@PathVariable int age,Model model){ User user = new User(); user.setName(name); user.setId(id); user.setAge(age); System.out.println(user); model.addAttribute("user",user); return "user"; } }
4. The front page (user.jsp)
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${aaa} <hr> <p> 姓名:${user.getName()} ID:${user.getId()} 年龄:${user.getAge()} </p> </body> </html>
pojo entity classes:
package com.xbf.pojo; public class User { private int id; private String name; private int age; public User() { } public User(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
Summary: DispatcherServlet will intercept the requests distributed to the front end of a specific controller, the model data is processed by the controller and returned.