SpringMVC学习二(日期参数/数据保存/重定向)

  • 接受的参数为日期类型
  • controller进行数据保存
  • Controller如何进行重定向跳转

1.对于前端页面传来日期类型的数据时如何进行处理,有两种方法

  1.1在对应的Controller中插入代码,对于其中的SimpleDateFormat("yyyy-MM-dd")部分可以更改,例如加上时分秒HH:mm:ss

@InitBinder
public void initBinder(ServletRequestDataBinder binder){
    //只要网页中传来的数据格式为yyyy-MM-dd 就会转化为Date类型
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}

  当有了上面的代码时,在执行下面的代码之前会先执行上面的代码,从而进行格式处理

@RequestMapping("toDate.do")
public String toDate(Date date) {
    System.out.println(date);
    return "index";
}

  1.2在实体类的属性中加入注解

@DateTimeFormat(pattern="yyyy-MM-dd")shu//不是输出的结果格式,是接受的参数格式
private Date birthday;

通过这两种方式可处理

2.Controller进行数据保存

 数据保存到request作用域的方式.

  • 使用ModelAndView,那么该方法的返回类型必须是ModelAndView
  • 使用Model, 方法的返回值还是字符串类型。
  • 使用Map.方法的返回值还是字符串类型。
  • 原始的HttpServletRequest对象保存

 数据保存到session作用域的方式.

  • 使用原始的HttpSession保存。
  • 使用注解@SessionAttributes(name={key1,key2})

前提:我们在index.jsp中先写好以下代码进行接收,进行对比

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    这里是Index<br>
    输出结果为:===requestScope==${requestScope.name}<br>
    ===========sessionScope==${sessionScope.name}<br>
    ===========applicationScope==${applicationScope.name }
</body>
</html>

2.1使用ModelAndView,那么该方法的返回类型必须是ModelAndView

 1 @Controller
 2 @RequestMapping("/users/")
 3 @SessionAttributes(names= {"name","address"})
 4 public class UsersController {
 5     
 6     @RequestMapping("index.do")
 7     public ModelAndView index() {
 8         //1.保存到ModelAndView中,返回类型也是ModelAndView
 9         ModelAndView mv=new ModelAndView("index");
10         mv.addObject("name", "我在ModelAndView中");
11         return mv;
12     }
13 }

结果为:

 2.2使用Model, 方法的返回值还是字符串类型。

@RequestMapping("index2.do")
public String index(Model model) {
    //2.保存到Model,返回值还是字符串类型
    model.addAttribute("name","我在Model中");
    return "index";
}

结果为:

 2.3使用Map.方法的返回值还是字符串类型

@RequestMapping("index3.do")
public String index3(Map<String, Object> map) {
    //3.保存到Map
    map.put("name","我在Map中");
    return "index";
}

结果为:

2.4 使用原始的HttpSession保存,这是数据保存到session作用域的两种方式之一(其一)

@RequestMapping("index4.do")
public String index4(HttpSession session) {
    //3.保存到session
    session.setAttribute("name","我在session中");
    return "index";
}

结果为:

 2.5对于想要存在application中的方法

@RequestMapping("index5.do")
public String index5(Model model,HttpSession session) {
    //5.存放结果到application中    
    model.addAttribute("name","model_session");
    session.getServletContext().setAttribute("name", "application");
    return "index";
}

结果为:

 至此我们发现,所有的sessionScope都可以获取值,是因为每一个都加上了注解:@SessionAttributes(names= {"name","address"}),这是数据保存到session作用域的两种方式之一(其二)

@SessionAttributes(names= {"name","address"})

3.Controller如何进行重定向跳转

因为默认为请求转发的方式,所以若需要重定向,则需要加入一点小小的bang助

@RequestMapping("img.do")
public String img(HttpSession session) {
    //重定向
    session.setAttribute("name","我是session中");
    return "redirect:red.do";//此处redirect提供重定向的作用
}
    
@RequestMapping("red.do")
public String red(Model model) {
    model.addAttribute("name","我在Model中");
    return "img";
}

img.jsp页面代码为:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    ${requestScope.name}
    <img src="/SpringMVC09_03/images/b4.jpg">
</body>
</html>

页面跳转成功,显示如下

猜你喜欢

转载自www.cnblogs.com/murmansk/p/11456137.html