SpringMVC经典系列-08讲解SpringMVC的ModelAndVIew类---【LinusZhu】
来源:互联网 发布:淘宝有多少店铺 编辑:程序博客网 时间:2024/04/30 14:02
注意:此文章是个人原创,希望有转载需要的朋友们标明文章出处,如果各位朋友们觉得写的还好,就给个赞哈,你的鼓励是我创作的最大动力,LinusZhu在此表示十分感谢,当然文章中如有纰漏,请联系linuszhu@163.com,敬请朋友们斧正,谢谢。
ModelAndView是模型视图类,从名字上我们可以知道ModelAndView中的Model代表模型,View代表视图,这个类把要显示的数据存储到了Model属性中,要跳转的视图信息存储到了view属性,其作用域同request。
ModelAndView测试代码如下:
package com.spring.controller;
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.spring.po.User;
@Controller
@RequestMapping("/user.do")
public class UserController extends MultiActionController {
@RequestMapping(params="method=reg")
public ModelAndView reg(String uname){
ModelAndView mv = new ModelAndView();
mv.setViewName("index");
User u = new User();
u.setUname("LinusZhu");
mv.addObject("user", u);
return mv;
}
}
页面展示如下:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<h1>${requestScope.user.uname}</h1>
</body>
</html>
ModelAndView的源码如下,有兴趣的朋友们可以看一下:
public class ModelAndView {
/** View instance or view name String */
private Object view;//属性view
/** Model Map */
private ModelMap model;//属性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 <code>addObject</code>.
* @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 <code>addObject</code>.
* @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 <code>null</code>, but the
* model Map may be <code>null</code> if there is no model data.
*/
public ModelAndView(String viewName, Map<String, ?> model) {
this.view = viewName;
if (model != null) {
getModelMap().addAllAttributes(model);
}
}
/**
* Creates new ModelAndView given a View object and a model.
* <emphasis>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</emphasis>
* @param view View object to render
* @param model Map of model names (Strings) to model objects
* (Objects). Model entries may not be <code>null</code>, but the
* model Map may be <code>null</code> if there is no model data.
*/
public ModelAndView(View view, Map<String, ?> 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 <code>null</code> 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 <code>null</code> 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 <code>ModelAndView</code> 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. <code>true</code>
* 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 <code>null</code>.
* Called by DispatcherServlet for evaluation of the model.
*/
protected Map<String, Object> getModelInternal() {
return this.model;
}
/**
* Return the underlying <code>ModelMap</code> instance (never <code>null</code>).
*/
public ModelMap getModelMap() {
if (this.model == null) {
this.model = new ModelMap();
}
return this.model;
}
/**
* Return the model map. Never returns <code>null</code>.
* To be called by application code for modifying the model.
*/
public Map<String, Object> 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 <code>null</code>)
* @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 <code>null</code>)
* @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<String, ?> modelMap) {
getModelMap().addAllAttributes(modelMap);
return this;
}
/**
* Clear the state of this ModelAndView object.
* The object will be empty afterwards.
* <p>Can be used to suppress rendering of a given ModelAndView object
* in the <code>postHandle</code> 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.
* <p>Returns <code>false</code> if any additional state was added to the instance
* <strong>after</strong> 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();
}
}
- SpringMVC经典系列-08讲解SpringMVC的ModelAndVIew类---【LinusZhu】
- SpringMVC经典系列-07讲解SpringMVC的ModelMap类---【LinusZhu】
- SpringMVC经典系列-06讲解SpringMVC核心注解---【LinusZhu】
- SpringMVC经典系列-12基于SpringMVC的文件上传---【LinusZhu】
- SpringMVC经典系列-14自定义SpringMVC的拦截器---【LinusZhu】
- SpringMVC经典系列-15对SpringMVC的总结---【LinusZhu】
- SpringMVC经典系列-01简介SpringMVC---【LinusZhu】
- SpringMVC经典系列-13使用SpringMVC处理Ajax请求---【LinusZhu】
- SpringMVC经典系列-09对SpringMVC的Controller层返回值的处理---【LinusZhu】
- SpringMVC经典系列-05深入了解SpringMVC的核心原理---【LinusZhu】
- SpringMVC经典系列-11简单获取request、session对象---【LinusZhu】
- SpringMVC经典系列-02基于Spring2.5注解的方式进行项目开发---【LinusZhu】
- SpringMVC经典系列-03基于Spring2.5采用XMl配置的方式进行项目开发---【LinusZhu】
- SpringMVC经典系列-04基于Spring3.1注解的方式进行项目开发---【LinusZhu】
- SpringMVC经典系列-10详解SpringMVC请求转发和重定向---【LinusZhu】
- springmvc ModelAndView类的使用
- springMVC 的modelandView
- springMVC 的 ModelAndView 用法
- 《修改代码的艺术》迷你书
- Android4.4 状态栏信号和数据业务图片说明
- Sie werden komplett in Echtzeit zu
- JS随录-3
- 在mac os 10.9.3上安装xgraph 12.2遇到的问题及解决办法
- SpringMVC经典系列-08讲解SpringMVC的ModelAndVIew类---【LinusZhu】
- hdu 3172 Virtual Friends (并查集 + 字典树)
- linux下串口的阻塞和非阻塞操作
- cocos2d-x 内存回收研究
- Qt4.7在ARM开发板上如何设置中文显示
- ZeroMQ各模式总结
- css 优化
- 真正的TextView跑马灯
- 无闪烁刷屏技术的实现