本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名原文链接~~~

Struts2(3)之Action相关

微信搜索 zze_coding 或扫描 👉 二维码关注我的微信公众号获取更多资源推送:


Action 类的几种类型

Struts2 的 Action 类有如下几种:

POJO

Action 类可以是一个 POJO(简单的 Java 类),例:

public class HelloAction1 {    public String execute() {
        System.out.println("hello Struts2");
        return "hello";
    }
}

实现 Action 接口

可以实现 com.opensymphony.xwork2.Action 接口标识一个类是 Action 类,并且在该接口中已经定义了一些常用逻辑视图名称常量。例:

import com.opensymphony.xwork2.Action;

/**
 * 实现接口 Action
 * Action 接口中提供了五个逻辑视图名称常量:
 * public static final String SUCCESS = "success";
 * public static final String NONE = "none";
 * public static final String ERROR = "error";
 * public static final String INPUT = "input";
 * public static final String LOGIN = "login";
 */
public class HelloAction2 implements Action {
    @Override
    public String execute() throws Exception {
        System.out.println("hello Struts2");
        return SUCCESS;
    }
}

继承 ActionSupport 类(推荐)

还可以通过继承 com.opensymphony.xwork2.ActionSupport 类,例:

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction3 extends ActionSupport {
    @Override
    public String execute() throws Exception {
        System.out.println("hello Struts2");
        return super.execute();
    }
}

Action 的访问

要让一个 Action 类能够通过 Http 请求访问到需要进行相关配置,Action 的访问配置也有三种方式,看如下示例。

如现有如下 Action 类:

// com.zze.action.TestAction
package com.zze.action;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport {
    public String save() {
        System.out.println("from save");
        return NONE;
    }

    public String delete(){
        System.out.println("from delete");
        return NONE;
    }

    public String select(){
        System.out.println("from select");
        return NONE;
    }

    public String update(){
        System.out.println("from update");
        return NONE;
    }
}

method 配置

<!-- struts.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="test" extends="struts-default" namespace="/">
        <!--localhost:8080/save-->
        <action name="save" class="com.zze.action.TestAction" method="save"></action>
        <!--localhost:8080/delete-->
        <action name="delete" class="com.zze.action.TestAction" method="delete"></action>
        <!--localhost:8080/select-->
        <action name="select" class="com.zze.action.TestAction" method="select"></action>
        <!--localhost:8080/update-->
        <action name="update" class="com.zze.action.TestAction" method="update"></action>
    </package>
</struts>

通配符配置

<!-- struts.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="test" extends="struts-default" namespace="/">
        <!--localhost:8080/save-->
        <!--localhost:8080/delete-->
        <!--localhost:8080/update-->
        <!--localhost:8080/select-->
        <!--method:{1} 代表 name 中第一个通配符的取值-->
        <action name="*" class="com.zze.action.TestAction" method="{1}"></action>

        <!--localhost:8080/Test_save-->
        <!--localhost:8080/Test_delete-->
        <!--localhost:8080/Test_update-->
        <!--localhost:8080/Test_select-->
        <!--通配符更抽象写法-->
        <!--<action name="*_*" class="com.zze.action.{1}Action" method="{2}"></action>-->
    </package>
</struts>

动态方法访问

<!-- struts.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <!--
    设置常量,开启动态方法访问
    开启之后可通过 <host>:<port>/<actionName>!<methodName> 访问:
    host : ip
    port : 端口
    actionName : action 名称
    methodName : 要访问的方法名称
    -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
    <package name="test" extends="struts-default" namespace="/">
        <!--localhost:8080/test!save-->
        <!--localhost:8080/test!delete-->
        <!--localhost:8080/test!select-->
        <!--localhost:8080/test!delete-->
        <action name="test" class="com.zze.action.TestAction" method="{1}"></action>
    </package>
</struts>
# Struts2  

如果这篇文章对您有帮助,可点击下方链接分享给你的朋友们😋,如果遇到问题欢迎评论、留言~~~😇

评论

公众号:zze_coding

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×