zoukankan      html  css  js  c++  java
  • 补充:SpringMVC的结果跳转方式

    结果跳转方式

    1. ModelAndView

      设置ModelAndView对象, 根据view的名称, 和视图解析器, 跳转到指定的页面

    页面的路径: {视图解析器前缀} + viewName + {视图解析器后缀}

    视图解析器

    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    controller类

    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    //只要实现了Controller接口的类, 说明这就是一个控制器了
    public class ControllerTest1 implements Controller {
        @Override
        public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
            ModelAndView mv = new ModelAndView();
    
            //设置视图的属性
            mv.addObject("msg", "ControllerTest1");
            //设置跳转的页面
            mv.setViewName("test");
    
            return mv;
        }
    }

    页面的路径: /WEB-INF/jsp/test.jsp

    2. ServletAPI

      在controller中可以使用response和request 因为SpringMVC本质也是一个servlet!

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @Controller
    public class ModelTest1 {
    
        @RequestMapping("/m1/t1")
        public String test(HttpServletRequest req, HttpServletResponse resp) {
    
            System.out.println(req.getSession().getId());
    
            return "hello";
        }
    
    }

    3. SpringMVC实现【掌握】

    视图解析器:

    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    controller:

    @Controller
    @RequestMapping("/user")
    public class ControllerDemo {
    
        @RequestMapping("/u1")
        public String test1() {
            //转发一(注意:如果在xml中配置了视图解析器会找不到路径)
            return "/index.jsp";
        }
    
        @RequestMapping("/u2")
        public String test2() {
            //转发二
            return "forward:/index.jsp";
        }
    
        @RequestMapping("/u3")
        public String test3() {
            //重定向
            return "redirect:/index.jsp";
        }
    
        @RequestMapping("u4")
        public String test01() {
            // 重定向到另外一个请求
            return "redirect:/user/u5";
        }
    
        @RequestMapping("/u5")
        public String test02() {
            // 通过转发跟重定向可以到具体的页面
            return "forward:/index.jsp";
        }
    }
  • 相关阅读:
    基于贝叶斯概率模型的单幅图像去运动模糊算法
    Hihocoder 1067 最近公共祖先二
    HDU 2855 Fibonacci Check-up 矩阵
    HDU 2276 Kiki & Little Kiki 2 矩阵
    HDU 3483 A Very Simple Problem 矩阵构造
    HDU 2807 The Shortest Path 矩阵 + Floyd
    HDU 5015 233 Matrix 矩阵快速幂
    ZOJ 3497 Mistwald 矩阵
    POJ 3233 Matrix Power Series 矩阵等比数列求和
    HDU 2157 How many ways?? 矩阵
  • 原文地址:https://www.cnblogs.com/zhangzhixi/p/14294874.html
Copyright © 2011-2022 走看看