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>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置初始化参数:到 src 目录下加载 springmvc.xml 配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 注册 spring 核心编码过滤器,指定中文编码,防止中文乱码 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
springmvc.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"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 开启注解扫描:扫描指定包下的所有注解;需要引入 context 名称空间 -->
<context:component-scan base-package="com.springmvc.demo"/>
</beans>
emp.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/emp/findById.action" method="post">
<table border="2" align="center">
<tr>
<td>编号</td>
<td><input type="text" name="id" value="1"></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="username" value="哈哈"></td>
</tr>
<tr>
<td>月薪</td>
<td><input type="text" name="salary" value="7000"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="编 辑"></td>
</tr>
</table>
</form>
</body>
</html>
EmpAction.java:
package com.springmvc.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* 控制器:单例
* @Controller 注解:表示当前类是一个控制器
* @RequestMapping(value = "/emp") 此注解放在类上面,表示模块的根路径
*/
@Controller
@RequestMapping(value = "/emp")
public class EmpAction {
/**
* 业务方法:根据 id 查询员工数据
* @RequestMapping 注解:表示请求的映射,所有 /findById.action 的请求,都会直接进入该方法;
* method = RequestMethod.POST:表示此方法只能接受 POST 请求;
*
* 参数 id 用来接收表单提交的 编号数据;
*/
@RequestMapping(value = "/findById.action", method = RequestMethod.POST)
public String findById(int id) throws Exception{
System.out.println("查询 " + id + " 号员工的信息");
// 业务方法,根据 id 查询员工数据
// 转发到 updateById 业务方法:此方法中接收的 id 会共享到 updateById 方法中;
// 转发到 同一控制器的 不同业务方法,可以共享参数;
return "forward:/emp/updateById.action";
}
/**
* 业务方法:根据 id 更新员工数据
* @RequestMapping 注解:表示请求的映射,所有 /updateById.action 的请求,都会直接进入该方法;
* method = RequestMethod.POST:表示此方法只能接受 POST 请求;
*
* 参数 id 和 findById 方法中的 id 共享数据;
*/
@RequestMapping(value = "/updateById.action", method = RequestMethod.POST)
public String updateById(Model model, int id) throws Exception{
System.out.println("更新 " + id + " 号员工的信息");
// 业务方法,根据 id 更新员工数据
// 封装数据
model.addAttribute("message", "更新数据成功!");
// 跳转到 jsp 页面
return "/index.jsp";
}
}
注意:在同一控制器的 不同业务方法间 使用重定向,不可以共享参数;
重定向传递参数有两种方式:
1、通过 get 方法,将 id 作为参数传递过去,如下所示:
@RequestMapping(value = "/findById.action", method = RequestMethod.POST)
public String findById(int id) throws Exception{
System.out.println("查询 " + id + " 号员工的信息");
// 业务方法,根据 id 查询员工数据
// 转发到 updateById 业务方法:此方法中接收的 id 会共享到 updateById 方法中;
// return "forward:/emp/updateById.action";
// 重定向到 updateById 业务方法,通过 get 方法将 id 作为参数传递过去
return "redirect:/emp/updateById.action?id=" + id;
}
2、通过 HttpServletRequest 将 id 放入 session 域对象中传递过去,如下所示:
@RequestMapping(value = "/findById.action", method = RequestMethod.POST)
public String findById(int id, HttpServletRequest request) throws Exception{
System.out.println("查询 " + id + " 号员工的信息");
// 业务方法,根据 id 查询员工数据
// 转发到 updateById 业务方法:此方法中接收的 id 会共享到 updateById 方法中;
// return "forward:/emp/updateById.action";
// 重定向到 updateById 业务方法:通过 get 方法将 id 作为参数传递过去
// return "redirect:/emp/updateById.action?id=" + id;
// 重定向到 updateById 业务方法:将 id 添加到 session 域对象中传递过去
request.getSession().setAttribute("id", id);
return "redirect:/emp/updateById.action";
}
在 updateById 业务方法中获取 id:
@RequestMapping(value = "/updateById.action")
public String updateById(Model model, HttpServletRequest request) throws Exception{
// 从 session 域对象中获取 id
int id = (int) request.getSession().getAttribute("id");
System.out.println("更新 " + id + " 号员工的信息");
// 业务方法,根据 id 更新员工数据
// 封装数据
model.addAttribute("message", "更新数据成功!");
// 跳转到 jsp 页面
return "/index.jsp";
}