还是一样的操作
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.chentianyu</groupId><artifactId>springmvc_requestGoTo</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.16</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version></dependency></dependencies><build><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include><include>**/*.properties</include></includes></resource><resource><directory>src/main/resources</directory><includes><include>**/*.xml</include><include>**/*.properties</include></includes></resource></resources></build></project>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.chentianyu.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/admin/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:default-servlet-handler />
<mvc:annotation-driven />
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<filter>
<filter-name>filter1</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>filter1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/one">1.请求转发页面</a>
<a href="${pageContext.request.contextPath}/two">2.请求转发</a>
<a href="${pageContext.request.contextPath}/three">3.重定向页面</a>
<a href="${pageContext.request.contextPath}/four">4.重定向</a>
</body>
</html>
请求转发页面&请求转发
package com.chentianyu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class JumpAction {
@RequestMapping("/one")
public String one(){
System.out.println("这是请求转发页面跳转");
return "main";//默认使用视图解析器拼接前缀后缀进行页面跳转
}
@RequestMapping("/two")
public String two(){
System.out.println("这是请求转发action跳转");
//'/other'这个会变成 /admin/other/.jsp
return "/other";//默认使用视图解析器拼接前缀后缀进行页面跳转
}
}
package com.chentianyu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class OtherAction {
@RequestMapping("/other")
public String other(){
System.out.println("这是other的action访问...");
return "main";
}
}
不会成功因为会跳转的是因为'/other'这个会变成 /admin/other/.jsp,我们去看InternalResourceViewResolver类
public class InternalResourceViewResolver extends UrlBasedViewResolver {}
它是集成了UrlBasedViewResolver类
public class UrlBasedViewResolver extends AbstractCachingViewResolver implements Ordered {
public static final String REDIRECT_URL_PREFIX = "redirect:";
public static final String FORWARD_URL_PREFIX = "forward:";
@Nullable
private Class<?> viewClass;
private String prefix = "";
private String suffix = "";
....
}
当我们看到prefix和suffix的设置,是成员变量。我们看到还有forward:和redirect:使用这俩个就不会再进行前缀后缀的拼接
修改
@RequestMapping("/two")
public String two(){
System.out.println("这是请求转发action跳转");
//'/other'这个会变成 /admin/other/.jsp
//forward: 这组字符串可以屏蔽前缀和后缀的拼接
return "forward:/other";//默认使用视图解析器拼接前缀后缀进行页面跳转
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>main页面</title>
</head>
<body>
<p>main...</p>
</body>
</html>
请求转发->twoAction->otherAction->admin/main.jsp,请求路径的url:http://localhost:8080/two
重定向跳转代码实现
@RequestMapping("/three")
public String three(){
System.out.println("这是重定向页面...");
//redirect: 这组字符串可以屏蔽前缀后缀的拼接,实现重定向跳转
return "redirect:/admin/main.jsp";
}
@RequestMapping("/four")
public String four(){
System.out.println("这是重定向");
return "redirect:/other";
}
注意地址栏的变化
3.重定向页面 : http://localhost:8080/admin/main.jsp
4.重定向: http://localhost:8080/other
