SpringMVC 中ModelAndView用法

SpringMVC 中ModelAndView用法ModelAndView作用1.返回到指定的页面ModelAndView构造方法可以指定返回的页面名称   例:returnnewModelAndView("redirect:/m07.jsp");通过setViewName()方法跳转到指定的页面   例:mav.setViewName("hello"); 2.返回参数到指定页面的request作用于中使…

大家好,又见面了,我是你们的朋友全栈君。

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>
	
	<br>
	
	${requestScope.address}   
	
	<br>
	
	${address }    

	<br>	
	
</body>

视图解析器配置

SpringMVC 中ModelAndView用法

SpringMVC 中ModelAndView用法

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/161006.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • PhpStorm激活码2021.04(最新序列号破解)

    PhpStorm激活码2021.04(最新序列号破解),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月19日
    44
  • 黑盒测试的等价类划分法_黑盒测试等价类输出

    黑盒测试的等价类划分法_黑盒测试等价类输出1、等价类划分2、边界值分析

    2022年10月3日
    4
  • 解决pycharm新建项目后按钮灰色问题

    解决pycharm新建项目后按钮灰色问题解决pycharm新建项目后按钮灰色问题出现过多次该问题了,在此记录一下同样适用于导入别人的新项目后无法运行问题原因一:  pycharm没有设置系统解析器解决方法一:  打开pycharm->File->Settings->ProjectInterpreter->设置python路径(系统python)原因二:  没有创…

    2022年8月29日
    2
  • JDK1.8新特性CompletableFuture总结

    CompletableFuture这个completableFuture是JDK1.8版本新引入的类。下面是这个类。实现了俩接口。本身是个class。这个是Future的实现类。使用completionStage接口去支持完成时触发的函数和操作。一个completetableFuture就代表了一个任务。他能用Future的方法。还能做一些之前说的executorService配合fu…

    2022年4月4日
    155
  • goland2020 激活码_通用破解码

    goland2020 激活码_通用破解码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月16日
    421
  • uIP协议栈分析_协议栈

    uIP协议栈分析_协议栈转载地址:http://blog.sina.com.cn/s/blog_abd39cc70101fj1f.htmluIP特性uIP协议栈往掉了完整的TCP/IP中不常用的功能,简化了通讯流程,但保存了网络通讯必须使用的协议,设计重点放在了IP/TCP/ICMP/UDP/ARP这些网络层和传输层协议上,保证了其代码的通用性和结构的稳定性。由于uIP协议栈专门为嵌进式系统而设计,因此还具有…

    2022年8月30日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号