Model/ModelMap 和 ModelAndView 的区别使用
Model/ModelMap 和 ModelAndView 的区别使用
Model/ModelMap
controller:
package springmvc.controller; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import springmvc.model.User; @Controller
public class HelloController { private static final Log logger = LogFactory.getLog(HelloController.class); @ModelAttribute
public void userModel(String name, String password, Model model)
{
logger.info("userModel");
//创建userModel
User user = new User();
user.setName(name);
user.setPassword(password);
//user对象存在model当中
model.addAttribute("user", user);
} @RequestMapping(value="/hello", method=RequestMethod.GET)
public String hello()
{
return "index";
} @RequestMapping(value="/login", method=RequestMethod.POST)
public String post(Model model)
{
//从model中取出之前存的user对象
User user = (User) model.asMap().get("user");
System.out.println(user);
//设置user对象
user.setName("测试");
model.addAttribute("sucess", "ok");
System.out.println(user);
return "post";
} }
jsp:
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>hello-index</title>
</head>
<body> <form:form method="post" action="/gu2/login" modelAttribute="user">
姓名:<form:input path="name" id="name"/>
<br>
密码:<form:password path="password" id="password"/>
<br>
<input type="submit" vlaue="提交">
</form:form> </body>
</html>
post.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>post</title>
</head>
<body> <h3>post</h3>
name: ${user.name}<br>
passwod: ${user.password}<br>
${sucess} </body>
</html>
ModelAndView
controller:
package springmvc.controller; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; import springmvc.model.User; @Controller
public class Hello2Controller { private static final Log logger = LogFactory.getLog(Hello2Controller.class); @ModelAttribute
public void userModel(String name, String password, ModelAndView mv)
{
logger.info("usermodel");
User user = new User();
user.setName(name);
user.setPassword(password); mv.addObject("user", user);
} @RequestMapping(value="/h2", method=RequestMethod.GET)
public ModelAndView index()
{
//return new ModelAndView("hello2_index", "commond", new User());
return new ModelAndView("h2_index", "command", new User());
} @RequestMapping(value="/lh2",method=RequestMethod.POST)
public ModelAndView post( ModelAndView mv)
{
logger.info("hello2_index_h2");
//从modelAndView中的model里获取user
User user = (User) mv.getModel().get("user");
System.out.println(user); if( user !=null)
user.setName("ccccc"); mv.setViewName("h2_post");
return mv;
} }
jsp
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>h2-index</title>
</head>
<body> <form:form method="post" action="/gu2/lh2" command="user">
name: <form:input path="name" id="name"/>
password: <form:input path="password" id="password"/>
<input type="submit" value="提交"/>
</form:form> </body>
</html>
h2_post.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>h2-post</title>
</head>
<body> <h3>post</h3>
name: ${user.name}<br>
password: ${user.password}<br> </body>
</html>
Model/ModelMap 和 ModelAndView 的区别使用的更多相关文章
- springMVC Model ModelMap 和 ModelAndView的区别(转)
原文地址:springMVC Model ModelMap 和 ModelAndView的区别 近来在看代码,发现controller里有不同的处理返回数据的方式,而自己一直在用ModelAndVie ...
- Spring中Model,ModelMap以及ModelAndView之间的区别
原文链接:http://blog.csdn.net/zhangxing52077/article/details/75193948 Spring中Model,ModelMap以及ModelAndVie ...
- SpringMVC Map Model ModelMap 和 ModelAndView
代码: package com.gaussic.controller; import com.gaussic.model.AccountModel; import org.springframewor ...
- Spring中Model、ModelMap及ModelAndView之间的区别
Spring中Model.ModelMap及ModelAndView之间的区别 1. Model(org.springframework.ui.Model)Model是一个接口,包含addAttr ...
- Model、ModelMap、ModelAndView的使用和区别
1.Model的使用 数据传递:Model是通过addAttribute方法向页面传递数据的: 数据获取:JSP页面可以通过el表达式或C标签库的方法获取数据: return:return返回的是指定 ...
- Model、ModelMap、ModelAndView的作用及区别
Model.ModelMap.ModelAndView的作用及区别 对于MVC框架,控制器controller执行业务逻辑 用于产生模型数据Model 视图view用来渲染模型数据 Model和Mod ...
- ModelMap和ModelAndView区别
首先介绍ModelMap和ModelAndView的作用 ModelMap ModelMap对象主要用于传递控制方法处理数据到结果页面,也就是说我们把结果页面上需要的数据放到ModelMap对象中即可 ...
- Spring MVC 向页面传值-Map、Model、ModelMap、ModelAndView
Spring MVC 向页面传值,有4种方式: ModelAndView Map Model ModelMap 使用后面3种方式,都是在方法参数中,指定一个该类型的参数. Model Model 是一 ...
- SpringMVC学习 -- ModelAndView , Model , ModelMap , Map 及 @SessionAttributes 的使用
输出模型数据: ModelAndView:处理方法返回值类型为 ModelAndView 时 , 其中包含视图和模型信息.方法体即可通过该对象添加模型数据 , 即 SpringMVC 会把 Model ...
随机推荐
- java利用poi 把ppt转化为图片,
导入jar包: poi-3.8.jar poi-ooxml-3.9.jar poi-scratchpad-3.8.jar 代码: package test4; import java.awt.Dime ...
- Spring MVC 框架学习
一.spirng的简介 Spring是一个开源框架,它由Rod Johnson创建.它是为了解决企业应用开发的复杂性而创建的.Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情. ...
- poj2185 Milking Grid【KMP】
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10084 Accepted: 4371 Des ...
- go 工具链目前[不支持编译 windows 下的动态链接库]解决方案
go 工具链目前[不支持编译 windows 下的动态链接库][1],不过[支持静态链接库][2].想要产生dll,可以这样 workaround ,参考 golang [issuse#11058][ ...
- python基础之递归、二分法
一 递归 1. 必须有一个明确的结束条件2. 每次进入更深一层递归时,问题规模相比上次递归都应有所减少3. 递归效率不高,递归层次过多会导致栈溢出(在计算机中,函数调用是通过栈(stack)这种数据结 ...
- 双态运维联盟(BOA)正式成立
3月1日,由联想.新华三.华为等12家IT企业在北京正式达成协议,联合发起成立“双态运维联盟”.中国电子工业标准技术协会.信息技术服务分会数据中心运营管理工作组(DCMG)组长肖建一先生出席了会议. ...
- Word 2010文档自动生成目录和某页插入页码
一.Word 2010文档自动生成目录 关于Word文档自动生成目录一直是我身边同学们最为难的地方,尤其是毕业论文,经常因为目录问题,被要求修改,而且每次修改完正文后,目录的内容和页码可能都会发生变化 ...
- [转] Delphi Socket Architecture
Delphi Socket Architecture - Felix John COLIBRI. abstract : The architecture of the ScktComp socket ...
- Python实现支付宝在线支付
windows系统安装Python虚拟环境 首先保证你的系统已经安装好了Python 安装virtualenv C:\WINDOWS\system32>pip3 install virtuale ...
- 文字和图片居中的HTML代码怎么写?
HTML 代码 ,怎么将文本/ 图片居中?这是在W3Cschool的编程问答中前端♌蕾儿提出的问题.网友施主同西否给出了详细的解答. html文字居中和html图片居中方法代码,通过在html中实现文 ...