一、处理请求
(一)处理请求的方式
1.ModelAndView: 通过setViewName方法
2.不响应: void + @Respository注解
3.直接指定响应页面: 返回值为String类型,返回结果指定跳转地址
4.重定向: 跳转地址前到redirect:前缀即可
5.HttpServletRequest和HttpServletResponse: 形参中声明这两个变量,然后通过相关api跳转
(二)具体代码演示
web.xml和spring-mvc.xml配置采用注解配置,以及index.jsp页面代码都未改变,具体可参考SpringMVC注解配置
以下为AnnotationController自定义控制层类
1.ModelAndView
`@Controller
//@RequestMapping("/hello")
public class AnnotationController {
/**
* 服务器端响应请求
* ModelAndView
* @return
*/
@RequestMapping("/h1")
public ModelAndView hello1(){
System.out.println("-----hello1------");
ModelAndView mav = new ModelAndView();
mav.setViewName("/index.jsp");
return mav;
}
}`
2.不响应请求
public class AnnotationController {
/**
* 服务器端不响应请求
*/
@RequestMapping("/h2")
@ResponseBody
public void hello2(){
System.out.println("-------h2-----");
}
}
3.直接指定响应页面:
//**(1)有return值,并且还有@ResponseBody,那么会将return的值以字符串的形式返回到客户端页面**\
//**(2)返回路径注意: 返回的字符带"/“表示从根目录下开始找,不带”/"从当前目录下查找**
@Controller
@RequestMapping("/hello")
public class AnnotationController {
/**
* 服务器端响应字符串
* @return
*/
@RequestMapping("/h3")
@ResponseBody
public String hello3(){
System.out.println("------h3------");
return "hello ...你好啊";//存在中文乱码问题
}
/**
* 服务器端响应一个页面,绝对路径
* @return
*/
@RequestMapping("/h4")
public String hello4(){
System.out.println("------h4------");
return "/index.jsp";
}
/**
* 有return值,并且还有@ResponseBody,那么会将return的值以字符串的形式返回到客户端页面
* @return
*/
@RequestMapping("/h5")
@ResponseBody
public String hello5(){
System.out.println("------h5------");
return "index.jsp";
}
/**
* 服务器端响应一个页面,相对路径
* 相对路径默认在
* @return
*/
@RequestMapping("/h6")
public String hello6(){
System.out.println("------h6------");
return "index.jsp"; //错误提示/SpringMVC-03-Response/hello/index.jsp路径找不到
}
}
4.重定向:跳转地址前到redirect:前缀即可
@Controller
//@RequestMapping("/hello") //若没有注释这个,提示错误
public class AnnotationController {
/**
* @return
*/
@RequestMapping("/h7")
public String hello7(){
System.out.println("------h7------");
return "redirect:index.jsp";
}
/**
* @return
*/
@RequestMapping("/h8")
public String hello8(){
System.out.println("------h8------");
return "redirect:/index.jsp";
}
}
h7和h8都能成功跳转页面
5.HttpServletRequest和HttpServletResponse
@Controller
//@RequestMapping("/hello") //若没有注释这个,提示错误
public class AnnotationController {
@RequestMapping("/h9")
@ResponseBody
public void hello5(HttpServletRequest req,HttpServletResponse resp){
System.out.println("---h9--");
try {
req.getRequestDispatcher("/index.jsp").forward(req, resp);
} catch (Exception e) {
e.printStackTrace();
}
}
@RequestMapping("/h10")
@ResponseBody
public void hello6(HttpServletRequest req,HttpServletResponse resp){
System.out.println("---h10--");
try {
// resp.sendRedirect("/index.jsp");//客户端跳转到http://localhost:8080/index.jsp
//客户端跳转到http://localhost:8080/SpringMVC-08-Annotation/hello/index.jsp
//因为该类配置了映射路径为:/hello
resp.sendRedirect("index.jsp");//将类映射路径注释掉,可以访问m
} catch (IOException e) {
e.printStackTrace();
}
}
}
二、处理请求传入的数据
(一)接收传入参数的类型
1.基本数据类型:直接在形参中声明
2.简单对象:直接在形参中声明
3.包装对象:通过"."处理,比如user.name
4.集合类型:不能直接再形参中声明,只能再对象中使用
5.数组类型:可以再形参中声明,也可以在对象中使用
6.Date类型:需要自定义转换器,将传入的String类型的值转换成Date类型
(二)具体代码演示
1.web.xml文件未改变
2.JavaBean类(需要设置getter方法)
public class User {
private Integer id;
private String name;
private Book book;
private List<String> favorites;
}
public class Book {
private Integer bookId;
private String bookName;
private String author;
}
3.index.jsp,user.jsp,user1.jsp
<!-- index.jsp -->
<body>
<h1>Hello SpringMVC</h1>
</body>
<!-- user.jsp -->
<body>
<form action="fun4" method="post">
<table>
<tr>
<td>编号</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>图书编号</td>
<td><input type="text" name="book.id"></td>
</tr>
<tr>
<td>书名</td>
<td><input type="text" name="book.bookName"></td>
</tr>
<tr>
<td>作者</td>
<td><input type="text" name="book.author"></td>
</tr>
<tr>
<td><input type="submit" value="添加"></td>
</tr>
</table>
</form>
</body>
<!-- user1.jsp -->
<body>
<form action="fun7" method="get">
<table>
<tr>
<td>编号</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>兴趣爱好</td>
<td>
<input type="checkbox" name="favorites" value="zuqiu">足球
<input type="checkbox" name="favorites" value="lanqiu">篮球
<input type="checkbox" name="favorites" value="pingpang">乒乓球
</td>
</tr>
<tr>
<td><input type="submit" value="注册"></td>
</tr>
</table>
</form>
</body>
4.自定义控制层代码
@Controller
public class AnnotationController {
/**
* 1-1.基本数据类型:方法参数和网页传递参数一致
* @param id
* @param name
*/
@RequestMapping("/fun1")
@ResponseBody
public void fun(Integer id,String name){
System.out.println(id+"---"+name);
}
/**
* 1-2.基本数据类型:方法参数和网页传递参数不一致
* @param id
* @param name
*/
@RequestMapping("/fun2")
@ResponseBody //服务器端没有响应数据给客户端时,一定要加这个
public void fun1(Integer id,@RequestParam("username")String name){
System.out.println(id+"---"+name);
}
/**
* 1-3.基本数据类型:方法参数和网页传递参数不一致
* 方法参数,非必要,required=false,或者设置默认值
* 如果username没有设值按照默认值输出,传值按照传入的值输出
* @param id
* @param name
*/
@RequestMapping("/fun3")
@ResponseBody //服务器端没有响应数据给客户端时,一定要加这个
public void fun2(Integer id,@RequestParam(value="username",defaultValue="111")String name){
System.out.println(id+"---"+name);
}
/**
* 2.简单对象
* 3.包装对象
* 将输入的参数封装到一个对象中(根据对象的属性匹配传入的参数)
* 值可以通过地址栏传参也可以使用form表单
* @param user
*/
@RequestMapping("/fun4")
@ResponseBody //服务器端没有响应数据给客户端时,一定要加这个
public void fun3(User user){
System.out.println(user);
}
/**
* 4.集合类型
* 5.数组类型
* 将输入的参数封装到一个对象中(根据对象的属性匹配传入的参数)
* 多选框checkbox参数值传入
* @param user
*/
@RequestMapping("/fun5")
@ResponseBody //服务器端没有响应数据给客户端时,一定要加这个
public void fun4(User user){
System.out.println(user);
for (String s : user.getFavorites()) {
System.out.println(s);
}
}
/**4.集合类型
* 无法直接用一个集合去接收数据,会报错Failed to instantiate [java.util.List]: Specified class is an interface
* 如果是用对象去接收传入数据,而对象封装了List集合,这样才可以接收到值
* 多选框checkbox参数值传入 集合
* @param user
*/
@RequestMapping("/fun7")
@ResponseBody //服务器端没有响应数据给客户端时,一定要加这个
public void fun7(Integer id,String name,List<String> favorites){
System.out.println(id+"----"+name);
for (String s : favorites) {
System.out.println(s);
}
}
/**6.Date类型
* 当接收类型是Date类型时,需要通过转换器接收
* 需要自定义String到Date转换器,并且在SpringMVC.xml文件中配置转换器
* @param t
*/
@RequestMapping("/fun8")
@ResponseBody
public void fun8(Date t){
System.out.println(t);
}
}
5.自定义String到Date转换器
**
* 转换器类,需要实现Converter,并指定从装换和被转换成的泛型
* @author 行没咯
*
*/
public class StringToDateConverter implements Converter<String, Date> {
@Override
public Date convert(String msg) {
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
try {
return date.parse(msg);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
6.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 开启自动扫描 -->
<context:component-scan base-package="com.luo.controller"></context:component-scan>
<!-- 开启mvc注解配置 -->
<mvc:annotation-driven conversion-service="formattingConversionServiceFactoryBean"></mvc:annotation-driven>
<!-- 配置转换器 -->
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean "
id="formattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.luo.converter.StringToDateConverter"></bean>
</set>
</property>
</bean>
</beans>
三、响应请求
(一)响应请求方式
1.ModelAndView
2.HttpServletRequest
3.HttpSession
4.Map
5.Model
6.ModelMap
(二)具体代码演示
1.web.xml,SpringMVC.xml与第二大点一致
2.index.jsp
<body>
<h2>request:${requestScope.msg}</h2>
<h2>session:${sessionScope.msg}</h2>
<h2>application:${applicationScope.msg}</h2>
</body>
3.自定义controller层
@Controller
@SessionAttributes("msg") //设置msg的生命周期时session
public class AnnotationController {
/**
* ModelAndView方式响应数据
* 转换到index.jsp页面中显示数据
* @return
*/
@RequestMapping("/q1")
public ModelAndView query1(){
ModelAndView mav = new ModelAndView();
mav.setViewName("/index.jsp");
return mav;
}
/**
* HttpServletRequest方式响应数据
* 利用服务器端跳转到index.jsp页面
* @param req
*/
@RequestMapping("/q2")
@ResponseBody
public void query2(HttpServletRequest req,HttpServletResponse resp){
try {
req.getRequestDispatcher("/index.jsp").forward(req, resp);
} catch (ServletException | IOException e) {
e.printStackTrace();
};
}
/**
* HttpSession方式响应数据
* 利用服务器端跳转到index.jsp页面
* @param req
*/
@RequestMapping("/q3")
public String query3(HttpSession session){
session.setAttribute("msg", "session msg...");
return "/index.jsp";
}
/**
* Map方式响应数据
* 利用服务器端跳转到index.jsp页面
* @param req
*/
@RequestMapping("/q4")
public String query4(Map<String,Object> map){
map.put("msg", "map msg..");
return "/index.jsp";
}
/**
* Model方式响应数据
* 利用服务器端跳转到index.jsp页面
* @param req
*/
@RequestMapping("/q5")
public String query5(Model m){
m.addAttribute("msg", "model msg...");
return "/index.jsp";
}
/**
* ModelMap方式响应数据
* 利用服务器端跳转到index.jsp页面
* @param req
*/
@RequestMapping("/q6")
public String query6(Model m){
m.addAttribute("msg", "modelMap msg...");
return "/index.jsp";
}
}