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中获取。

啦啦啦

原文地址:https://www.cnblogs.com/ClassNotFoundException/p/6700844.html