SpringMVC learning two (date parameters / data storage / redirect)

  • Accepted parameters for the date type
  • controller data to be saved
  • How to redirect Jump Controller

1. respect to the front page date type coming how data is processed in two ways

  1.1 Controller inserted in the corresponding code, for which the SimpleDateFormat ( "yyyy-MM-dd ") moiety may be changed, for example, plus minutes and seconds HH: mm: ss

@InitBinder
 public  void of an initBinder (ServletRequestDatabinder Binder) {
     // long as the page data from the format of yyyy-MM-dd will be converted to the Date type 
        binder.registerCustomEditor (Date. Class , new new the CustomDateEditor ( new new the SimpleDateFormat ( "the MM-YYYY -dd "), to true )); 
}

  When given the above code, the following code is executed before the first performs the above code, thereby performing formatting

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

  1.2 annotations in the properties of entity classes

@DateTimeFormat (pattern = "the MM-dd-YYYY" ) format // Shu not result output parameter format is accepted
 Private a Date Birthday;

By these two methods can be processed

2.Controller data to be saved

 Save data to the request scope of way.

  • Use ModelAndView, then the method's return type must be ModelAndView
  • Use Model, the return value of the method is a string type.
  • Use Map. Return value is a string type.
  • The original HttpServletRequest object to save

 Save the data to the session scope of way.

  • Use the original HttpSession save.
  • Use annotations @SessionAttributes (name = {key1, key2})

Premise: Let's write the following code in index.jsp to receive, compare

<%@ 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, then the method's return type must be 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 }

The results are:

 

 

 2.2 Model, the return value of the method is a string type.

@ RequestMapping ( "index2.do" )
 public String index (Model Model) {
     // 2. Save the Model, the return value is a string type 
    model.addAttribute ( "name", "I am in the Model" );
     return "index " ; 
}

The results are:

 

 

 2.3 Map. Return value or string type

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

The results are:

 

 

2.4  using the original HttpSession preservation, which is one of two ways to save data session scope (one)

 

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

 

The results are:

 

 

 2.5 For the application of the method you want to exist

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

The results are:

 

 

 So far we have found that all sessionScope can get the value, because each one adding a comment: @SessionAttributes (names = { "name ", "address"}), which is to save data to the session in two ways scopes one (Second)

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

How 3.Controller redirect Jump

Because the default request forwarded by the way, so if you need to redirect, you need to add a little bang to help

@ RequestMapping ( "img.do" )
 public String img (the HttpSession session) {
     // redirect 
    session.setAttribute ( "name", "I was in the session" );
     return "redirect: red.do"; // here providing redirection redirect action 
} 
    
@RequestMapping ( "red.do" )
 public String Red (Model Model) { 
    model.addAttribute ( "name", "I am in the Model" );
     return "IMG" ; 
}

img.jsp page code is:

<%@ 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>

Page jump success, shown below

 

Guess you like

Origin www.cnblogs.com/murmansk/p/11456137.html