spring mvc处理方法返回方式
Model:
package org.springframework.ui; import java.util.Collection;
import java.util.Map; public interface Model { Model addAttribute(String attributeName, Object attributeValue); Model addAttribute(Object attributeValue); Model addAllAttributes(Collection<?> attributeValues); Model addAllAttributes(Map<String, ?> attributes); Model mergeAttributes(Map<String, ?> attributes); boolean containsAttribute(String attributeName); Map<String, Object> asMap(); }
Map:
package java.util;
public interface Map<K,V> {
int size();
boolean isEmpty();
boolean containsKey(Object key);
boolean containsValue(Object value);
V get(Object key);
V put(K key, V value);
V remove(Object key);
void putAll(Map<? extends K, ? extends V> m);
void clear();
Set<K> keySet();
Collection<V> values();
Set<Map.Entry<K, V>> entrySet();
interface Entry<K,V> {
K getKey();
V getValue();
V setValue(V value);
boolean equals(Object o);
int hashCode();
}
// Comparison and hashing
boolean equals(Object o);
int hashCode();
}
ModelMap:
package org.springframework.ui; import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map; import org.springframework.core.Conventions;
import org.springframework.util.Assert; @SuppressWarnings("serial")
public class ModelMap extends LinkedHashMap<String, Object> { public ModelMap() {
} public ModelMap(String attributeName, Object attributeValue) {
addAttribute(attributeName, attributeValue);
} public ModelMap(Object attributeValue) {
addAttribute(attributeValue);
} public ModelMap addAttribute(String attributeName, Object attributeValue) {
Assert.notNull(attributeName, "Model attribute name must not be null");
put(attributeName, attributeValue);
return this;
} public ModelMap addAttribute(Object attributeValue) {
Assert.notNull(attributeValue, "Model object must not be null");
if (attributeValue instanceof Collection && ((Collection<?>) attributeValue).isEmpty()) {
return this;
}
return addAttribute(Conventions.getVariableName(attributeValue), attributeValue);
} public ModelMap addAllAttributes(Collection<?> attributeValues) {
if (attributeValues != null) {
for (Object attributeValue : attributeValues) {
addAttribute(attributeValue);
}
}
return this;
} public ModelMap addAllAttributes(Map<String, ?> attributes) {
if (attributes != null) {
putAll(attributes);
}
return this;
} public ModelMap mergeAttributes(Map<String, ?> attributes) {
if (attributes != null) {
for (Map.Entry<String, ?> entry : attributes.entrySet()) {
String key = entry.getKey();
if (!containsKey(key)) {
put(key, entry.getValue());
}
}
}
return this;
} public boolean containsAttribute(String attributeName) {
return containsKey(attributeName);
} }
View:
package org.springframework.web.servlet; import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.http.MediaType; public interface View { String RESPONSE_STATUS_ATTRIBUTE = View.class.getName() + ".responseStatus"; String PATH_VARIABLES = View.class.getName() + ".pathVariables"; String SELECTED_CONTENT_TYPE = View.class.getName() + ".selectedContentType"; String getContentType(); void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception; }
ModelAndView:
package org.springframework.web.servlet; import java.util.Map; import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils; public class ModelAndView { private Object view; private ModelMap model; private boolean cleared = false; public ModelAndView() {
} public ModelAndView(String viewName) {
this.view = viewName;
} public ModelAndView(View view) {
this.view = view;
} public ModelAndView(String viewName, Map<String, ?> model) {
this.view = viewName;
if (model != null) {
getModelMap().addAllAttributes(model);
}
} public ModelAndView(View view, Map<String, ?> model) {
this.view = view;
if (model != null) {
getModelMap().addAllAttributes(model);
}
} public ModelAndView(String viewName, String modelName, Object modelObject) {
this.view = viewName;
addObject(modelName, modelObject);
} public ModelAndView(View view, String modelName, Object modelObject) {
this.view = view;
addObject(modelName, modelObject);
} public void setViewName(String viewName) {
this.view = viewName;
} public String getViewName() {
return (this.view instanceof String ? (String) this.view : null);
} public void setView(View view) {
this.view = view;
} public View getView() {
return (this.view instanceof View ? (View) this.view : null);
} public boolean hasView() {
return (this.view != null);
} public boolean isReference() {
return (this.view instanceof String);
} protected Map<String, Object> getModelInternal() {
return this.model;
} public ModelMap getModelMap() {
if (this.model == null) {
this.model = new ModelMap();
}
return this.model;
} public Map<String, Object> getModel() {
return getModelMap();
} public ModelAndView addObject(String attributeName, Object attributeValue) {
getModelMap().addAttribute(attributeName, attributeValue);
return this;
} public ModelAndView addObject(Object attributeValue) {
getModelMap().addAttribute(attributeValue);
return this;
} public ModelAndView addAllObjects(Map<String, ?> modelMap) {
getModelMap().addAllAttributes(modelMap);
return this;
} public void clear() {
this.view = null;
this.model = null;
this.cleared = true;
} public boolean isEmpty() {
return (this.view == null && CollectionUtils.isEmpty(this.model));
} public boolean wasCleared() {
return (this.cleared && isEmpty());
} @Override
public String toString() {
StringBuilder sb = new StringBuilder("ModelAndView: ");
if (isReference()) {
sb.append("reference to view with name '").append(this.view).append("'");
}
else {
sb.append("materialized View is [").append(this.view).append(']');
}
sb.append("; model is ").append(this.model);
return sb.toString();
} }
String:
void:
spring mvc处理方法返回方式的更多相关文章
- Spring MVC三种返回方式
spring mvc处理方法支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void. 下面一一进行说明: 1.ModelAndV ...
- Spring MVC的方法返回值和参数传递
1. SpringMVC方法的返回值类型 3.1String类作为返回值 3.1.1Controller层 /** * 返回值类型为String时,一般用于返回视图名称 * 1.当方法返回值为null ...
- Spring MVC同一方法返回JSON/XML格式
最近一道面试题,要求同一API接口支持不同格式返回值.一开始是设想通过过滤器(Filter)设置返回值,但是并不可行,因为方法返回值一般都是类型需要做转换,而过滤器则是前置的.另一方面可以通过拦截器的 ...
- Spring MVC 3.0 返回JSON数据的方法
Spring MVC 3.0 返回JSON数据的方法1. 直接 PrintWriter 输出2. 使用 JSP 视图3. 使用Spring内置的支持// Spring MVC 配置<bean c ...
- Spring MVC 前后端 Json 方式交互和处理
众所周知,在mvc中,数据是在各个层次之间进行流转是一个不争的事实. 而这种流转,也就会面临一些困境,这些困境,是由于数据在不同世界中的表现形式不同而造成的. 数据在页面上是一个扁平的,不带数据类 ...
- Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法
Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法 在Action中方法的返回值都是字符串行,一般情况是返回某个JSP,如: return "xx" ...
- SpringBoot系列: Spring MVC视图方法的补充
SpringMVC 视图方法的参数, 已经在这个文章中写得非常清楚了, 链接为 https://www.cnblogs.com/morethink/p/8028664.html 这篇文章做一些补充. ...
- Spring MVC--------处理方法返回值的可选类型
对于Spring MVC处理方法支持支持一系列的返回方式: (1)ModelAndView (2)Model (3)ModelMap (4)Map (5)View (6)String (7)Void ...
- SpringMVC(八):使用Servlet原生API作为Spring MVC hanlder方法的参数
在SpringMVC开发中,是有场景需要在Handler方法中直接使用ServletAPI. 在Spring MVC Handler的方法中都支持哪些Servlet API作为参数呢? --Respo ...
随机推荐
- SpringBoot 集成 Mybatis 使用 Druid数据源 MySQL数据库
思路: 1.创建项目 项目结构如下: 2.导入相应包 POM.xml文件如下: <?xml version="1.0" encoding="UTF-8"? ...
- PCL滤波介绍(1)
在获取点云数据时 ,由于设备精度,操作者经验环境因素带来的影响,以及电磁波的衍射特性,被测物体表面性质变化和数据拼接配准操作过程的影响,点云数据中讲不可避免的出现一些噪声.在点云处理流程中滤波处理作为 ...
- jquery操作select取值赋值与设置选中[转]
本节内容:jquery实现select下拉框的取值与赋值,设置选中的方法大全. 比如<select class="selector"></select> 1 ...
- 【转】C#解析Json Newtonsoft.Json
Newtonsoft.Json源码 Newtonsoft.Json介绍 在做开发的时候,很多数据交换都是以json格式传输的.而使用Json的时候,我们很多时候会涉及到几个序列化对象的使用:DataC ...
- struts+ajax+jquery:实现异步新增数据
很久未有更新,最近因为团队其它事耽误没有继续学习,但心中十分忐忑不安,抽空把自己薄弱的点拿来再巩固一下! 本身异步刷新用处非常多,SSH框架对我来讲,已无难度,但结合ajax处理一些增删查改分页等,就 ...
- Spring JDBC对象批量操作
以下示例将演示如何使用spring jdbc中的对象进行批量更新.我们将在单次批次操作中更新student表中的记录. student表的结果如下 - CREATE TABLE student( id ...
- Linux下查看某个进程的线程数量(转)
转自:https://www.cnblogs.com/caosiyang/archive/2012/10/15/2724585.html 有些时候需要确实进程内部当前运行着多少线程,那么以下几个方法值 ...
- tpshop全局公共方法
TPshop 全局公告函数库 前后台可用 这里只列出有哪些方法, 简单描述, 具体的函数体查看 Application\Common\Common\function.php 文件 <?php ...
- ASM - ByteCode插件下载、安装以及相关遇到的问题
http://asm.ow2.org/eclipse/index.html http://download.forge.objectweb.org/eclipse-update/ http://for ...
- Linux下设置SSH Server设置时间链接限制
OpenSSH基于安全的理由,如果用户连线到SSH Server后闲置一段时间,SSH Server会在超过特定时间后自动终止SSH连线.本人习惯长时间连接,需要做如下修改: 1.打开ssh配置文件: ...