#SpringMVC中的响应之返回值是void :转发、重定向、输出流@FDDLC

    科技2022-07-12  128

    本篇博客研究【当控制器方法返回值为void时,如何进行转发】

     

    依赖:spring-webmvc(及其相关依赖)、servlet-api(以便使用HttpServletRequest和HttpServletResponse)

     

    当控制器方法返回值为void的默认情况:

    请求结果:

    即默认会去寻找{【prefix】拼接【请求路径】拼接【suffix】}!

     

    一、转发:

     

    二、重定向:

     

    三、输出流:

     


    最后附上源码:

     

     

    项目结构:

     

    pom.xml的内容:

    <?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>org.example</groupId> <artifactId>P053_response_forward_redirect_writer</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>P053_response_forward_redirect_writer Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.8.RELEASE</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>servlet-api</artifactId> <version>6.0.36</version> </dependency> </dependencies> <build> <finalName>P053_response_forward_redirect_writer</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>

     

    web.xml的内容:

    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--关联springmvc.xml,让其中的配置生效!--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

     

    springmvc.xml的内容:

    <?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:mvc="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"> <mvc:component-scan base-package="cn.liuxingchang.controller"></mvc:component-scan> </beans>

     

    index.jsp的内容:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <body> <h3>test:返回值为void的默认情况</h3> <a href="test">test</a> <%----------------------%> <h3>test2:转发</h3> <a href="test2">test2</a> <%----------------------%> <h3>test3:重定向</h3> <a href="test3">test3</a> <%----------------------%> <h3>test4:输出流之print</h3> <a href="test4">test4</a> <%----------------------%> <h3>test5:输出流之write</h3> <a href="test5">test5</a> </body> </html>

     

    success.jsp的内容:

    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <h3>success</h3> </body> </html>

     

    new.jsp的内容:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h3>Hello!</h3> </body> </html>

     

    HelloController.java的内容:

    package cn.liuxingchang.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Controller public class HelloController { @RequestMapping("/test") public void test() { System.out.println("test"); } /*--------------------*/ @RequestMapping("/test2") public void test2(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("test2"); //注意:手动转发不经过视图解析器,前后缀需要自己写! request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request, response); } /*--------------------*/ @RequestMapping("/test3") public void test3(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("test3"); System.out.println(request.getContextPath()); //重定向无法访问WEB-INF下的内容! //重定向需要指出ContextPath! response.sendRedirect(request.getContextPath() + "/new.jsp"); } /*--------------------*/ @RequestMapping("/test4") public void test4(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setContentType("text/html;charset=UTF-8"); response.getWriter().print("<h1>Are you OK?</h1>"); response.getWriter().print("<h3>你好吗?</h3>"); } /*--------------------*/ @RequestMapping("/test5") public void test5(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("<h1>Are you OK?</h1>"); response.getWriter().write("<h3>你好吗?</h3>"); } }

     

    Processed: 0.012, SQL: 8