[springmvc]rendering view

1. DispatcherServlet render Views

  Alternative to having a HttpMessageConverter write the response body

  Designed for generating text/* content from a template

    @RequestMapping(value="html", method=RequestMethod.GET)
public String prepare(Model model) {
model.addAttribute("foo", "bar");
model.addAttribute("fruit", "apple");
return "views/html";
}

2. Model parameter to export data to the view

  Call model.addAttribute(“name”, value) for each item to export

    @RequestMapping(value="/viewName", method=RequestMethod.GET)
public void usingRequestToViewNameTranslator(Model model) {
model.addAttribute("foo", "bar");
model.addAttribute("fruit", "apple");
}

3. Select the view by to render by returning a String

  Do not use @ResponseBody annotation in this case

  Configured ViewResolver maps name to a View instance

    @RequestMapping(value="pathVariables/{foo}/{fruit}", method=RequestMethod.GET)
public String pathVars(@PathVariable String foo, @PathVariable String fruit) {
// No need to add @PathVariables "foo" and "fruit" to the model
// They will be merged in the model before rendering
return "views/html";
}

4. Default ViewResolver forwards to internal servlet resources

  Many other options: JSP, Tiles, Freemarker, Velocity, iText PDF, JExcel, Jasper Reports, and XSLT are all supported out of the box

  Can also write your own View integrations

    @RequestMapping(value="dataBinding/{foo}/{fruit}", method=RequestMethod.GET)
public String dataBinding(@Valid JavaBean javaBean, Model model) {
// JavaBean "foo" and "fruit" properties populated from URI variables
return "views/dataBinding";
}





原文地址:https://www.cnblogs.com/lavieenrose/p/2417295.html