SpringMVC如何处理模型数据ModelAndView使用
1、ModelAndView既可以包含视图信息,也可以包含模型数倌栗受绽据信息。package com.gwolf.springmvc.h锾攒揉敫andlers;import java.util.Date;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.ExceptionHandler;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.servlet.ModelAndView;import com.gwolf.springmvc.domain.Person;@Controller@RequestMapping("/springmvc")public class HelloWorldController { @RequestMapping("/testModelAndView") public ModelAndView testModelAndView() { ModelAndView modelAndView = new ModelAndView("success"); //添加模型数据到ModelAndView中 modelAndView.addObject("time",new Date()); return modelAndView; } }
2、在页面中跳转上面跳转并且显示我们的时间。<html><body><h2> <a href="springmvc/testModelAndView">testModelAndView</a></h2></body></html>
3、在跳转成功页面显示时间:<%@ page language="ja箪滹埘麽va" 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> <h1>Success Page</h1> time:${requestScope.time }</body></html>
4、访问jsp页面,查看程序执行效果:
5、在springmvc中会遍历ModelAndView中的所有属性,并把请求域中的数据放到request对象中。
6、处理数据模型有以下三种方式: