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)(多模块/单模块)maven打包引入第三方jar方式,使用scope:system配置systemPath编译,不用添加到本地仓库!
- JavaWeb_(SpringMVC框架)测试SpringMVC&Spring&MyBatis三大整合
- [SpringMVC] - 解决 RequestMappingHandlerAdapter 报红的错误
- 挺详细的spring+springmvc+mybatis配置整合|含源代码
- SpringMVC那点事
- 【转】Spring 注解学习手札(超好的springmvc注解教程)
- springmvc的简单介绍以及springmvc组件的介绍
- 手把手教你整合最优雅SSM框架:SpringMVC + Spring + MyBatis
- spring 学习3-Spring AOP
- spring 学习2-Spring Configuration in detail
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- Spring读源码系列之AOP--06---AopProxy===>spring使用jdk和cglib生成代理对象的终极奥义
- dubbo+zookeeper+spring+springMVC+mybatis的使用
- 【SpringMVC笔记06】SpringMVC中常用注解介绍
- springMVC标签
- SpringBoot搭建SpringMVC项目
- 【项目实战】SpringMVC拦截器实战 - 自定义拦截器防止重复提交
- Spring+SpringMVC+Mybatis(开发必备技能)05、task任务配置
- Spring+SpringMVC+Mybatis(开发必备技能)02、完整DML&DQL语句demo示例
- Spring+SpringMVC 配置事务管理无效原因及解决方案。
- Java学习路线-65:整合 SSM-Spring+SpringMVC+MyBatis
- spring+websocket综合(springMVC+spring+MyBatis这是SSM框架和websocket集成技术)
- 005-spring cache-原理、缓存AOP机制、Spring Cache抽象集成机制、springboot自动配置机制
- 009-Spring Boot 事件监听、监听器配置与方式、spring、Spring boot内置事件
- uploadify在火狐下上传不了的解决方式,java版(Spring+SpringMVC+MyBatis)具体解决方式
- Spring+SpringMVC+Mybatis(开发必备技能)01、基础idea环境配置