设为首页 加入收藏

TOP

基于springMVC 3.0项目开发实例(五)
2014-11-23 19:17:51 】 浏览:1848
Tags:基于 springMVC 3.0 项目 开发实例
rnull).

*/

public ModelMap getModelMap() {

if (this.model == null) {

this.model =new ModelMap();

}

returnthis.model;

}

/**

* Return the model map. Never returnsnull.

* 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 attributeva lue object to add to the model (nevernull)

* @see ModelMap#addAttribute(String, Object)

* @see #getModelMap()

*/

publicModelAndView addObject(String attributeName, Object attributeva lue) {

getModelMap().addAttribute(attributeName, attributeva lue);

returnthis;

}

/**

* Add an attribute to the model using parameter name generation.

* @param attributeva lue the object to add to the model (nevernull)

* @see ModelMap#addAttribute(Object)

* @see #getModelMap()

*/

publicModelAndView addObject(Object attributeva lue) {

getModelMap().addAttribute(attributeva lue);

returnthis;

}

/**

* Add all attributes contained in the provided Map to the model.

* @param modelMap a Map of attributeName-> attributeva lue pairs

* @see ModelMap#addAllAttributes(Map)

* @see #getModelMap()

*/

publicModelAndView addAllObjects(Map modelMap) {

getModelMap().addAllAttributes(modelMap);

returnthis;

}

/**

* 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

*/

publicvoid 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.

*/

publicboolean 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()

*/

publicboolean 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("/user.do")

publicclass UserControllerextends MultiActionController {

@RequestMapping(params="method=reg")

public ModelAndView reg(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");

returnmv;

}

}

<%@ page language="java"import="java.util.*"pageEncoding="gbk"%>

<%@ taglib prefix="c"uri="http://java.sun.com/js

首页 上一页 2 3 4 5 6 7 8 下一页 尾页 5/8/8
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇ftk学习记(list篇) 下一篇策略模式Strategy――回家乘什么..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目