springmvc七:处理模型数据_数据

 

ModelAndView 

springmvc七:处理模型数据_数据_02

package com.atChina.controller;

import java.io.IOException;
import java.io.Writer;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.atChina.entities.User;

@RequestMapping("/SpringMvc")
@Controller
public class RequestMapperingTest {
	
	/*
	 * 目标方法的返回值可以是ModelAndView类型
	 * 其中可以包含视图和模型信息
	 * SpringMvc会把ModelAndView的model中数据放入到request域对象中
	 */
	@RequestMapping("/testModelAndView")
	public ModelAndView testModelAndView(){
		
		String viewName = "success";
		ModelAndView modelAndView = new ModelAndView(viewName);
		
		// 添加模型数据到ModelAndView中
		modelAndView.addObject("time", new Date());
		return modelAndView;
	}
}

map作为参数

package com.atChina.controller;

import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.atChina.entities.User;

@RequestMapping("/SpringMvc")
@Controller
public class RequestMapperingTest {
	
	@RequestMapping("/testMap")
	public String testMap(Map<String, Object> map){
	
		map.put("names", Arrays.asList("水浒传","三国演义"));
		return "success";
	}
}

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
	你好啊 <br>
	names:${requestScope.names}
</body>
</html>

 

 @SessionAttributes,该注解只能放在类的上面,不能放在方法上面

springmvc七:处理模型数据_html_03

 

package com.atChina.controller;

import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.atChina.entities.User;

@SessionAttributes(value={"user"}, types={String.class})
@RequestMapping("/SpringMvc")
@Controller
public class RequestMapperingTest {
	
	@RequestMapping("/testSessionAttributes")
	public String testSessionAttributes(Map<String, Object> map){
		
		User user = new User("tianxia", "123456789", "123456789@qq.com", 22);
		map.put("user", user);
		map.put("jop", "coder");
		return "success";
	}
}

jsp页面,取出user,jop属性 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
	你好啊 <br>
	request user: ${requestScope.user} <br>
	session user: ${sessionScope.user} <br>
	request jop:  ${requestScope.jop} <br>
	session jop:  ${sessionScope.jop} <br>
</body>
</html>