您的位置:首页 > 编程语言 > Java开发

SpringMVC学习(四)-处理模型数据

2017-08-26 00:24 337 查看
处理模型数据的方式:

ModelAndView

Map

SessionAttributes注解

1.SpringMVCTest.java测试类

package com.springMVC.Helloworld;

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

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.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;

@SessionAttributes(value= {"user"},types= {String.class})
@RequestMapping("/springmvc")
@Controller
public class SpringMVCTest {

/*
* @SessionAttributes除了可以通过属性名指定主要放入会话中的属性
* 还可以通过模型的对象类型指定那些模型 需要放到会话中
*/
@RequestMapping("/testSessionAttributes")
public String testSessionAttributes(Map<String,Object> map) {
User user=new User("Tom", "123", "123@qq.com", 12);
map.put("user", user);
map.put("school", "XUPT");
return "success";
}

/*
* 目标方法可以添加Map类型
*/
@RequestMapping("/testMap")
public String testMap(Map<String,Object> map) {
map.put("names", Arrays.asList("Tom","Jerry","Mike"));
return "success";
}
/*
* 目标方法返回值可以是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;
}

@RequestMapping("/testPojo")
public String testPojo(User user) {

System.out.println("testPojo :"+user);
return "success";
}

/*
* @CookieValue映射一个Cook值
*/
@RequestMapping("testCookieValue")
public String testCookieValue(@CookieValue("JSESSIONID") String sessionId) {

System.out.println("testCookieValue:sessionId "+sessionId);
return "success";
}

}


2.index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>

<body>

<a href="springmvc/testSessionAttributes">testSessionAttributes</a>
<br><br>

<a href="springmvc/testMap">testMap</a>
<br><br>

<a href="springmvc/testModelAndView">testModelAndView</a>
<br><br>

<form action="springmvc/testPojo">
username:<input type="text" name="username"/>
<br>
password:<input type="password" name="password"/>
<br>
email<input type="text" name="email"/>
<br>
age:<input type="text" name="age"/>
<br>
city:<input type="text" name="address.city">
<br>
province:<input type="text" name="address.province">
<br>
<input type="submit" value="submit">
</form>

<a href="springmvc/testCookieValue">testCookieValue</a>
<br><br>

</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring mvc