zoukankan      html  css  js  c++  java
  • 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>
    
    <br>
    
    ${requestScope.address} 
    
    <br>
    
    ${address } 
    
    <br>    
    
    </body>

    ————————————————

    原文链接:https://blog.csdn.net/qq_41648616/java/article/details/81776145

  • 相关阅读:
    TYVJ 2002 扑克牌 题解
    TYVJ P1933 绿豆蛙的归宿 题解(未完成)
    TYVJ-P1864 守卫者的挑战 题解
    HDU 4901 The Romantic Hero 题解——S.B.S.
    OpenJudge 8782 乘积最大——S.B.S
    COGS 08-备用交换机 题解——S.B.S.
    poj2186 Popular Cows 题解——S.B.S.
    高级c++头文件bits/stdc++.h
    #include &lt;NOIP2010 Junior&gt; 三国游戏 ——using namespace wxl;
    NOIP 2008提高组第三题题解by rLq
  • 原文地址:https://www.cnblogs.com/afei1013/p/12987926.html
Copyright © 2011-2022 走看看