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 ...
随机推荐
- js 的正则表达式 部分展示test()方法的验证功能
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- The Rise of Database Sharding DATABASE SHARDING
w玻璃碎片.0共享 http://www.agildata.com/database-sharding/ The Rise of Database Sharding The concept of Da ...
- sklearn学习笔记(一)——数据预处理 sklearn.preprocessing
https://blog.csdn.net/zhangyang10d/article/details/53418227 数据预处理 sklearn.preprocessing 标准化 (Standar ...
- Spring Boot+CXF搭建WebService
Spring Boot WebService开发 需要依赖Maven的Pom清单 <?xml version="1.0" encoding="UTF-8" ...
- cocos代码研究(19)Widget子类ImageView学习笔记
理论基础 显示图片的小控件,继承自 Widget . 代码实践 static ImageView * create()创建一个空的ImageView static ImageView * create ...
- VS2010/MFC编程入门之十(对话框:设置对话框控件的Tab顺序)
前面几节鸡啄米为大家演示了加法计算器程序完整的编写过程,本节主要讲对话框上控件的Tab顺序如何调整. 上一讲为“计算”按钮添加了消息处理函数后,加法计算器已经能够进行浮点数的加法运算.但是还有个遗留的 ...
- 线程、进程、daemon、GIL锁、线程锁、递归锁、信号量、计时器、事件、队列、多进程
# 本文代码基于Python3 什么是进程? 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别就在于:程序是指令的集合,它是进程运行 ...
- C# static的用法详解
C# static的用法详解 有的东西你天天在用,但未必就代表你真正了解它,正如我之前所了解的 static . 一.静态类 静态类与非静态类的重要区别在于静态类不能实例化,也就是说,不能使用 n ...
- Mac OS 终端下使用 Curl 命令下载文件
在 mac os下,如何通过命令行来下载网络文件?如果你没有安装或 wget 命令,那么可以使用 curl 工具来达到我们的目的. curl命令参数: curl 'url地址' curl [选项] ' ...
- lombok常见注解
一.使用lombok简化代码 lombok提供了很多注解,在编译时候生成java代码,代替了手工编写一些简单的代码,使程序员可以关注更重要的实现. 二.常用注解 以model为例 public cla ...