package com.boventech.learning.controller;  

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

SpringMVC的页面几种返回方式的更多相关文章

  1. django 模板语法和三种返回方式

    模板 for循环 {% for athlete in athlete_list %} <li>{{ athlete.name }}</li> {% endfor %} if语句 ...

  2. Spring MVC三种返回方式

    spring mvc处理方法支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void. 下面一一进行说明: 1.ModelAndV ...

  3. SpringMVC的几种返回方式

    package com.boventech.learning.controller; import java.util.HashMap; import java.util.Map; import or ...

  4. asp.net 的页面几种传值方式

    http://www.cnblogs.com/makqiq/p/5882448.html 1.Querystring Querystring也叫查询字符串,这种页面间传递数据是利用网页地址URL.如果 ...

  5. SpringMVC 三种异常处理方式

    SpringMVC 三种异常处理方式 在 SpringMVC, SpringBoot 处理 web 请求时, 若遇到错误或者异常,返回给用户一个良好的错误信息比 Whitelabel Error Pa ...

  6. springMVC中controller的几种返回类型

    ==网文1,还不错,感觉比较老旧springMVC中controller的几种返回类型 - CSDN博客http://blog.csdn.net/qq_16071145/article/details ...

  7. SpringMVC请求后台地址URL没有.*的几种实现方式

    今天做项目,由于项目是通过扫二维码进入,二维码存放的地址不希望有 .do,而是http:xxxx:8080/xxx/yyy/zzz的格式(zzz为参数),但是项目其它请求url后面都必须要有.do,想 ...

  8. 【spring springmvc】这里有你想要的SpringMVC的REST风格的四种请求方式

    概述 之前的文章springmvc使用注解声明控制器与请求映射有简单提到过控制器与请求映射,这一次就详细讲解一下SpringMVC的REST风格的四种请求方式及其使用方法. 你能get的知识点 1.什 ...

  9. SpringMVC常用注解,返回方式,路径匹配形式,验证

    常用注解元素 @Controller 标注在Bean的类定义处 @RequestMapping 真正让Bean具备 Spring MVC Controller 功能的是 @RequestMapping ...

随机推荐

  1. cocos2d-x retain和release倒底怎么玩?

    转载请注明,原文地址: http://blog.csdn.net/musicvs/article/details/8689345 正文: 1. 为什么会有retain? C++和Java不一样,Jav ...

  2. 【linux c】setsockopt 详解

    转自:http://blog.csdn.net/zhonglinzhang/article/details/9183229 功能描述:        获取或者设置与某个套接字关联的选 项.选项可能存在 ...

  3. Java 集合系列之 Vector详细介绍(源码解析)和使用示例

    Vector简介 Vector 是矢量队列,它是JDK1.0版本添加的类.继承于AbstractList,实现了List, RandomAccess, Cloneable这些接口. Vector 继承 ...

  4. SharePoint 2016 安装 Cumulative Update for Service Bus 1.0 (KB2799752)报错

    前言 SharePoint 服务器场安装workflow manager 1.0的时候,报下面的错误,搜了很多博客都没有解决.然后,灵机一动,下载了一个英文版的累计更新包,安装成功了. SharePo ...

  5. 《TCP/IP网络编程》

    <TCP/IP网络编程> 基本信息 作者: (韩)尹圣雨 译者: 金国哲 丛书名: 图灵程序设计丛书 出版社:人民邮电出版社 ISBN:9787115358851 上架时间:2014-6- ...

  6. [Web 前端] React Router v4 入坑指南

    cp from : https://www.jianshu.com/p/6a45e2dfc9d9 万恶的根源 距离React Router v4 正式发布也已经过去三个月了,这周把一个React的架子 ...

  7. Orchard模块开发全接触6:自定义用户注册

    我们都知道 Orchard 的用户注册相当简单,现在,我们需要一个自定义的用户注册,现在,开始吧. 一:定义实体 Models/CustomerPartRecord.cs: public class ...

  8. [转]pear windows 安装

    FROM : http://jingyan.baidu.com/article/ca41422fd8cf3d1eae99ed3e.html 因为想使用phpdocument生成文档,不得不安装pear ...

  9. 系统吞吐量、TPS(QPS)、用户并发量、性能測试概念和公式

    PS:以下是性能測试的主要概念和计算公式,记录下: 一.系统吞度量要素: 一个系统的吞度量(承压能力)与request对CPU的消耗.外部接口.IO等等紧密关联.单个reqeust 对CPU消耗越高, ...

  10. go语言之进阶篇WriteString的使用

    1.WriteString的使用 示例: package main import ( "fmt" "os" ) func WriteFile(path stri ...