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 ...
随机推荐
- 小米路由器刷Xiaomi Mi WiFi Mini openwrt
Current Stable Release - OpenWrt 18.06.1,released on August, 18th 2018. there is also PandoraBox fir ...
- 你必须要懂的APK瘦身知识
随着业务复杂度的逐渐增加,代码.资源也在不断的增加,此时你的APP大小也在增加.从用户层面来说,面对动辄几十兆的APP来说在非WIFI情况下还是会犹豫要不要下载,不下载你就可能因此失去了一个用户.从公 ...
- Linux共享库 配置文件读取
#ifndef __INIPARSERHELPER_H_ #define __INIPARSERHELPER_H_ #define IN #define OUT #define INOUT typed ...
- github开源库(一)
http://www.open-open.com/lib/view/open1388317199516.html 1.ActionBarSherlock ActionBarSherlock应该算得上是 ...
- Numpy 的通用函数:快速的元素级数组函数
通用函数: 通用函数(ufunc)是一种对ndarray中的数据执行元素级运算的函数.你可以将其看作简单函数(接受一个或多个标量值,并产生一个或度过标量值)的矢量化包装器. 简单的元素级变体,如sqr ...
- 利用函数或映射进行数据转换 (map)
先来看个数据 df = DataFrame({"food":["bacon", "pulled pork", "bacon&quo ...
- VIM中一些按键的作用:
表示范围的命令: l: 表示一个字符,可以于操作符组合使用: aw :表示一个单词: 不会count 空格: 前面可以加数字的: iw: 选中一些单词, 会包括空格: 前面可以加数字: ap: 表 ...
- e821. 设置JScrollPane滚动栏
A scroll bar in a scroll pane can be set to appear only as needed, always appear, or never appear. B ...
- logbook日志系统
python中替代logging的日志系统. 不过比之前的logging难理解. 先上打印到屏幕上的代码和存到日志文件中的代码: #!/usr/bin/env python3 # -*- coding ...
- mac命令行启动tomcat
一.修改授权 进入tomcat的bin目录,修改授权 ➜ bin pwd /Users/yp/Documents/workspace/apache-tomcat-7.0.68/bin ➜ bin su ...