zoukankan      html  css  js  c++  java
  • 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>
    

      

  • 相关阅读:
    JS基础
    NodeJs实现他人项目实例
    Node.js在任意目录下使用express命令‘不是内部或外部命令’解决方法
    HTTP基本知识
    RESTful API
    基本概念和方法1
    Node.js--安装express以及创建第一个express项目(windows)
    Node-debug方法
    css3动画划过有一个框
    translate 动画不同时间飞入文字
  • 原文地址:https://www.cnblogs.com/achengmu/p/9230296.html
Copyright © 2011-2022 走看看