一、实现Action类

1、POJO实现(Plain Ordinary Java Object 简单的java对象)

public class User1 {
public String execute(){
System.out.println("Action1");
return "success";
}
}

  Action类不继承任何特殊类,不实现任何特殊接口,只要有一个公共的无参的构造函数(默认构造函数即可)和一个execute()方法

  execute()方法要求:public类型,返回值String,方法无参

2、实现Action接口

public class User2 implements Action {
public String execute() throws Exception {
System.out.println("Action2");
return "success";
}
}

Action接口具体代码

public interface 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 String execute() throws Exception;
}

  常量使用:execute()中return ”success“等同于return SUCCESS

3、继承ActionSupport类

public class User3 extends ActionSupport {
   private static final long serialVersionUID=1L;
@Override
public String execute() throws Exception {
System.out.println("Action3");
return SUCCESS;
}
}

  因为ActionSupport类实现了Serializable接口,所以继承的Action类需要声明变量 serialVersionUID

二、配置Action

Action元素属性

属性 说明 是否必须
name 标识Action,指定Action所处理请求的URL
class 指定Action对应的实现类
method 指定请求Action时的调用方法

如果没有指定class属性值,其默认值为com.opensymphony.xwork2.ActionSupport类,该类使用默认处理方法execute(),ActionSupport类中的execute()方法不会做任何处理,二十直接返回success值。

如果没有指定method属性,Action调用class的execute()方法。

三、使用通配符

<package name="test" namespace="/test" extends="struts-default">
<action name="ActionTest_*" class="com.test.pojo.TestAction2" method="{1}">
<result name="hello">/{}.jsp</result>
<result name="hi">/{}.jsp</result>
</action>
</package>

在上述代码中,当客户端发送/test/ActionTest_hello.action这样的请求时,<action>元素的name属性就被设置成ActionTest_hello,method就被设置成hello;,当客户端发送/test/ActionTest_hi.action这样的请求时,<action>元素的name属性就被设置成ActionTest_hi,method就被设置成hi。

结果:

其中method的属性值中的数字1匹配表示匹配第一个*。如果定义的Action名称为*_*,class属性为action.{1},method属性值为{2}。如果Action名称设置为*,则可以匹配所有Action。

struts2核心配置之Action的更多相关文章

  1. Struts2中配置默认Action

    1.当访问的Action不存在时,页面会显示错误信息,可以通过配置默认Action处理用户异常的操作:2.配置方法:    在struts.xml文件中的<package>下添加如下内容: ...

  2. struts2核心配置之struts.xml

    struts.xml -常量配置 -包配置 -包含配置 一.常量配置 struts2常量的配置通常采用三种方式: 1.在struts.xml中使用<constant>元素配置常量 < ...

  3. struts2核心配置之Result

    result作用:在struts.xml中,使用<result>元素配置result逻辑视图和物理视图之间的映射 元素属性 属性 说明 是否必须 name 指定逻辑视图的名称(Action ...

  4. JavaWeb框架_Struts2_(二)----->Struts2的核心配置

    2.  Struts2的核心配置 2.1  配置Struts.xml文件 2.1.1 Struts.xml文件 Struts2框架的核心配置文件是Struts.xml,该文件主要用来配置Action和 ...

  5. JavaWeb_(Struts2框架)struts.xml核心配置、动态方法调用、结果集的处理

    此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...

  6. Struts2基于注解的Action配置

    使用注解来配置Action的最大好处就是可以实现零配置,但是事务都是有利有弊的,使用方便,维护起来就没那么方便了. 要使用注解方式,我们必须添加一个额外包:struts2-convention-plu ...

  7. 在Struts2中配置Action

    在Struts2中配置Action <package>: 1.定义Action使用<package>标签下的<action>标签完成,一个<package&g ...

  8. Struts2学习---基本配置,action,动态方法调用,action接收参数

    首先我们先来直接配置,然后再来讲原理: 第一步:jar包的引入: 我们可以到struts2的官网上下载: http://struts.apache.org/download.cgi#struts251 ...

  9. Struts2之配置文件中Action的详细配置

    在Struts2之配置一文中,我们知道一个struts配置文件可以分为三部分:常量配置    包含其他配置文件的配置    Action配置  . 这其中 常量配置  和 包含其他配置文件的配置  二 ...

随机推荐

  1. SpringMVC中 Session的使用情况

    在SpringMVC中,使用Session可以用通过两种方式 1.servlet-api 即HttpSession session.setAttritute(),session.getAttribut ...

  2. intellij idea 中添加maven远程仓库

    在intellij idea 中配置好maven后 是这样的 如果加载失败,则需要自定义远程仓库,这里以阿里maven仓库为例, 在项目的pom文件中添加以下配置 <repositories&g ...

  3. (转)Linux下使Shell 命令脱离终端在后台运行

    转自: http://www.linuxidc.com/Linux/2011-05/35723.htm 方法如下: (1)输入命令: nohup 你的shell命令 & (2)回车,使终端回到 ...

  4. 从无到有搭建SSM框架

    框架   https://www.cnblogs.com/xiaoL/p/7753130.html log4j配置详解    https://www.cnblogs.com/SummerinShire ...

  5. [POI2004] SZP (贪心+拓扑排序)

    [问题描述] Byteotian 中央情报局(BIA) 雇佣了许多特工. 他们每个人的工作就是监视 另一名特工. Byteasar 国王需要进行一次秘密行动,所以他要挑选尽量多的信得过的特工. 但 是 ...

  6. Shuffle Cards(牛客第三场+splay)

    题目: 题意:将1~n的数进行m次操作,每次操作将第pi位到pi+si-1位的数字移到第一位,求最后的排列. 思路:现在还没不会写splay,在知道这是splay模板题后找了一波别人的模板,虽然过了, ...

  7. 大聊Python----quene队列

    当必须在多个线程之间安全地交换信息时,队列在线程编程中特别有用. class queue.Queue(maxsize=0)  # 先入先出class queue.LifoQueue(maxsize=0 ...

  8. [Leetcode Week12]Unique Paths

    Unique Paths 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths/description/ Description A ...

  9. linux编程之信号

    信号(signal)机制是UNIX系统中最为古老的进程之间的通信机制,它用在一个或多个进程之间传递异步信号,信号可以由各种异步事件产生,如: 键盘中断等等,在Linux 的shell 中,也可以使用信 ...

  10. 天气api接口

    python调用天气api接口: http://www.sojson.com/open/api/weather/json.shtml?city=北京 http://www.sojson.com/blo ...