• SpringMVC转发重定向


    转发一

    1 @Controller
    2 public class ModelTest {
    3     @RequestMapping("/t1")//url访问的地址  http://localhost:8080/springmvc_04_controller01_war_exploded/t1
    4     public String Test(Model model){
    5         model.addAttribute("msg","jsp下的test.jsp");
    6         /*默认使用forward转发*/
    7         return "/WEB-INF/jsp/test.jsp";
    8     }
    9 }

    转发二

    1 @Controller
    2 public class ModelTest {
    3     @RequestMapping("/t1")//url访问的地址  http://localhost:8080/springmvc_04_controller01_war_exploded/t1
    4     public String Test(Model model){
    5         model.addAttribute("msg","jsp下的test.jsp");
    6         /*在WEB-INF加forward转发*/
    7         return "forward:/WEB-INF/jsp/test.jsp";
    8     }
    9 }

    重定向

     1 @Controller
     2 public class ModelTest {
     3     @RequestMapping("/t1")//url访问的地址 
     4     public String Test(Model model){
     5         model.addAttribute("msg","jsp下的test.jsp");
     6         /*redirect*/
     7         return "redirect:/index.jsp";
     8     }
     9 }
    10 WEB-INF下的 资源是不能重定向的

    web配置

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
     5          version="4.0">
     6     <servlet>
     7         <servlet-name>springmvc</servlet-name>
     8         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     9         
    10         <init-param>
    11             <param-name>contextConfigLocation</param-name>
    12             <param-value>classpath:springmvc-servlet.xml</param-value>
    13         </init-param>
    14     </servlet>
    15     <servlet-mapping>
    16         <servlet-name>springmvc</servlet-name>
    17         <url-pattern>/</url-pattern>
    18     </servlet-mapping>
    19 
    20 </web-app>

    springmvc-servlet.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xmlns:mvc="http://www.springframework.org/schema/mvc"
     6        xsi:schemaLocation="http://www.springframework.org/schema/beans
     7         https://www.springframework.org/schema/beans/spring-beans.xsd
     8         http://www.springframework.org/schema/context
     9         http://www.springframework.org/schema/context/spring-context.xsd
    10         http://www.springframework.org/schema/mvc
    11         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    12     <!--开启注解-->
    13     <context:component-scan base-package="com.rzk.controller"/>
    14     <!--过滤静态资源-->
    15     <mvc:default-servlet-handler/>
    16     <!--省去了  处理器,映射器.适配器-->
    17     <mvc:annotation-driven/>
    18 
    19 </beans>

    上面是不加视图解析器

    springmvc-servlet.xml  配置视图解析器

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xmlns:mvc="http://www.springframework.org/schema/mvc"
     6        xsi:schemaLocation="http://www.springframework.org/schema/beans
     7         https://www.springframework.org/schema/beans/spring-beans.xsd
     8         http://www.springframework.org/schema/context
     9         http://www.springframework.org/schema/context/spring-context.xsd
    10         http://www.springframework.org/schema/mvc
    11         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    12     <!--开启注解-->
    13     <context:component-scan base-package="com.rzk.controller"/>
    14     <!--过滤静态资源-->
    15     <mvc:default-servlet-handler/>
    16     <!--省去了  处理器,映射器.适配器-->
    17     <mvc:annotation-driven/>
    18 
    19 <!--    &lt;!&ndash;视图解析器 &ndash;&gt;-->
    20     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    21           id="InternalResourceViewResolver">
    22         <!--前缀-->
    23         <property name="prefix" value="/WEB-INF/jsp/"/>
    24         <!--后缀-->
    25         <property name="suffix" value=".jsp"/>
    26     </bean>
    27 
    28 </beans>

    先试试重定向

    1 @Controller
    2 public class ModelTest {
    3     @RequestMapping("/t1")//url访问的地址
    4     public String Test(Model model){
    5         model.addAttribute("msg","jsp下的test.jsp");
    6         /*redirect*/
    7         return "redirect:/index.jsp";
    8     }
    9 }

     是能访问的   加了视图解析器也是一样的使用

    再试试有视图解析器的转发

    1 @Controller
    2 public class ModelTest {
    3     @RequestMapping("/t1")//url访问的地址
    4     public String Test(Model model){
    5         model.addAttribute("msg","index");
    6         /*redirect*/
    7         return "index";
    8     }
    9 }

  • 相关阅读:
    note
    deep learning
    matlab远程调试
    faster rcnn
    十一旅行
    python生成随机数
    python 读取mat文件
    opencv anaconda
    python文件操作
    python换行
  • 原文地址:https://www.cnblogs.com/rzkwz/p/12732875.html
Copyright © 2020-2023  润新知