SpringMVC的几种返回方式(get请求)

  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.       
  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.     //对于ModelAndView构造函数可以指定返回页面的名称,也可以通过setViewName方法来设置所需要跳转的页面;  
  32.       
  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.     //返回的是一个包含模型和视图的ModelAndView对象;  
  41.       
  42.     /** 
  43.      * Model一个模型对象, 
  44.      * 主要包含spring封装好的model和modelMap,以及java.util.Map, 
  45.      * 当没有视图返回的时候视图名称将由requestToViewNameTranslator决定;  
  46.      * @return 
  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.     //响应的view应该也是该请求的view。等同于void返回。  
  56.       
  57.     //返回String  
  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. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值