SpringMVC中ModelAndView的使用及说明

 更新时间:2022年11月22日 08:42:27   作者:风中的剑  
这篇文章主要介绍了SpringMVC中ModelAndView的使用及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

ModelAndView 作用

1.返回到指定的页面

ModelAndView构造方法可以指定返回的页面名称

例:

return new ModelAndView("redirect:/m07.jsp");

通过setViewName()方法跳转到指定的页面

例:

mav.setViewName("hello");

2.返回参数到指定页面的request作用于中

使用addObject()设置需要返回的值,addObject()有几个不同参数的方法,可以默认和指定返回对象的名字,参数会返回到新页面的request作用域中

ModelAndView 的3种用法

1.ModelAndView的第一种用法,先创建ModelAndView对象,再通过它的方法去设置数据与转发的视图名

  • setViewName(String viewName):‎设置此 ModelAndView 的视图名称, 由 DispatcherServlet 通过 ViewResolver 解析‎
  • addObject(String attributeName, Object attributeValue):通过key/value的方式绑定数据
package com.gxa.spmvc.controller;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
 
import com.gxa.spmvc.entity.Student;
 
/**
 * SpringMVC的控制器(业务控制器)
 * 定义的方法就是一个请求处理的方法
 * @author caleb
 *
 */
@Controller
@RequestMapping("/user")
public class TestController {
    
    /**
     * 利用ModelAndView来转发数据,给前端视图
     * @return
     */
    @RequestMapping("/m06")
    public ModelAndView m06() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("m06");
        modelAndView.addObject("message", "Hello World, Hello Kitty");
        return modelAndView;
    }
}

2.ModelAndView的第二种方法,可以直接通过带有参数的构造方法 ModelAndView(String viewName, String attributeName, Object attributeValue) 来返回数据与转发的视图名

package com.gxa.spmvc.controller;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
 
import com.gxa.spmvc.entity.Student;
 
/**
 * SpringMVC的控制器(业务控制器)
 * 定义的方法就是一个请求处理的方法
 * @author caleb
 *
 */
@Controller
@RequestMapping("/user")
public class TestController {
    
    /**
     * 利用ModelAndView来转发数据,给前端视图
     * @return
     */
    @RequestMapping("/m07")
    public ModelAndView m07() {
        return new ModelAndView("m07", "message", "Hello World");
    }
    
}

3.ModelAndView的第三种用法,设置重定向

package com.gxa.spmvc.controller;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
 
import com.gxa.spmvc.entity.Student;
 
/**
 * SpringMVC的控制器(业务控制器)
 * 定义的方法就是一个请求处理的方法
 * @author caleb
 *
 */
@Controller
@RequestMapping("/user")
public class TestController {
    
    /**
     * ModelAndView默认转发
     * ModelAndView还是可以设置重定向
     * 1. 重定向另一个控制器
     * 2. 重定向具体的jsp页面
     * @param name
     * @return
     */
    @RequestMapping("/{name}/m07")
    public ModelAndView m07(@PathVariable String name) {
        if (!"admin".equals(name)) {
            return new ModelAndView("redirect:/m07.jsp");
        }
        return new ModelAndView("m07");
    }
    
}

ModelAndView使用实例

要点:

  • 1.@RequestMapping 注解的使用
  • 2.modelandview 的使用
  • 3.jsp页面request作用域的取值
  • 4.视图解析器配置

ModelAndView 使用代码

package com.dgr.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@RequestMapping("mvc")
@Controller
public class TestRequestMMapping {
	
	@RequestMapping(value="/testModelAndView")
	public ModelAndView testModelAndView(){
		ModelAndView mav = new ModelAndView();
		mav.setViewName("hello");//跳转新的页面名称
		mav.addObject("address", "中国广东省广州市");//传入request作用域参数
		return mav;
	}
}

跳转前jsp页面链接设置

<a href="mvc/testModelAndView">Test ModelAndView</a>

跳转后jsp页面以及request作用于取值

<title>New Page</title>
</head>
<body>
	<h1>ModelAndView 跳转</h1>

	
	${requestScope.address} 

	
	${address }  
	
</body>

视图解析器配置

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 使用Springboot实现word在线编辑保存

    使用Springboot实现word在线编辑保存

    PageOffice目前支持的Web编程语言及架构有:Java(JSP、SSH、MVC等),ASP.NET(C#、VB.NET、MVC、Razor等),PHP,ASP,本篇文章就带你使用Springboot整合PageOffice实现word在线编辑保存
    2021-08-08
  • Java 中EasyExcel的使用方式

    Java 中EasyExcel的使用方式

    这篇文章主要介绍了Java 中EasyExcel的使用方式,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下
    2022-08-08
  • 使用SpringBoot简单了解Druid的监控系统的配置方法

    使用SpringBoot简单了解Druid的监控系统的配置方法

    这篇文章主要介绍了使用SpringBoot简单了解Druid的监控系统的配置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • 2个java希尔排序示例

    2个java希尔排序示例

    java希尔排序示例,希尔排序是插入排序的一种类型,也可以用一个形象的叫法缩小增量法,需要的朋友可以参考下
    2014-05-05
  • jedis获取redis中二进制图片转Base64方式

    jedis获取redis中二进制图片转Base64方式

    这篇文章主要介绍了jedis获取redis中二进制图片转Base64方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • 剑指Offer之Java算法习题精讲数组与字符串

    剑指Offer之Java算法习题精讲数组与字符串

    跟着思路走,之后从简单题入手,反复去看,做过之后可能会忘记,之后再做一次,记不住就反复做,反复寻求思路和规律,慢慢积累就会发现质的变化
    2022-03-03
  • ReentrantReadWriteLock不能锁升级的原因总结

    ReentrantReadWriteLock不能锁升级的原因总结

    今天给大家带来的是关于Java并发的相关知识,文章围绕着为什么ReentrantReadWriteLock不能锁升级展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06
  • RestTemplate发送HTTP POST请求使用方法详解

    RestTemplate发送HTTP POST请求使用方法详解

    这篇文章主要为大家介绍了RestTemplate发送HTTP POST请求的使用方法详解,有需要的朋友可以借鉴参考下希望能够有所帮助,祝大家多多进步
    2022-03-03
  • 详解SpringBoot 快速整合MyBatis(去XML化)

    详解SpringBoot 快速整合MyBatis(去XML化)

    本篇文章主要介绍了详解SpringBoot 快速整合MyBatis(去XML化),非常具有实用价值,需要的朋友可以参考下
    2017-10-10
  • 一文教你如何使用AES对接口参数进行加密

    一文教你如何使用AES对接口参数进行加密

    这篇文章主要是想为大家介绍一下如何使用AES实现对接口参数进行加密,文中的示例代码简洁易懂,具有一定的借鉴价值,需要的小伙伴可以了解一下
    2023-08-08

最新评论