一、实现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. 转:为什么在定义hashcode时要使用31这个数呢?

    散列计算就是计算元素应该放在数组的哪个元素里.准确的说是放到哪个链表里面.按照Java的规则,如果你要想将一个对象放入HashMap中,你的对象的类必须提供hashcode方法,返回一个整数值.比如S ...

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

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

  3. windows 安装elk日志系统

    1.前往https://www.elastic.co官网下载对应的elasticsearch .kibana和logstash他们的版本号一致. 2.elasticsearch 解压后前往bin文件下 ...

  4. 【vijos】P1659 河蟹王国

    [算法]线段树 [题解]区间加上同一个数+区间查询最大值.注意和谐值可以是负数,初始化ans为负无穷大. #include<cstdio> #include<algorithm> ...

  5. 【BZOJ】2679: [Usaco2012 Open]Balanced Cow Subsets

    [算法]折半搜索+数学计数 [题意]给定n个数(n<=20),定义一种方案为选择若干个数,这些数可以分成两个和相等的集合(不同划分方式算一种),求方案数(数字不同即方案不同). [题解] 考虑直 ...

  6. poj 2762 tarjan缩点+拓扑序

    2013-09-08 10:00 var m, n :longint; t :longint; f, last :..] of longint; pre, other :..] of longint; ...

  7. linux系统引导流程

    1.固件:硬件和软件结合.加电自检是我们按下电源会检测硬件像cpu.内存.网卡等信息.(从硬件层次检测硬件是否可用) 固件设置实例:设置时间 软件时间和硬件时间: [root@VM_0_12_cent ...

  8. mysql where/having区别

    mysql> select 2-1 as a,password from mysql.user where user='root' having a>0; +---+----------- ...

  9. Eclipse中SVN更改连接用户

    Eclipse中安装了SVN插件,当连接到SVN服务器后,便无法从客户端更改连接帐号 百度一下,也就知道 查看Eclipse中使用的是什么SVN Interface,位置在windows > p ...

  10. python基础===python os.path模块

    os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...