一. 简单配置 (XML)
1. web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- 配置:所有请求由SpringMVC管理 --> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
2. DispatchServlet-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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- 1. 配置 url 处理映射 --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!-- 2.配置控制器处理适配器 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> <!-- 3. 配置控制器 (访问路径) --> <bean name="/user.do" class="com.q.backoffice.web.controller.UserController" /> <!-- 4. 配置资源视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/views/"></property> <!-- 后缀 --> <property name="suffix" value=".jsp"></property> </bean> </beans>
3. UserController.java
package com.q.backoffice.web.controller; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class UserController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView mav = new ModelAndView("user/userlist"); mav.addObject("name", "q"); return mav; } }
4. userlist.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>用户列表</title> </head> <body> <h3>用户列表</h3> ${name} </body> </html>
二. 简单配置 (注解)
1. web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- 配置:所有请求由SpringMVC管理 --> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
2. DispatcherServlet-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- 配置:所有请求由SpringMVC管理 --> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
3. UserController
@Controller @RequestMapping("/user") // 根路径 public class UserController{ // 获取页面 @RequestMapping("/list") public String list(){ return "user/userlist"; } }
三. 获取表单发送的数据
1. 三种方式:
// 获取表单发送的数据,方式一(在方法中传递参数):
@RequestMapping(value = "/register", method = RequestMethod.POST) public String register(String username, String password, int age, String birthday, String[] hobbyIds){ System.out.println(username); System.out.println(password); System.out.println(age); System.out.println(birthday); System.out.println(Arrays.toString(hobbyIds)); return "user/register"; }
// 获取表单发送的数据,方式二 (创建一个User类):
@RequestMapping("/register2") public String register2(User user){ System.out.println(user); return "user/register"; }
// 获取表单发送的数据,方式三(在类中嵌套一个类,需要在页面中加上 类属性:user.username):
@RequestMapping("/register3") public String register3(UserExt user){ System.out.println(user); return "user/register"; }
2. 接收 ArrayList 类型数据
设置(记得 set/get 方法):
private List<User> users = new ArrayList<User>();
发送:
// 一个 user对象 <input type="text" name="user[0].username"> // 两个 user对象 <input type="text" name="user[1].username">
接收:
public String register(UserExt userExt){ System.out.println(userExt.getUsers()); reutrn "user/info"; }
3. 接收 Map 类型数据
设置(记得 set/get 方法):
private Map<String, Object> info = new HashMap<String, Object>();
发送:
<input type="text" name="info["username"]">
接收:
public String register(UserExt userExt){ System.out.println(userExt.getInfo()); reutrn "user/info"; }
四. 展示数据
1. UserController.java
@RequestMapping("userlist") public String register4(Model model){ // 模拟数据库数据 List<User> userList = new ArrayList<User>(); User user1 = new User("q","123",1,"2018"); user1.setId(1); User user2 = new User("q2","456",1,"2018"); user2.setId(2); userList.add(user1); userList.add(user2); // 将数据存储到 Modle 中 model.addAttribute("userList",userList); return "user/userlist"; }
2. userlist.jsp
<table border="1"> <tr> <td>账号</td> <td>密码</td> <td>年龄</td> <td>生日</td> </tr> <c:forEach items="${userList}" var="user"> <tr> <td>${user.username}</td> <td>${user.password}</td> <td>${user.age}</td> <td>${user.birthday}</td> </tr> </c:forEach> </table>
五. 修改数据
1. 方式一:
// 修改数据1: @RequestMapping("/edit") public String register5(int id, Model model){ // 模拟数据库数据 User user = new User("q","123",1,"2018/1/1"); user.setId(1); model.addAttribute("user", user); return "user/edit"; }
# 页面设置:
<a href="${pageContext.request.contextPath}/user/edit.do?id=${user.id}">修改1</a>
2. 方式二:
// 修改数据2: @RequestMapping("/edit/{id}") public String register5(Model model){ // 模拟数据库数据 User user = new User("q","123",1,"2018/1/1"); user.setId(1); model.addAttribute("user", user); return "user/edit"; }
# 页面设置:
<a href="${pageContext.request.contextPath}/user/edit/${user.id}.do">修改2</a>
六. 转发
@RequestMapping("test1") public String run1(){ // 同一个控制器的转发 return "forward:list.do"; }
七. 重定向
@RequestMapping("test2") public String run2(){ // 同一个控制器的转发 return "redirect:list.do"; }
八. 路径指定参数(如果没有该参数,就会抛异常)
// defaultValue 设置默认值(即使没有该参数也会跳转) @RequestMapping("test3") public String run3(@RequestParam(value = "uid", required = true, defaultValue = "1") Integer id){ System.out.println(id); return "redirect:list.do"; }
八. 接收和响应 json 数据
package com.q.backoffice.web.controller; import model.Student; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @Controller @RequestMapping("/student") // 根路径 public class UserController3 { @RequestMapping("/register") public String register(){ return "student/register"; } // 接收和相应 Json 数据 /* * @RequestBody :将 json 数据转换成模型对象 * @ResponseBody :返回 json 数据,把对象转换为字符串返回 * */ @RequestMapping("/get") public @ResponseBody Student get_json(@RequestBody Student s){ System.out.println(s); return s; } @RequestMapping("/get1") public @ResponseBody Student get_json1(Student s){ System.out.println(s); return s; } }
也许每个人都是无聊的,孤独的吧?