SpringMVC forward redirect

Forward one

1  @Controller
 2  public  class ModelTest {
 3      @RequestMapping ( " / t1 " ) // url access address   http: // localhost : 8080 / springmvc_04_controller01_war_exploded / t1 
4      public String Test (Model model) {
 5          model.addAttribute ( " msg " , " test.jsp under jsp " );
 6          / * forward forwarding is used by default * / 
7          return  " /WEB-INF/jsp/test.jsp " ;
 8      }
 9 }

Forward two

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 }

Redirect

1  @Controller
 2  public  class ModelTest {
 3      @RequestMapping ( " / t1 " ) // url access address 
4      public String Test (Model model) {
 5          model.addAttribute ( " msg " , " test.jsp under jsp " ) ;
 6          / * redirect * / 
7          return  " redirect: /index.jsp " ;
 8      }
 9  }
 10 Resources under WEB-INF cannot be redirected

 

web configuration

 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      <!-Open note->
 13      <context : component-scan base -package = " com.rzk.controller " />
 14      <!-Filter static resources->
 15      <mvc:default -servlet-handler />
 16      <!-Omit processor, mapper.      Adapter- >
 17 <mvc: annotation-driven />
 18  
19 </ beans>

 

The above is without a view resolver

 

springmvc-servlet.xml configuration view parser

 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      <!-Open note->
 13      <context : component-scan base -package = " com.rzk.controller " />
 14      <!-Filter static resources->
 15      <mvc:default -servlet-handler />
 16      <!-Omit processor, mapper.      Adapter- >
 17 <mvc: annotation-driven />
 18  
19 <!-& lt;! & ndash; View resolver & ndash; & gt;->
 20      <bean class = " org.springframework.web.servlet.view.InternalResourceViewResolver " 
21            id = " InternalResourceViewResolver " >
 22          <!- prefix- >
 23          <property name = " prefix " value = " / WEB-INF / jsp / " />
24          <!-Suffix->
 25         <property name="suffix" value=".jsp"/>
26     </bean>
27 
28 </beans>

Try redirection first

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 }

 

 It can be accessed, and the view resolver is also used in the same way.

 

Try forwarding with view resolver again

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 }

 

Guess you like

Origin www.cnblogs.com/rzkwz/p/12732875.html