专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

Struts2的结果处理方式(请求转发、重定向)

1、配置流程

(1)web.xml文件(在web目录下的WEB-INF目录里面):对过滤器进行配置(这里是统一的)

<?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>struts2</filter-name><!--不重复即可-->
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class><!--过滤器类名-->
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

(2)页面部分:用于测试是否能访问到目标页面

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

(3)struts.xml:在src目录下

(4)书写Action类:这里统一采用继承ActionSupport类的方式创建Action类

2、请求转发(默认方式)

(1)struts.xml文件配置:

<?xml version="1.0" encoding="UTF-8"?>
       <!DOCTYPE struts PUBLIC
                "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
                "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="hello" namespace="/hello" extends="struts-default">
        <action name="HelloAction" class="pers.zhb.hello.HelloAction" method="execute">
            <result name="success" type="dispatcher">/hello.jsp</result>
        </action>
    </package>
</struts>

result标签内部的属性被定义为请求转发的方式,请求转发为默认的访问方式,即使不配置该属性依旧以请求转发的方式访问hello.jsp

(2)Action类

public class HelloAction extends ActionSupport {
       public String execute() {
           return "success";
       }
}

(3)测试结果

96_1.png

3、重定向

(1)struts.xml配置文件

<struts>
    <package name="hello" namespace="/hello" extends="struts-default">
        <action name="HelloAction" class="pers.zhb.hello.HelloAction" method="execute">
            <result name="success" type="redirect">/hello.jsp</result>
        </action>
    </package>
</struts>

(2)测试结果:

96_2.png

4、从一个Action请求转发到另外一个Action

(1)struts.xml配置文件:

<struts>
    <package name="hello" namespace="/hello" extends="struts-default">
        <action name="HelloAction1" class="pers.zhb.hello.HelloAction1" method="execute">
            <result name="success" type="redirect">/hello.jsp</result>
        </action>
        <action name="HelloAction2" class="pers.zhb.hello.HelloAction2" method="execute">
            <result name="success" type="chain">
                <param name="actionName">HelloAction1</param>
                <param name="namespace">/hello</param>
            </result>
        </action>
    </package>
</struts>

其中package和param中的namespace属性的指定的值要保持一致,第一个param中的值为要请求转发到的Action的名称。

(2)创建两个Action:

Action1:

public class HelloAction1 extends ActionSupport {
       public String execute() {
           System.out.println("我是HelloAction1!");
           return "success";
       }
}

Action2:

public class HelloAction2 extends ActionSupport {
    public String execute() {
        System.out.println("我是HelloAction2!");
        return "success";
    }
}

(3)测试结果:

浏览器:

96_3.png

控制台:

96_4.png

(4)运行流程

96_5.png

5、从一个Action重定向到另外一个Action

(1)struts.xml配置文件:

<struts>
    <package name="hello" namespace="/hello" extends="struts-default">
        <action name="HelloAction1" class="pers.zhb.hello.HelloAction1" method="execute">
            <result name="success" type="dispatcher">/hello.jsp</result>
        </action>
        <action name="HelloAction2" class="pers.zhb.hello.HelloAction2" method="execute">
            <result name="success" type="redirectAction">
                <param name="actionName">HelloAction1</param>
                <param name="namespace">/hello</param>
            </result>
        </action>
    </package>
</struts>

(2)创建两个Action

(3)测试结果:

96_6.png

(4)访问流程:

96_7.png

文章永久链接:https://tech.souyunku.com/23127

未经允许不得转载:搜云库技术团队 » Struts2的结果处理方式(请求转发、重定向)

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们