`

springMVC 的几种返回方式

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

相关推荐

Global site tag (gtag.js) - Google Analytics