spring mvc 3.0 ModelAndView模型视图类
见名知意,从名字上我们可以知道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 <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();
- }
- }
测试代码:
- 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" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- </head>
- <body>
- <h1>${requestScope.a}</h1>
- <h1>${requestScope.user.uname}</h1>
- </body>
- </html>
spring mvc 3.0 ModelAndView模型视图类的更多相关文章
- Spring MVC 3.0 深入及对注解的详细讲解
核心原理 1. 用户发送请求给服务器.url:user.do 2. 服务器收到请求.发现Dispatchservlet可以处理.于是调用DispatchServlet. 3. ...
- Spring MVC 3.0 深入及对注解的详细讲解[转载]
http://blog.csdn.net/jzhf2012/article/details/8463783 核心原理 1. 用户发送请求给服务器.url:user.do 2. ...
- 视图框架:Spring MVC 4.0(2)
在<springMVC4(7)模型视图方法源码综合分析>一文中,我们介绍了ModelAndView的用法,它会在控制层方法调用完毕后作为返回值返回,里面封装好了我们的业务逻辑数据和视图对象 ...
- Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(四)
这一章大象将详细分析web层代码,以及使用Spring MVC的注解及其用法和其它相关知识来实现控制器功能. 之前在使用Struts2实现MVC的注解时,是借助struts2-conventi ...
- spring MVC之构造ModelAndView对象
spring MVC之构造ModelAndView对象 ---------- 构造ModelAndView对象 当控制器处理完请求时,通常会将包含视图名称或视图对象以及一些模型属性的ModelAndV ...
- 2017.3.31 spring mvc教程(一)核心类与接口
学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...
- Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(二)
在上一篇文章中我详细的介绍了如何搭建maven环境以及生成一个maven骨架的web项目,那么这章中我将讲述Spring MVC的流程结构,Spring MVC与Struts2的区别,以及例子中的一些 ...
- Spring MVC 3.0 返回JSON数据的方法
Spring MVC 3.0 返回JSON数据的方法1. 直接 PrintWriter 输出2. 使用 JSP 视图3. 使用Spring内置的支持// Spring MVC 配置<bean c ...
- spring mvc 3.0 实现文件上传功能
http://club.jledu.gov.cn/?uid-5282-action-viewspace-itemid-188672 —————————————————————————————————— ...
随机推荐
- 爬虫技术之——bloom filter(含java代码)
在爬虫系统中,在内存中维护着两个关于URL的队列,ToDo队列和Visited队列,ToDo队列存放的是爬虫从已经爬取的网页中解析出来的即将爬取的URL,但是网页是互联的,很可能解析出来的URL是已经 ...
- bzoj 1492 [NOI2007]货币兑换Cash(斜率dp+cdq分治)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1492 [题意] 有AB两种货币,每天可以可以付IPi元,买到A券和B券,且A:B= ...
- signal()函数
转自:http://blog.csdn.net/sddzycnqjn/article/details/7285760 1. 信号概念 信号是进程在运行过程中,由自身产生或由进程外部发过来的消息(事件) ...
- .NET中的Newtonsoft.Json.JsonConvert.SerializeObject(string a)
1.將string a 序列化為Json格式: 2.使用條件:將Newtonsoft.Json.dll作為引用添加到項目中.下载地址在这:http://json.codeplex.com/
- 桶排序-Node.js
, , , , ]; var a = [], i; ; i < b.length; i++) { var num = b[i]; a[num] = a[num]||; a[num] ++; nu ...
- Strider安装(Ubuntu)
安装: git clone https://github.com/Strider-CD/strider.git && cd strider nam install 然而还是出现一大波错 ...
- dom 拖拽div
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- DD_belatedPNG,解决 IE6 不支持 PNG-24 绝佳解决方案
png24在ie下支持透明.终于找到下面的可行办法: 我们知道 IE6 是不支持透明的 PNG-24 的,这无疑限制了网页设计的发挥空间. 然而整个互联网上解决这个 IE6 的透明 PNG-24 的方 ...
- UDP广域网,局域网通信-原理分析,穿透技术
一.UDP局域网通信. 这个比较简单,关于局域网中的2台或者更多的计算机之间的UDP通信,网络上一大把,直接复制粘贴就可以使用,原理也非常简单.所以,本文不做详细介绍. 二.UDP广域通信(包括路由器 ...
- Spark RDD概念学习系列之RDD是什么?(四)
RDD是什么? 通俗地理解,RDD可以被抽象地理解为一个大的数组(Array),但是这个数组是分布在集群上的.详细见 Spark的数据存储 Spark的核心数据模型是RDD,但RDD是个抽象类 ...