spring学习之springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序
spring mvc处理方法支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void。下面将对具体的一一进行说明:
ModelAndView
@RequestMapping("/show1")
public ModelAndView show1(HttpServletRequest request, HttpServletResponse response) throwsException {
ModelAndView mav = newModelAndView("/demo2/show");
mav.addObject("account", "account -1");
return mav;
}
通过ModelAndView构造方法可以指定返回的页面名称,也可以通过setViewName()方法跳转到指定的页面 ,使用addObject()设置需要返回的值,addObject()有几个不同参数的方法,可以默认和指定返回对象的名字。
调用addObject()方法将值设置到一个名为ModelMap的类属性,ModelMap是LinkedHashMap的子类,具体请看类。
public class ModelAndView {
/** View instance or view name String */
private Object view;
/** Model Map */
private ModelMap model;
/** Indicates whether or not this instance has been cleared with a call to {@link #clear()} */
private boolean cleared = false;
}
Model 是一个接口, 其实现类为ExtendedModelMap,继承了ModelMap类。
Class : ExtendedModelMap
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.springframework.ui; import java.util.Collection;
import java.util.Map; /**
* Subclass of {@link ModelMap} that implements the {@link Model} interface.
* Java 5 specific like the {@code Model} interface itself.
*
* @author Juergen Hoeller
* @since 2.5.1
*/
@SuppressWarnings("serial")
public class ExtendedModelMap extends ModelMap implements Model { @Override
public ExtendedModelMap addAttribute(String attributeName, Object attributeValue) {
super.addAttribute(attributeName, attributeValue);
return this;
} @Override
public ExtendedModelMap addAttribute(Object attributeValue) {
super.addAttribute(attributeValue);
return this;
} @Override
public ExtendedModelMap addAllAttributes(Collection<?> attributeValues) {
super.addAllAttributes(attributeValues);
return this;
} @Override
public ExtendedModelMap addAllAttributes(Map<String, ?> attributes) {
super.addAllAttributes(attributes);
return this;
} @Override
public ExtendedModelMap mergeAttributes(Map<String, ?> attributes) {
super.mergeAttributes(attributes);
return this;
} @Override
public Map<String, Object> asMap() {
return this;
} }
Class :
@RequestMapping("/demo2/show")
public Map<String, String> getMap() {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value-1");
map.put("key2", "value-2");
return map;
}
在jsp页面中可直通过${key1}获得到值, map.put()相当于request.setAttribute方法。
写例子时发现,key值包括 - . 时会有问题.
View 可以返回pdf excel等,暂时没详细了解。
String 指定返回的视图页面名称,结合设置的返回地址路径加上页面名称后缀即可访问到。
注意:如果方法声明了注解@ResponseBody ,则会直接将返回值输出到页面。
Class :
@RequestMapping(value = "/something", method = RequestMethod.GET)
@ResponseBody
public String helloWorld() {
return"Hello World";
}
上面的结果会将文本"Hello World "直接写到http响应流。
Class :
@RequestMapping("/welcome")
public String welcomeHandler() {
return"center";
}
对应的逻辑视图名为“center”,URL= prefix前缀+视图名称 +suffix后缀组成。
void 如果返回值为空,则响应的视图页面对应为访问地址
@RequestMapping("/welcome")
public void welcomeHandler() {}
此例对应的逻辑视图名为"welcome"。
小结:
1.使用 String 作为请求处理方法的返回值类型是比较通用的方法,这样返回的逻辑视图名不会和请求 URL 绑定,具有很大的灵活性,而模型数据又可以通过 ModelMap 控制。
2.使用void,map,Model 时,返回对应的逻辑视图名称真实url为:prefix前缀+视图名称 +suffix后缀组成。
3.使用String,ModelAndView返回视图名称可以不受请求的url绑定,ModelAndView可以设置返回的视图名称。
Model model,HttpServletRequest request, ModelMap map声明变量
request.getSession().setAttribute("test", "haiwei2Session");
request.setAttribute("test", "haiwei1request");
map.addAttribute("test", "haiweiModelMap");
model.addAttribute("test", "haiweiModel");
我通过${test}这个方式取值,优先取Model和ModelMap的,Model和ModelMap是同一个东西,谁最后赋值的就取谁的,然后是request,最后是从session中获取。
啦啦啦
spring学习之springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序的更多相关文章
- springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序
springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序 http://www.360doc.com/content/14/03 ...
- SpringMVC返回Json,自定义Json中Date类型格式
http://www.cnblogs.com/jsczljh/p/3654636.html —————————————————————————————————————————————————————— ...
- Spring中Model,ModelMap以及ModelAndView之间的区别
原文链接:http://blog.csdn.net/zhangxing52077/article/details/75193948 Spring中Model,ModelMap以及ModelAndVie ...
- 关于readdir返回值中struct dirent.d_type的取值有关问题(转)
关于readdir返回值中struct dirent.d_type的取值问题 原网页链接 http://www.gnu.org/software/libc/manual/html_node/Direc ...
- Spring MVC之Action返回类型
Spring MVC支持的方法返回类型 1)ModelAndView 对象.包含Model和View对象,可以通过它访问@ModelAttribute注解的对象. 2)Model 对象.仅包含数据访问 ...
- SpringMVC返回类型
7.SpringMVC的返回值类型和参数传递 1.SpringMVC的返回值类型 (1)ModelAndView返回值类型: 1.1当返回为null时,页面不跳转. 1.2当返回值没有指定视图名时,默 ...
- 【C】二级指针探秘 & 星号的两种用法(1.与基本类型结合形成另一种类型,比如与int结合形成int* 2.取值操作)
1)问题:二级指针到底是什么?怎么用的?怎么存放的? #include <stdio.h> #define TEST_ADDR 0x12FF40 void main() { int a = ...
- table里面,怎么根据checkbox选择的一行中的某个单元格的值是否为空,来判断是否该选中
<table class="stripe" id="tab2"> <tr> <th>选择</th> <th ...
- springmvc的ModelMap,前台取值
利用 ${user.id}或者‘${user.id}’都是可以直接获取到的,不过前提是在jsp页面的script脚本中,而在引用的js文件中是不可以使用的,因为${}是jsp的el标签. 利用 ${u ...
随机推荐
- iphone微信 h5页音乐自动播放
iphone微信 h5页音乐自动播放: // iphone自动播放 document.addEventListener("WeixinJSBridgeReady", functio ...
- 微信小程序——wxParse使用方法
wxParse是一个微信小程序富文本解析组件.现在小程序里面自带了一个<rich-text>组件也能解析富文本,但是表现不尽人意.所以我还是采用的wxParse来解析富文本的. wxPar ...
- sparkR集群启动脚本的封装。
sparkR默认是以单机方式运行的.实现sparkR的集群启动,需要指定master地址,占用内存.CPU,及UI端口等,这对分析人员来说是比较麻烦的. 如何实现对其启动的封装,方便分析人员来使用: ...
- JS复制内容到剪贴板(兼容FF/Chrome/Safari所有浏览器)
现在浏览器种类也越来越多,诸如 IE.Firefox.Chrome.Safari等等,因此现在要实现一个js复制内容到剪贴板的小功能就不是一件那么容易的事了. 在FLASH 9 时代,有一个通杀所有浏 ...
- 几款主流 NoSql 数据库的对比(转)
转自:http://www.cnblogs.com/vajoy/p/5471308.html 最近小组准备启动一个 node 开源项目,从前端亲和力.大数据下的IO性能.可扩展性几点入手挑选了 NoS ...
- 路由策略和策略路由 & route-map
今天,这个专题应用下route-map,在这个之前,有很多内容需要掌握,不是简单的制定一个路由图就可以了. -------- 本次专题理论的东西居多,但是不是复制黏贴,是加上自己的理解思想. 第一个要 ...
- Ubuntu之网易云音乐无法启动
官方最新版(1.1)有这个问题,似乎改成0.9版就可以了 deepin15(32位):http://s1.music.126.net/download/pc/netease-cloud-music_0 ...
- 传统餐饮O2O支付体系成难题
传统餐饮O2O支付体系成难题 网的数据化参考,使得门店运营更具科学性. 作为“易淘食”这样的订餐网站,主要依靠与餐厅分成 来获得利润.“根据每个餐厅合作情况不同,每一笔订单我们可获得5%-15%的佣金 ...
- e740. 向标签中加入一个图标
This example creates a JLabel component with an icon. // Fetch icon Icon icon = new ImageIcon(" ...
- 采用MiniProfiler监控EF与.NET MVC项目
今天来说说EF与MVC项目的性能检测和监控 首先,先介绍一下今天我们使用的工具吧. MiniProfiler~ 这个东西的介绍如下: MVC MiniProfiler是Stack Overflow团队 ...