您的位置首页百科知识

SpringMVC如何处理模型数据ModelAndView使用

SpringMVC如何处理模型数据ModelAndView使用

的有关信息介绍如下:

SpringMVC如何处理模型数据ModelAndView使用

SpringMVC如何处理模型数据ModelAndView使用

ModelAndView既可以包含视图信息,也可以包含模型数据信息。

package com.gwolf.springmvc.handlers;

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;

}

}

在页面中跳转上面跳转并且显示我们的时间。

testModelAndView

在跳转成功页面显示时间:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

Insert title here

Success Page

time:${requestScope.time }

访问jsp页面,查看程序执行效果:

在springmvc中会遍历ModelAndView中的所有属性,并把请求域中的数据放到request对象中。

处理数据模型有以下三种方式: