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类

    package com.wang.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!

    package com.wang.controller;
    
    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实现

    1. 无需视图解析器

    先将Spring配置文件中的视图解析器去掉

    @Controller
    public class ResultSpringMVC {
        @RequestMapping("/rsm/t1")
        public String test1(){
            //转发
            return "/index.jsp";
        }
     
        @RequestMapping("/rsm/t2")
        public String test2(){
            //转发二
            return "forward:/index.jsp";
        }
     
        @RequestMapping("/rsm/t3")
        public String test3(){
            //重定向
            return "redirect:/index.jsp";
        }
    }
    

    此时要写页面的全限定名

    2. 使用视图解析器

    在Spring的配置文件中配置视图解析器, 注意路径的前缀与后缀

    @Controller
    public class ResultSpringMVC2 {
        @RequestMapping("/rsm2/t1")
        public String test1(){
            //转发
            return "test";
        }
     
        @RequestMapping("/rsm2/t2")
        public String test2(){
            //重定向
            return "redirect:/index.jsp";
            //return "redirect:hello.do"; //hello.do为另一个请求/
        }
     
    }
    
  • 相关阅读:
    微软一站式示例代码浏览器 v5.1 更新
    Developers’ Musthave: the new Microsoft AllInOne Code Framework Sample Browser and 3500+ samples
    栈溢出攻击 [转]
    深入浅出Java的访问者模式 [转]
    优先级反转 [转]
    latex 引用section [转]
    linux内存管理浅析 [转]
    静态,动态,强类型,弱类型 [转]
    linux硬链接与软链接 [转]
    GCC __attribute__ 详解 [转]
  • 原文地址:https://www.cnblogs.com/wang-sky/p/13633422.html
Copyright © 2011-2022 走看看