zoukankan      html  css  js  c++  java
  • 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)测试结果

     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)测试结果:

    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)测试结果:

    浏览器:

    控制台:

     

     (4)运行流程

     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)测试结果:

     (4)访问流程:

  • 相关阅读:
    低代码:时代的选择
    AI+云原生,把卫星遥感虐的死去活来
    网络货运平台要智能,安全的数据底座少不了
    基于昇腾CANN的卡通图像生成可在线体验啦!十分钟带你了解CANN应用开发全流程
    什么是强化学习?
    高可用架构演进之单元化
    AOC萌新探索:搭建和体验在线AOC环境
    如何将知识引入机器学习模型提升泛化能力?
    零代码以“王者荣耀”为例解析设计七原则
    从零开始搭建前端脚手架
  • 原文地址:https://www.cnblogs.com/zhai1997/p/12266198.html
Copyright © 2011-2022 走看看