文章目录
- Controller层请求
- SpringMVC参数绑定
- SpringMVC支持的参数类型
- 默认参数类型
- 基本参数类型基本的类型
- 自定义参数类型
- Controller请求返回值的类型
- 返回ModelAndView类型
- 返回String类型
- 返回逻辑视图名
- redirect重定向
- forward页面转发
- 返回void类型
- 通过request转向页面
- 通过response页面重定向
- 通过response进行页面响应
Controller层请求
在请求过程中使用@RequestMapping注解,将URL映射到controller的类或方法上。
@RequestMapping特点
- 1、窄化请求
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/test")
public @ResponseBody String test(){
return "test";
}
通过这种方式可以对URL进行分类管理,在类上添加该注解的URL,称之为根路径,需要通过根路径+子路径的方式访问。
- 2、限制HTTP请求方法
@RequestMapping(value = "/list" ,method = RequestMethod.POST)
这样限制HTTP请求为特定的方法可以起到保护URL的目的,如果指定为get方法请求,则除了get请求可以通过之外,其他请求都不能通过。
SpringMVC参数绑定
参数绑定即客户端发送请求,而请求中包含一些数据,这些数据传递到controller层对应的方法参数上,这就涉及到了参数绑定的问题了。
参数绑定过程
springMVC在客户端提交数据的过程中,客户端将数据以key-value形式提交。
处理器适配器调用springMVC提供的参数绑定组件将数据转化成controller方法的形参。
我们来看下面这个例子,我们从展示的学生列表页面,点击修改,将参数传入后台,再根据id拿到信息,返回修改页面。
列表页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>学生信息</title>
</head>
<body>
<table align="center" border="1">
<thead>
<tr>
<td>id</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>编辑</td>
</tr>
</thead>
<tbody>
<c:forEach items="${students}" var="student">
<tr>
<td>${student.SID}</td>
<td>${student.sname}</td>
<td>${student.ssex}</td>
<td>${student.sage}</td>
<td><a href="${pageContext.request.contextPath}/student/${student.SID}"/>修改</td>
</tr>
</c:forEach>
</tbody>
</table>
</form>
</body>
</html>
修改页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!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>修改页面</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/student/submit" method="post">
<table width="60%" border="1"align="center">
<h1 align="center">修改用户信息</h1>
<tr>
<td>用户ID</td>
<td> <input type="text" name="id" value="${student.SID }" readonly ></td> </td>
</tr>
<tr>
<td>用户名</td>
<td> <input id="username" type="text" name="username" value="${student.sname }"></td> </td>
</tr>
<tr>
<td>性别</td>
<td> <input id="sex" type="text" name="sex" value="${student.ssex }"></td> </td>
</tr>
<tr>
<td>地址</td>
<td> <input id="address" type="text" name="address" value="${student.sage }"></td> </td>
</tr>
<tr ><td colspan="2" align="center"><input type="submit" value="提交"/></td></tr>
</table>
</form>
</body>
</html>
handler:
@RequestMapping(value = "/{id}")
//{id} 通过符 @PathVariable注解将注解括号中的属性名绑定到当前的参数上
public ModelAndView edit(@PathVariable("id")int id) throws Exception {
Student student = studentService.selectStudentById(id);
//修改页面将原数据进行回显
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("student",student);
modelAndView.setViewName("editStudent");
return modelAndView;
}
SpringMVC支持的参数类型
默认参数类型
SpringMVC中支持默认参数类型,可以直接在参数上给出默认的参数类型,可以直接使用。
- HttpServletRequest request,通过request参数对象进行参数获取
- HttpServletResponse response,通过response对象处理响应信息
- HttpSession session,通过Session对象来获取Session中的对象
- Model model,Model是一个接口,modelMap是接口实现,将model数据封装到request中
request.getParameter("id");//前端页面传递的id属性通过request.getParameter进行接收
response.getWriter().write("");//响应结果
session.setAttribute("name","张三"); //写缓存信息
session.getAttribute("name");//获取缓存
model.addAttribute("name","key");//写数据
基本参数类型基本的类型
byte\short\int\long\float\double\char\blollean基本类型都能够进行支持
下面这个例子,以int类型为例
jsp页面:这里写好了num为10
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>数据提交</title>
</head>
<body>
<form action="/student/test" method="post">
<input name="num" value="10" type="text"/>
<buttom type="submit" value="提交"> </buttom>
</form>
</tbody>
</table>
</form>
</body>
</html>
controller层:
@RequestMapping("/test")
public void test(int num){
System.out.println("test:"+num);
}
如果表单中input的name和controller层上方法保持一致,就可以完成参数绑定。
如果不一致,可以通过注解@RequestParam完成参数绑定。
自定义参数类型
jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!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>修改页面</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/student/submit" method="post">
<table width="60%" border="1"align="center">
<h1 align="center">修改用户信息</h1>
<tr>
<td>用户ID</td>
<td> <input type="text" name="SID" value="${user.SID }" readonly ></td> </td>
</tr>
<tr>
<td>用户名</td>
<td> <input id="username" type="text" name="name" value="${ }"></td> </td>
</tr>
<tr>
<td>性别</td>
<td> <input id="sex" type="text" name="sex" value="${user.sex }"></td> </td>
</tr>
<tr>
<td>年龄</td>
<td> <input id="address" type="text" name="age" value="${user.age }"></td> </td>
</tr>
<tr ><td colspan="2" align="center"><input type="submit" value="提交"/></td></tr>
</table>
</form>
</body>
</html>
controller
@RequestMapping("/submit")
public void submit(Student student){
System.out.println("submit:"+student);
}
在进行自定义参数绑定时,input中name的值需要和自定义类型中属性名保持一致,才能完成自定义对象的映射,否则无法完成映射。
Controller请求返回值的类型
返回ModelAndView类型
modelAndView对象返回包含数据和逻辑视图名,或者仅仅是视图
@RequestMapping("/list")
public ModelAndView list(){
List<Student> students = studentService.selectAllStudents();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("students",students);
modelAndView.setViewName("studentlist");
return modelAndView;
}
返回String类型
返回逻辑视图名
即前缀+逻辑视图名+后缀
@RequestMapping(value = "/list1" )
public String list1(Model model) throws IOException {
List <Student> student = userService.selectAllStudents();
model.addAttribute("students",student);
return "/WEB-INF/jsp/studentlist.jsp";
}
redirect重定向
这里涉及到http协议中的一些知识,不了解的可以看看:http协议 重定向:浏览器上地址会改变,修改后的数据无法传递到重定向页面(request数据不能共享)
@RequestMapping("/submit")
public String submit(Student student){
System.out.println("id:"+student.getSID());
return "redirect:/student/list";
}
forward页面转发
浏览器地址不会发生改变,request数据可以共享
@RequestMapping("/submit")
public String submit(Student student){
System.out.println("id:"+student.getSID());
return "redirect:/student/list";
}
重定向和页面转发的异同点:
相同点:均是页面的跳转,均是由服务器端控制的跳转。
不同点:
转发:
- 1 是服务器行为
- 2 转发不会改变浏览器url
- 3 共享一个request对象
- 4 只能在同一web应用下使用,效率较高
重定向:
- 1是一种客户端行为
- 2会改变url
- 3不会共享url
- 4可以指向任意url
- 5必须是决定路径
返回void类型
使用request或response指定响应结果
通过request转向页面
@RequestMapping("/submit")
public void submit(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//forward
request.getRequestDispatcher("/student/list").forward(request, response);
}
通过response页面重定向
@RequestMapping("/submit")
public void submit(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//redirect
response.sendRedirect("/student/list");
}
通过response进行页面响应
@RequestMapping("/submit")
public void submit(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("hello test");
}