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 ...
随机推荐
- 使用jquery的$.post()时浏览器崩溃
代码: function verifyStepOne() { var phoneNumber = $("#phoneNumber"); var username = $(" ...
- Redundant Paths---poj3177(双连通分量)
题目链接:http://poj.org/problem?id=3177 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使 ...
- Python开发【Django】:图片验证码、KindEditor
图片验证码 生成图片验证码需要以下: session check_code.py(依赖:Pillow,字体文件) 模块安装 pip install Pillow src属性后面加? 在utils下拷贝 ...
- 评论抓取:Python爬取微信在APPStore上的评论内容及星级
#完整程序如下: import requests import re def getHTMLText(url): try: r = requests.get(url) r.raise_for_stat ...
- php中使用Curl、socket、file_get_contents三种方法POST提交数据
抓取远程内容,之前一直都在用file_get_content函数,其实早就知道有curl这么一个好东西的存在,但是看了一眼后感觉使用颇有些复杂,没有file_get_content那么简单,再就是需求 ...
- Flask系列(九)flask-script组件
Flask Script扩展提供向Flask插入外部脚本的功能,包括运行一个开发用的服务器,一个定制的Python shell,设置数据库的脚本,cronjobs,及其他运行在web应用之外的命令行任 ...
- 使用distcp并行拷贝大数据文件
以前我们介绍的访问HDFS的方法都是单线程的,Hadoop中有一个工具可以让我们并行的拷贝大量数据文件,这个工具就是distcp. distcp的典型应用就是在两个HDFS集群中拷贝文件,如果两个集群 ...
- Spark的RDD原理以及2.0特性的介绍
转载自:http://www.tuicool.com/articles/7VNfyif 王联辉,曾在腾讯,Intel 等公司从事大数据相关的工作.2013 年 - 2016 年先后负责腾讯 Yarn ...
- Python的星号(*)和双星号(**)用法
①引言 在Python中,星号除了用于乘法数值运算和幂运算外,还有一种特殊的用法是在变量前加单个星号或两个星号,实现多参数的传人或变量的拆解. ②什么是星号变量 最初,星号变量是用在函数的参数传递上的 ...
- Qt信号和槽连接方式的选择
看了下Qt的帮助文档,发现connect函数最后还有一个缺省参数. connect函数原型是这样的: QMetaObject::Connection QObject::connect(const QO ...