Java - Spring MVC output response

Response to produce results mainly in two ways:
  @ResponseBody generated response text, returns a string
  ModelAndView use template engine render output, return to the page

 

1.@ResponseBody

@ResponseBody directly generate data in response thereof, the process does not involve any view.
@ResponseBody standard string can be generated / JSON / XML format data.
@ResponseBody be affected StringHttpMessageConverter.

 

2.ModelAndView

ModelAndView object refers to the "model (data) and the view (Interface)" object.
By ModelAndView can contain data objects to bind to the template engine.
Spring MVC View is the default JSP, you can also configure other template engines.

In the following webapp directory, create a JSP file.
Then write controller:

@GetMapping ( "/ View" )
 public ModelAndView ShowView () {
     // Test root path webapp, / view.jsp and can view.jsp. 
    MAV = ModelAndView new new ModelAndView ( "/ the view.jsp" );
     return MAV;
}

 General Back to back-end of time will certainly need to pass parameters, so we need to inject parameters to the View page:

@GetMapping("/view")
public ModelAndView showView(@RequestParam(value = "userid") Integer userId){
    UserInfo userInfo = new UserInfo();
    if (userId == 1){
        userInfo.setName ( "Kobe" );
        userInfo.setMobile("13167246031");
        userInfo.setAddress ( "Luotian" );
    } else {
        userInfo.setName ( "Chen Xing" );
        userInfo.setMobile("18972068500");
        userInfo.setAddress ( "Hubei Anlu" );
    }
    ModelAndView mav = new ModelAndView("/view.jsp");
    mav.addObject("user",userInfo);
    return mav;
}

Then you can receive the JSP page:
<H3> User: the user.name $ {} </ H3>
<H3> User: $ {user.address} </ H3>
access the page:

 

 Return in view of the map, list all possible, not only the object:
the Map <String, String> = new new HashMap the Map <String, String> ();
map.put ( "Sex", "male");
mav.addObject ( "map", map);

or direct line also returns a string:
mav.addObject ( "msg", "I want to return value");

 

3.ModelAndView core usage

mav.addObject () method sets the default attributes are stored in the current request.
The default ModelAndView use request forwarding (forward) to the page. It is passed intact to the current request to view.jsp. addObecj () data can be stored in the current request.
Redirection using new ModelAndView ( "redirect: /view.jsp" ), called redirection is please send a new request to access view.jsp

request / view in the process, the link has not changed:

 

 If we use ModelAndView ( "redirect: /view.jsp"), then the page will occur Jump:

 

 URL actually changed.
We found that the parameters seen, it is because the request contains the original data is cleared. So how to solve data loss problems?
MAV = new new ModelAndView ModelAndView ();
mav.setViewName ( "/ view.jsp"); // recommended to use absolute paths, software designed to reduce dependence on the external environment
mav.addObject ( "the User", userInfo);
return MAV;

we just set the address during a run on it.

 

4.String and ModelMap 

ModelMap ModelAndView can be achieved with a similar function.

@GetMapping("/show")
public String show(ModelMap modelMap){
    String view = "/um/view.jsp";
    modelMap.addAttribute ( "msg", "I Am Legend" );
     return View;
}

 

 

@ResponseBody methods are described, SpringMVC string itself directly responsive String

@GetMapping("/show")
@ResponseBody
public String show(ModelMap modelMap){
    String view = "/um/view.jsp";
    modelMap.addAttribute ( "msg", "I Am Legend" );
     return View;
}

 

Guess you like

Origin www.cnblogs.com/yangmingxianshen/p/12521560.html