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 ...
随机推荐
- Map与Url查询参数相互转换
package com.thunisoft.maybee.engine.utils; import org.apache.commons.lang3.StringUtils; import java. ...
- Genymotion 解决虚拟镜像下载速度特别慢的问题[转]
Genymotion下载地址(需注册账号):https://www.genymotion.com/download/ Genymotion号称Android模拟器中运行最快的,但是服务器在国外,And ...
- loadrunner 中Error和failed transaction 的区别
Down:没有运行 Pending:挂起 Init:初始化 Ready:准备就绪 Run:正在运行 Rendezvous:正在集结 Passed:运行通过 Failed:运行失败 Error:出现故障 ...
- OSPF邻居状态机
当OSPF邻居建立的过程之中,路由器在和邻居达到完全邻接关系之前,要经过几个状态.这些状态在OSPF RFC2328有相关的定义,这些状态分别是Down, Attempt, Init, 2-Way, ...
- 如何利用pyCharm编写和运行python文件
在安装python环境后,通常可以利用IDE pyCharm来编译我们的python文件.创建一个python文件夹,用pyCharm打开文件夹,在文件夹中新建一个python文件demo.py 也许 ...
- Cocos2d-x绘制圆角矩形
/* * @brief 画圆角矩形 * @param origin 矩形开始点 * @param destination 矩形结束点 * @param radius 圆角半径 * @param seg ...
- Numpy的线性代数
线性代数的矩阵乘法 线性代数(如矩阵乘法.矩阵分解.行列式以及其他方阵数学等)是任何数组库的重要组成部分.不想某些语言(如MATLAB), 通过*对两个二维数组相乘得到的是一个元素级的积,而不是一个矩 ...
- 对微软Microsoft Dynamics CRM 的认识
MS CRM的认识 技术层面: MS CRM使用了当前最为流行的Web Service作为数据交互的手段,这给我们的二次开发和系统级的集成带来了无可比拟的方便性.易用性.我们不用关心如何去访问CRM数 ...
- e836. 设置JTabbedPane中卡片的提示语
There are two ways to set a tool tip on a tab. The first is to specify it when the tab is created; t ...
- 每天一个linux命令:traceroute命令
通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一 ...