博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Model/ModelMap 和 ModelAndView 的区别使用
阅读量:5066 次
发布时间:2019-06-12

本文共 3866 字,大约阅读时间需要 12 分钟。

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;@Controllerpublic 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" %>
hello-index
姓名:
密码:

  

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"%>
post

post

name: ${user.name}
passwod: ${user.password}
${sucess}

  

 

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;@Controllerpublic 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" %>
h2-index
name:
password:

  

h2_post.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"   pageEncoding="utf-8"%><%@ page isELIgnored="false" %>
h2-post

post

name: ${user.name}
password: ${user.password}

  

 

转载于:https://www.cnblogs.com/achengmu/p/9230296.html

你可能感兴趣的文章
ant 安装
查看>>
新手Python第一天(接触)
查看>>
vue路由动态加载
查看>>
iOS中ARC内部原理
查看>>
【bzoj1029】[JSOI2007]建筑抢修
查看>>
synchronized
查看>>
你不得不了解的应用容器引擎---Docker
查看>>
easyui datagrid 弹出页面会出现两个上下滚动条处理办法!
查看>>
迭代器和生成器
查看>>
codevs 1080 线段树练习
查看>>
JS模块化库seajs体验
查看>>
Android内核sysfs中switch类使用实例
查看>>
POJ2288 Islands and Bridges(TSP:状压DP)
查看>>
[No0000195]NoSQL还是SQL?这一篇讲清楚
查看>>
IOS开发UI篇--UITableView的自定义布局==xib布局
查看>>
【深度学习】caffe 中的一些参数介绍
查看>>
Python-Web框架的本质
查看>>
Unrecognized Windows Sockets error: 0: JVM_Bind 异常解决办法
查看>>
QML学习笔记之一
查看>>
7NiuYun云存储UploadPicture
查看>>