博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring mvc 3.0 ModelAndView模型视图类
阅读量:6320 次
发布时间:2019-06-22

本文共 9030 字,大约阅读时间需要 30 分钟。

见名知意,从名字上我们可以知道ModelAndView中的Model代表模型,View代表视图。即,这个类把要显示的数据存储到了Model属性中,要跳转的视图信息存储到了view属性。我们看一下ModelAndView的部分源码,即可知其中关系:

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; /** * Default constructor for bean-style usage: populating bean * properties instead of passing in constructor arguments. * @see #setView(View) * @see #setViewName(String) */ public ModelAndView() { } /** * Convenient constructor when there is no model data to expose. * Can also be used in conjunction with addObject. * @param viewName name of the View to render, to be resolved * by the DispatcherServlet's ViewResolver * @see #addObject */ public ModelAndView(String viewName) { this.view = viewName; } /** * Convenient constructor when there is no model data to expose. * Can also be used in conjunction with addObject. * @param view View object to render * @see #addObject */ public ModelAndView(View view) { this.view = view; } /** * Creates new ModelAndView given a view name and a model. * @param viewName name of the View to render, to be resolved * by the DispatcherServlet's ViewResolver * @param model Map of model names (Strings) to model objects * (Objects). Model entries may not be null, but the * model Map may be null if there is no model data. */ public ModelAndView(String viewName, Map
model) { this.view = viewName; if (model != null) { getModelMap().addAllAttributes(model); } } /** * Creates new ModelAndView given a View object and a model. *
Note: the supplied model data is copied into the internal * storage of this class. You should not consider to modify the supplied * Map after supplying it to this class
* @param view View object to render * @param model Map of model names (Strings) to model objects * (Objects). Model entries may not be
null, but the * model Map may be
null if there is no model data. */ public ModelAndView(View view, Map
model) { this.view = view; if (model != null) { getModelMap().addAllAttributes(model); } } /** * Convenient constructor to take a single model object. * @param viewName name of the View to render, to be resolved * by the DispatcherServlet's ViewResolver * @param modelName name of the single entry in the model * @param modelObject the single model object */ public ModelAndView(String viewName, String modelName, Object modelObject) { this.view = viewName; addObject(modelName, modelObject); } /** * Convenient constructor to take a single model object. * @param view View object to render * @param modelName name of the single entry in the model * @param modelObject the single model object */ public ModelAndView(View view, String modelName, Object modelObject) { this.view = view; addObject(modelName, modelObject); } /** * Set a view name for this ModelAndView, to be resolved by the * DispatcherServlet via a ViewResolver. Will override any * pre-existing view name or View. */ public void setViewName(String viewName) { this.view = viewName; } /** * Return the view name to be resolved by the DispatcherServlet * via a ViewResolver, or
null if we are using a View object. */ public String getViewName() { return (this.view instanceof String ? (String) this.view : null); } /** * Set a View object for this ModelAndView. Will override any * pre-existing view name or View. */ public void setView(View view) { this.view = view; } /** * Return the View object, or
null if we are using a view name * to be resolved by the DispatcherServlet via a ViewResolver. */ public View getView() { return (this.view instanceof View ? (View) this.view : null); } /** * Indicate whether or not this
ModelAndView has a view, either * as a view name or as a direct {
@link View} instance. */ public boolean hasView() { return (this.view != null); } /** * Return whether we use a view reference, i.e.
true * if the view has been specified via a name to be resolved by the * DispatcherServlet via a ViewResolver. */ public boolean isReference() { return (this.view instanceof String); } /** * Return the model map. May return
null. * Called by DispatcherServlet for evaluation of the model. */ protected Map
getModelInternal() { return this.model; } /** * Return the underlying
ModelMap instance (never
null). */ public ModelMap getModelMap() { if (this.model == null) { this.model = new ModelMap(); } return this.model; } /** * Return the model map. Never returns
null. * To be called by application code for modifying the model. */ public Map
getModel() { return getModelMap(); } /** * Add an attribute to the model. * @param attributeName name of the object to add to the model * @param attributeValue object to add to the model (never
null) * @see ModelMap#addAttribute(String, Object) * @see #getModelMap() */ public ModelAndView addObject(String attributeName, Object attributeValue) { getModelMap().addAttribute(attributeName, attributeValue); return this; } /** * Add an attribute to the model using parameter name generation. * @param attributeValue the object to add to the model (never
null) * @see ModelMap#addAttribute(Object) * @see #getModelMap() */ public ModelAndView addObject(Object attributeValue) { getModelMap().addAttribute(attributeValue); return this; } /** * Add all attributes contained in the provided Map to the model. * @param modelMap a Map of attributeName -> attributeValue pairs * @see ModelMap#addAllAttributes(Map) * @see #getModelMap() */ public ModelAndView addAllObjects(Map
modelMap) { getModelMap().addAllAttributes(modelMap); return this; } /** * Clear the state of this ModelAndView object. * The object will be empty afterwards. *

Can be used to suppress rendering of a given ModelAndView object * in the postHandle method of a HandlerInterceptor. * @see #isEmpty() * @see HandlerInterceptor#postHandle */ public void clear() { this.view = null; this.model = null; this.cleared = true; } /** * Return whether this ModelAndView object is empty, * i.e. whether it does not hold any view and does not contain a model. */ public boolean isEmpty() { return (this.view == null && CollectionUtils.isEmpty(this.model)); } /** * Return whether this ModelAndView object is empty as a result of a call to { @link #clear} * i.e. whether it does not hold any view and does not contain a model. *

Returns false if any additional state was added to the instance * after the call to { @link #clear}. * @see #clear() */ public boolean wasCleared() { return (this.cleared && isEmpty()); } /** * Return diagnostic information about this model and view. */ @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(); }}

测试代码:

package com.sxt.web;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.multiaction.MultiActionController;import com.sxt.po.User;@Controller@RequestMapping(value = "user")public class UserController extends MultiActionController  {        @RequestMapping(value = "/reg")    public ModelAndView reg(@RequestParam("uname") String uname){        ModelAndView mv = new ModelAndView();        mv.setViewName("index");//        mv.setView(new RedirectView("index"));                User u = new User();        u.setUname("高淇");        mv.addObject(u);   //查看源代码,得知,直接放入对象。属性名为”首字母小写的类名”。 一般建议手动增加属性名称。        mv.addObject("a", "aaaa");        return mv;    }}

jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>             

${requestScope.a}

${requestScope.user.uname}

 

转载地址:http://smdaa.baihongyu.com/

你可能感兴趣的文章
加拿大程序员趣闻系列 3/N : 生活篇
查看>>
Git工程实践(一)巧用commit message
查看>>
iOS中如何使用多个Target去管理你的项目环境版本(测试环境与线上环境)
查看>>
Java程序员必看:技术大牛都在用这四个小技巧
查看>>
九、Touchable组件点击事件
查看>>
ArcBlock 合作 | ABT 加入美国数码产业商会
查看>>
Android系统全貌
查看>>
爱因斯坦的思考题Java语言求解
查看>>
Java虚拟机 —— 垃圾回收机制
查看>>
180612-Spring之Yml配置文件加载问题
查看>>
【译】十五分钟,学习 Webpack
查看>>
scrapy进阶开发(一):scrapy架构源码分析
查看>>
iOS入门常见错误
查看>>
React全家桶构建一款Web音乐App实战(二):字体图标制作及页面路由搭建
查看>>
Deep learning学习记录(课程2)
查看>>
web页面兼容性问题记录
查看>>
如何优雅地使用 macOS
查看>>
【TopRightMenu】一步搞定手机QQ界面右上角弹出菜单
查看>>
说说 Vue.js 中的 v-show 指令
查看>>
vuex 源码:深入 vuex 之 module
查看>>