还是一样的操作

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.chentianyu</groupId>
  6. <artifactId>springmvc_requestGoTo</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>war</packaging>
  9. <properties>
  10. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  11. <maven.compiler.source>1.8</maven.compiler.source>
  12. <maven.compiler.target>1.8</maven.compiler.target>
  13. </properties>
  14. <dependencies>
  15. <dependency>
  16. <groupId>junit</groupId>
  17. <artifactId>junit</artifactId>
  18. <version>4.11</version>
  19. <scope>test</scope>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework</groupId>
  23. <artifactId>spring-webmvc</artifactId>
  24. <version>5.3.16</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>javax.servlet</groupId>
  28. <artifactId>javax.servlet-api</artifactId>
  29. <version>4.0.1</version>
  30. </dependency>
  31. </dependencies>
  32. <build>
  33. <resources>
  34. <resource>
  35. <directory>src/main/java</directory>
  36. <includes>
  37. <include>**/*.xml</include>
  38. <include>**/*.properties</include>
  39. </includes>
  40. </resource>
  41. <resource>
  42. <directory>src/main/resources</directory>
  43. <includes>
  44. <include>**/*.xml</include>
  45. <include>**/*.properties</include>
  46. </includes>
  47. </resource>
  48. </resources>
  49. </build>
  50. </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 = "";
    ....
}

当我们看到prefixsuffix的设置,是成员变量。我们看到还有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