ITeye博客

springMVC 的几种返回方式

chengtong_java · 2015-11-18 15:26 · springMVC · 企业架构 · Markdown

  1. package com.boventech.learning.controller;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.ui.Model;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.bind.annotation.RequestParam;
  9. import org.springframework.web.bind.annotation.ResponseBody;
  10. import org.springframework.web.servlet.ModelAndView;
  11. import com.boventech.learning.entity.User;
  12. /**
  13. * MVCReturn
  14. * @author peng.xia
  15. *
  16. */
  17. @Controller
  18. @RequestMapping("/MVCReturn")
  19. public class SpringMVCReturnController {
  20. //对于ModelAndView构造函数可以指定返回页面的名称,也可以通过setViewName方法来设置所需要跳转的页面;
  21. @RequestMapping(value="/index1",method=RequestMethod.GET)
  22. public ModelAndView index(){
  23. ModelAndView modelAndView = new ModelAndView("/user/index");
  24. modelAndView.addObject("name", "xxx");
  25. return modelAndView;
  26. }
  27. //返回的是一个包含模型和视图的ModelAndView对象;
  28. @RequestMapping(value="/index2",method=RequestMethod.GET)
  29. public ModelAndView index2(){
  30. ModelAndView modelAndView = new ModelAndView();
  31. modelAndView.addObject("name", "xxx");
  32. modelAndView.setViewName("/user/index");
  33. return modelAndView;
  34. }
  35. /**
  36. * Model一个模型对象,
  37. * 主要包含spring封装好的model和modelMap,以及java.util.Map,
  38. * 当没有视图返回的时候视图名称将由requestToViewNameTranslator决定;
  39. * 响应的view应该也是该请求的view。等同于void返回。
  40. */
  41. @RequestMapping(value="/index3",method=RequestMethod.GET)
  42. public Map<String, String> index3(){
  43. Map<String, String> map = new HashMap<String, String>();
  44. map.put("1", "1");
  45. //map.put相当于request.setAttribute方法
  46. return map;
  47. }
  48. //返回String,这里的string相当于 modelAndView.setViewName("/user/index");
  49. //通过model进行使用
  50. @RequestMapping(value="/index4",method = RequestMethod.GET)
  51. public String index(Model model) {
  52. String retVal = "user/index";
  53. User user = new User();
  54. user.setName("XXX");
  55. model.addAttribute("user", user);
  56. return retVal;
  57. }
  58. //通过配合@ResponseBody来将内容或者对象作为HTTP响应正文返回(适合做即时校验);
  59. @RequestMapping(value = "/valid", method = RequestMethod.GET)
  60. @ResponseBody
  61. public String valid(@RequestParam(value = "userId", required = false) Integer userId,
  62. @RequestParam(value = "name") String name) {
  63. return String.valueOf(true);
  64. }
  65. //返回字符串表示一个视图名称,这个时候如果需要在渲染视图的过程中需要模型的话,就可以给处理器添加一个模型参数,然后在方法体往模型添加值就可以了,
  66. @RequestMapping(method=RequestMethod.GET)
  67. public void index5(){
  68. ModelAndView modelAndView = new ModelAndView();
  69. modelAndView.addObject("xxx", "xxx");
  70. }
  71. //返回的结果页面还是:/type
  72. //这个时候我们一般是将返回结果写在了HttpServletResponse 中了,如果没写的话,
  73. //spring就会利用RequestToViewNameTranslator 来返回一个对应的视图名称。如果这个时候需要模型的话,处理方法和返回字符串的情况是相同的。
  74. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。