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:

时间: 2024-07-29 23:27:48

spring mvc处理方法返回方式的相关文章

Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法

Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法 在Action中方法的返回值都是字符串行,一般情况是返回某个JSP,如: return "xx":意思是返回到某个JSP页面上 如果想在此Action中跳转到另一个Action中怎样做呢? return "redirect://.do?" 例如: @RequestMapping(params = "action=delete") public String del

Spring MVC--------处理方法返回值的可选类型

对于Spring MVC处理方法支持支持一系列的返回方式:  (1)ModelAndView (2)Model (3)ModelMap (4)Map (5)View (6)String (7)Void (8)Object 一,ModelAndView @RequestMapping("/threadRequest*") public ModelAndView threadRequest(){ ModelAndView mv=new ModelAndView(); mv.setViewN

spring mvc 控制器方法传数组对象的一些经验

因为项目需要在一个表单里面提交多个对象,比较好的做法就是直接在控制器方法参数里传一个数组. 由于Spring mvc框架在反射生成控制方法的参数对象的时候会调用这个类的getDeclaredConstructor方法来获得构造函数, 但是一直报NoSuchMethodException的异常. 根据这个方法的jdk文档,这个类是一个数组对象时,这个方法会抛出java.lang.NoSuchMethodException,因为接口.数组类.void.基本类型没有构造函数. 同事后来给我支了两招,使

判断JSON是否为空 (用spring mvc @ResponseBody 自动返回的json串 )

判断JSON是否为空 (用spring mvc @ResponseBody 自动返回的json串 ) 知识分类:EXTJS  spring mvc json 记录时间: 20150708 简单描述:用json.length 属性长度来判断是否为空,在此过程中spring mvc 自动返回的json串是 字符串的类型,所以用.length的方式返回的则是字符串的长度(一般空的JSON 在此处返回的长度应为3),并不是数组的长度,在EXTJS中用Ext.decode(json); 则可以将字符串js

spring mvc 异常统一处理方式

springMVC提供的异常处理主要有两种方式,一种是直接实现自己的HandlerExceptionResolver,另一种是使用注解的方式实现一个专门用于处理异常的Controller--ExceptionHandler. 1.实现自己的HandlerExceptionResolver,HandlerExceptionResolver是一个接 口,springMVC本身已经对其有了一个自身的实现--DefaultExceptionResolver,该解析器只是对其中的一些比较典型的异常 进行了

Spring MVC灵活控制返回json的值(自定义过滤字段)

在使用spring MVC开发过程中,为了提高项目执行效率,所以在一些外键字段的实体中会注解"@ManyToOne(fetch = FetchType.LAZY)"以实现延迟加载的效果. 但是,在使用ajax请求数据,当需要返回的序列化数据中包含延迟加载的属性时,会出现错误,延迟加载的属性无法进行序列化.在这个时候,我们需要暂时取消延迟加载,以取到所有需要的数据. 可是,这样又会造成一堆垃圾数据的产生.序列化.传递至前台. 所以需要在控制层进行数据过滤,只序列化需要的数据. 具体过滤方

Spring MVC 的 XML 配置方式

索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: solution/pom.xml solution/webapi/pom.xml solution/mapper/pom.xml web.xml spring-mvc.xml spring-context.xml dbconfig.properties log4j.properties RESTfulController.java 一.要点讲解 1.引入构建 spring .spring mvc 的框架类库 s

Spring MVC拦截器+注解方式实现防止表单重复提交

原理:在新建页面中Session保存token随机码,当保存时验证,通过后删除,当再次点击保存时由于服务器端的Session中已经不存在了,所有无法验证通过. 注,如果是集群的方式,则需要将token放入到缓存中即可. 注解Token代码:java源码  Java代码 复制代码 收藏代码 1[email protected](ElementType.METHOD) 2[email protected] (RetentionPolicy.RUNTIME) 3.public @interface T

spring mvc 控制器方法传递一些经验对象的数组

由于该项目必须提交一个表单,其中多个对象,更好的方法是直接通过在控制器方法参数的数组. 因为Spring mvc框架在反射生成控制方法的參数对象的时候会调用这个类的getDeclaredConstructor方法来获得构造函数, 可是一直报NoSuchMethodException的异常. 依据这种方法的jdk文档,这个类是一个数组对象时,这种方法会抛出java.lang.NoSuchMethodException,由于接口.数组类.void.基本类型没有构造函数. 同事后来给我支了两招,使用A