springMVC(7)------springMVC处理模型数据

在springMVC中提供四种模型数据处理的方法,分别如下:

(1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体可以通过该对象添加模型数据。

(2)Map及Model:传入参数为org.springframework.ui.Model、org.springframework.ui.ModelMap或java.util.Map时,

处理方法返回时,Map中的数据会自动添加到模型中。

(3)@SessionAttributes:将模型中的某个属性暂存到HttpSession中,以便多个请求之间可以共享这个数据。

(4)@ModelAttribute:方法传入参数标注该注解后,入参对象就会放到数据模型中。

以下为四种模型数据处理的方法,也就是后台返回数据,在前端页面解析出来,用户能够看到的实例:

一,所需jar包(根据自己的需要选择版本)

commons-logging.jar

spring-aop-4.1.8.RELEASE.jar

spring-beans-4.1.8.RELEASE.jar

spring-context-4.1.8.RELEASE.jar

spring-core-4.1.8.RELEASE.jar

spring-expression-4.1.8.RELEASE.jar

spring-web-4.1.8.RELEASE.jar

spring-webmvc-4.1.8.RELEASE.jar

二,项目结构

springMVC(7)------springMVC处理模型数据_第1张图片

三,web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
		xmlns="http://java.sun.com/xml/ns/j2ee" 
		xmlns:javaee="http://java.sun.com/xml/ns/javaee" 
		xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
		xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
		http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" 
		id="WebApp_ID" version="2.4">
  <!-- 配置DisptatcherServlet -->
  <servlet>
  	<servlet-name>springDispatcherServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 配置DispatcherServlet的初始化参数 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc.xml</param-value>
  	</init-param>
  	<!-- web应用被加载的时候创建servlet -->
  	<load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>springDispatcherServlet</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

四,springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.1.xsd">
    <!--  配置自动扫描的包  -->
    <context:component-scan base-package="com.lanhuigu.springmvc"/>
	<!-- 视图解析器 -->    
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <!-- 配置前缀 -->
          <property name="prefix" value="/WEB-INF/views/"/>
          <!-- 配置后缀 -->
          <property name="suffix" value=".jsp"/>
    </bean>
</beans>

五,视图(views--success.jsp)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
<%  
    String path = request.getContextPath();  
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
 %>  
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
 <html>  
    <head>  
      <title>spring MVC实例初体验</title>  
    </head>  
      
    <body>  
      <h1>success Page</h1> 
      
      <p>=================1.ModelAndView=================</p>
      <p>Time:${requestScope.time }</p><br>
      
      <p>=================2.Map or Model=================</p>
      <p>Map:${requestScope.username }</p><br>
      
      <p>=================3.@SessionAttributes=================</p>
      <p>SessionAttributes--request(请求域):${requestScope.user }</p><br>
      <p>SessionAttributes--session(session中属性名):${sessionScope.user }</p><br>
      <p>SessionAttributes--session(session中属性类型):${sessionScope.staff }</p><br>
      
    </body>  
</html>  

六,控制器(TestDealModelData.java)

package com.lanhuigu.springmvc.action;

import java.util.Date;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 *测试 springMVC模型数据处理方式 
 */
@SessionAttributes(value={"user"},types={String.class})
@RequestMapping("/springDMD")
@Controller
public class TestDealModelData {
	private static final String SUCCESS = "success";
	//1.================ModelAndView=============
	/**
	 * 处理方法返回值类型为ModelAndView时,方法体可以通过该对象添加模型数据。
	 */
	@RequestMapping("/testModelAndView")
	public ModelAndView testModelAndView() {
		String viewName = SUCCESS;//视图名字
		ModelAndView modelAndView = new ModelAndView(viewName);
		
		//添加数据到ModelAndView中
		modelAndView.addObject("time", new Date());
		
		return modelAndView;
	}
	//2.================Map及Model================
	/**
	 * 目标方法可以添加Map类型或Model类型的参数
	 */
	@RequestMapping("/testMap")
	public String testMap(Map<String,Object> map) {
		map.put("username", "Make it");
		return SUCCESS;
	}
	//3.================@SessionAttributes========
	/**
	 * 通过注解@SessionAttributes,根据属性名(user),或属性值类型(admin),
	 * 把数据放在会话(session)中,实现数据共享
	 * 
	 * 这里的键值user与类开头@SessionAttributes中的名字一致,把user对象放在session中,
	 * 实现数据共享
	 */
	@RequestMapping("/testSessionAttributes")
	public String testSessionAttributest(Map<String,Object> map) {
		map.put("user", "[username=hh,password=123456]");
		map.put("staff","admin");
		return SUCCESS;
	}
}

七,访问入口页面(index.jsp)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
<%  
    String path = request.getContextPath();  
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
 %>  
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
 <html>  
    <head>  
      <title>spring MVC POJO</title>  
    </head>  
      
    <body>  
      <form action="testpj/testPojo" method="post">
      	username:<input name="username"/><br>
      	password:<input type="password" name="password"/><br>
      	province:<input name="address.province"/><br>
      	city:<input name="address.city"/><br>
      	<input type="submit" value="testPojoSubmit">
      </form>
      
      <!-- 1.ModelAndView -->
      <a href="springDMD/testModelAndView">Test ModelAndView</a><br>
      <!-- 2.Map or Model -->
      <a href="springDMD/testMap">Test Map</a><br>
      <!-- 3.@SessionAttributes -->
      <a href="springDMD/testSessionAttributes">Test @SessionAttributes</a><br>
    </body>  
</html>  

启动服务,通过http://localhost:9000/SpringMVC/index.jsp进入index.jsp,点击相应的超链接,进行测试。

@ModelAttribute在下一篇文章中了解......

你可能感兴趣的:(map,ModelAndView,springMVC处理模型数据)