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

 时间:2024-10-17 13:01:42

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

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

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

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

3、在跳转成功页面显示时间:<%@ 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> <h1>Success Page</h1> time:${requestScope.time }</body></html>

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

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

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

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

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

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

SpringMVC如何处理模型数据ModelAndView使用
  • 怎样让AI文件转入到ps中还是矢量图
  • 苹果来电不接不挂怎么退出
  • Axure实战003:APP启动界面5秒倒计时载入
  • 如何在AI中给文字添加红色和粗糙蜡笔效果
  • AI教程:[34]直接选择工具之删除所选锚点
  • 热门搜索
    盆腔炎怎么办 头大怎么办 耳膜穿孔怎么办 嗓子发炎怎么办 心慌气短是怎么回事 cad证书怎么考 qq微粒贷怎么开通 电动车真空胎怎么补 张飞是怎么死的 射进去了怎么办