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 ...
随机推荐
- react 侧栏二级菜单组件
侧边栏菜单组件 component 下新建menu文件,menu下建index.jsx和subitem.jsx index.jsx import React, { Component } from ' ...
- .Net 获取前端传递的数据
1. DotNet MVC: form是用来获得表单提交的数据:querystring是用来获得标识在URL后面的所有返回的变量及其值. 比如常见的URL网页地址都有xxx.asp?pn=123456 ...
- scrapy爬虫系列之六--模拟登录
功能点:如何发送携带cookie访问登录后的页面,如何发送post请求登录 爬取网站:bilibili.github 完整代码:https://files.cnblogs.com/files/book ...
- 七夕节---hdu1215(打表求因子和)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1215 给你一个数n(1<=n<=50w)求n的所有因子和, 由于n的范围比较大,所以要采用 ...
- ansible(3)
一.setup模块 ansible的setup模块主要用来收集信息,查看参数: [root@localhost ~]# ansible-doc -s setup # 查看参数,部分参数如下: filt ...
- mysql 数据操作 单表查询 where约束 工作模式
select name,age from employee where id >7; 1.首先先找到表 from employee 2.表存在 mysql拿着约束条件 去表里 看依次匹配数 ...
- wget 命令大全
wget 命令大全 wget默认会以最后一个符合”/”的后面的字符来命令,对于动态链接的下载通常文件名会不正确 wget http://www.minjieren.com/wordpress-3. ...
- Flask系列之自定义中间件
from flask import Flask app = Flask(__name__) @app.route('/index') def index(): return 'Hello World' ...
- sqlnet.ora的作用
sqlnet.ora的作用 1.限制客户端访问(如指定客户端域为不允许访问) 2.指定命名方法(local naming,directory nameing...)的优先级 3.启用日志及跟踪(log ...
- Cocos2d-JS实现的打飞机
一.前言 今天我们来讲一个最最最常见的一个小游戏--打飞机!是的,打飞机!还记得小时候在玩儿的雷电,应该是打飞机最早的样子了吧.直到现在,也有微信打飞机,全民飞机大战,全民打飞机等游戏的出现,这些游戏 ...