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

 时间:2026-02-14 02:56:15

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]直接选择工具之删除所选锚点
  • 热门搜索
    弱电是什么 灰指甲是什么症状 clever什么意思 c1驾驶证能开什么车 凝望的近义词是什么 什么时候跑步最好 冠状病毒是什么 7月15日是什么节日 美人鱼是什么动物 九牛一毛是什么意思