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 ...
随机推荐
- 利用aspose-words 实现 java中word转pdf文件
利用aspose-words 实现 java中word转pdf文件 首先下载aspose-words-15.8.0-jdk16.jar包 引入jar包,编写Java代码 package test; ...
- react路由守卫
react没有vue那样的路由钩子beforeEach,实现登陆验证. 实现效果:如果没有登陆,就跳转到登陆界面,如果登陆过浏览器存有登陆信息就跳转到所输入的路由界面,如果该路由不存在则跳到404页面 ...
- Python使用pyMysql模块插入数据到mysql的乱码解决
1.初步安装mysql,插入中文字符,出现的???的形式 终端提示: pymysql.err.InternalError: (1366, "Incorrect string value: ' ...
- expdp全库备份rac数据库因错误终止
1.expdp导出日志报错如下: ORA-39014: One or more workers have prematurely exited. ORA-39029: worker 2 with pr ...
- 一次tns连接错误的解决过程
--同事hadoop连接oracle导入数据,界面报错,后台alert日志报错tns相关错误: **************************************************** ...
- lampp and testrail
https://wyzx.testrail.net szllq2000 http://129.0.1.228/testrail/ http://docs.gurock.com/testrail-adm ...
- python重定向sys.stdin、sys.stdout和sys.stderr
转自:https://www.cnblogs.com/guyuyuan/p/6885448.html 标准输入.标准输出和错误输出. 标准输入:一般是键盘.stdin对象为解释器提供输入字符流,一般使 ...
- Shader工具
1. RenderMonkey 2. NVIDIA FX Composer 2.5
- 跟我学Makefile(二)
命令出错: 每当命令运行完后, make 会检测每个命令的返回码,如果命令返回成功,那么 make 会执行下一条命令. 如果一个规则中的某个命令出错了(命令退出码非零),那么 make 就会终止执行当 ...
- Linux光标移动异常
刚刚安装完毕CentOS7.5,进行基础优化来着,发现我的光标具有如下神奇的故障. 无法移动到头部? 刚开始还以为是ISO镜像的问题,后校验了阿里云官网镜像的MD5值,和本地镜像MD5对比之后, 发现 ...