springMvc ModelAndView

ModelAndView


package springMvc01.MVC.handlers;

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

@RequestMapping("/springmvc")
@Controller
public class SpringTest {

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

index.jsp

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

sussess.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>
Success
<br>
time:${requestScope.time}
</body>
</html>
ModelMap 类型)的参数

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

@RequestMapping("/springmvc")
@Controller
public class SpringTest {

	private static final String SUSSESS="success";
	/**
	 * 目标方法可以添加Map类型(实际上也可以是Model 类型的ModelMap 类型)的参数
	 * @param map
	 * @return
	 */
	@RequestMapping("/testMap")
	public String testMap(Map<String,Object> map) {
		System.out.println(map.getClass().getName());//org.springframework.validation.support.BindingAwareModelMap
		map.put("names", Arrays.asList("tom","Jerry","Mike"));
		System.out.println("testMap");
		return SUSSESS;
	}
}

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

@SessionAttributes 实现

package springMvc01.MVC.handlers;

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 entities.User;
/**
 * @SessionAttributes  除了可以通过属性名指定需要放到会话中的属性外,如value={"user"}(value属性值)
 * 还可以通过模型属性的对象类型指定哪些模型属性需要放到对话中 如types= {String.class}(types属性值)
 * 注意:该注解只能使用在类上,不能放在方法上
 * @author Administrator
 *
 */
@SessionAttributes(value={"user"},types= {String.class})
@RequestMapping("/springmvc")
@Controller
public class SpringTest {

	private static final String SUSSESS="success";
	
	@RequestMapping("/testAttributes")
	public String testAttributes(Map<String,Object> map) {
		User user = new User("Tom","123456","tom@qq.com",15);
		map.put("user", user);
		map.put("school", "atguigu");
		return SUSSESS;
		}
}
index.jsp:<a href="springmvc/testAttributes">testAttributes</a>

success.jsp

request user:${requestScope.user}
<br>
session user :${sessionScope.user}
<br>
request school:${requestScope.school}
<br>
session school :${sessionScope.school}



@ModelAttribute实现

package springMvc01.MVC.handlers;

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.ModelAttribute;
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 entities.User;
/**
 * @SessionAttributes  除了可以通过属性名指定需要放到会话中的属性外,如value={"user"}(value属性值)
 * 还可以通过模型属性的对象类型指定哪些模型属性需要放到对话中 如types= {String.class}(types属性值)
 * 注意:该注解只能使用在类上,不能放在方法上
 * @author Administrator
 *
 */
//@SessionAttributes(value={"user"},types= {String.class})
@RequestMapping("/springmvc")
@Controller
public class SpringTest {

	private static final String SUSSESS="success";
	
	@RequestMapping("/testModelAttribute")
	public String testModelAttribute(User user) {
		System.out.println("update:"+user);
		return SUSSESS;
	}
	/**
	 * 由@ModelAttribute标记的方法,会在每个目标方法执行之前被springmvc调用
	 * @param id
	 * @param map
	 */
	@ModelAttribute
	public void getUser(@RequestParam(value="id",required=false) Integer id,Map<String,Object>map) {
		if(id !=null) {
			//模拟从数据库中获取对象然后把对象放在map里
			User user = new User(1,"Tom","123456","tom@qq.com",12);
			System.out.println("从数据库获取一个对象:"+user);
			map.put("user", user);
		}
	}
}

运行流程:

1,执行
@ModelAttribute注解修饰的方法,从数据库中取出对象,把对象放入到map中,键为:user
2.springMvc 从map中取出user对象,并把表单的请求对象赋给user对象的对应属性,
3.springMvc把上述对象传入目标方法的参数


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值