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

2025-05-24 03:19:50

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

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

2、在页面中跳转上面跳转并且显示我们的时间。<html><body><h2> <a href="springmvc/testModelAndView">testModelAndView</a></h2></body></html>

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

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>

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

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

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

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

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

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

SpringMVC如何处理模型数据ModelAndView使用
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢