struts2核心配置之Action
一、实现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的更多相关文章
- Struts2中配置默认Action
1.当访问的Action不存在时,页面会显示错误信息,可以通过配置默认Action处理用户异常的操作:2.配置方法: 在struts.xml文件中的<package>下添加如下内容: ...
- struts2核心配置之struts.xml
struts.xml -常量配置 -包配置 -包含配置 一.常量配置 struts2常量的配置通常采用三种方式: 1.在struts.xml中使用<constant>元素配置常量 < ...
- struts2核心配置之Result
result作用:在struts.xml中,使用<result>元素配置result逻辑视图和物理视图之间的映射 元素属性 属性 说明 是否必须 name 指定逻辑视图的名称(Action ...
- JavaWeb框架_Struts2_(二)----->Struts2的核心配置
2. Struts2的核心配置 2.1 配置Struts.xml文件 2.1.1 Struts.xml文件 Struts2框架的核心配置文件是Struts.xml,该文件主要用来配置Action和 ...
- JavaWeb_(Struts2框架)struts.xml核心配置、动态方法调用、结果集的处理
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
- Struts2基于注解的Action配置
使用注解来配置Action的最大好处就是可以实现零配置,但是事务都是有利有弊的,使用方便,维护起来就没那么方便了. 要使用注解方式,我们必须添加一个额外包:struts2-convention-plu ...
- 在Struts2中配置Action
在Struts2中配置Action <package>: 1.定义Action使用<package>标签下的<action>标签完成,一个<package&g ...
- Struts2学习---基本配置,action,动态方法调用,action接收参数
首先我们先来直接配置,然后再来讲原理: 第一步:jar包的引入: 我们可以到struts2的官网上下载: http://struts.apache.org/download.cgi#struts251 ...
- Struts2之配置文件中Action的详细配置
在Struts2之配置一文中,我们知道一个struts配置文件可以分为三部分:常量配置 包含其他配置文件的配置 Action配置 . 这其中 常量配置 和 包含其他配置文件的配置 二 ...
随机推荐
- 多条select语句的合并
select (') , (') , (select max(ssq) from DW_MARK_FXJS.F_GS_YCXJJDCSY@new_zgxt ) from dual 这样就不用傻傻的执行 ...
- java线程的常用方法和属性介绍
start() start方法是Thread 类的方法,在这个方法中会调用native方法(start0())来启动线程,为该线程分配资源. sleep() sleep方法有2个方法. public ...
- Lodash js数据操作库
https://lodash.com/docs/4.17.4
- 51Nod 1344 走格子 | 贪心
Input示例 5 1 -2 -1 3 4 Output示例 2 贪心 #include <bits/stdc++.h> using namespace std; typedef long ...
- zk-web
Ref:https://github.com/qiuxiafei/zk-web zk-web是一个用clojure with noir and boostrap写的Zookeeper WEB UI管理 ...
- scrapy学习笔记一
以前写爬虫都是直接手写获取response然后用正则匹配,被大佬鄙视之后现在决定开始学习scrapy 一.安装 pip install scrapy 二.创建项目 scrapy startprojec ...
- 命令行创建KVM虚拟机
qemu命令创建虚拟机: qemu-img create -f qcow2 /home/ubuntu.img 20G qemu-system-x86_64 -m 2048 -enable-kvm ...
- linux编程之信号
信号(signal)机制是UNIX系统中最为古老的进程之间的通信机制,它用在一个或多个进程之间传递异步信号,信号可以由各种异步事件产生,如: 键盘中断等等,在Linux 的shell 中,也可以使用信 ...
- (转)LSI SAS 1068E Raid CentOS 5.5 安装实例浪潮NF5220系列 分类: linux
新来了一批服务器,全都是清一色的国产服务器,相同的阵列卡,令人头疼的是Linux标准内核不包含该raid驱动,需要单独安装,如果是新升级内核,肯定需要编译进去该raid驱动.一.先把主板自带的驱动光盘 ...
- Linux 入门记录:十九、Linux 包管理工具 RPM
一.源代码管理 绝大多数开源软件都是直接以源代码形式发布的,一般会被打包为 tar.gz 的归档压缩文件.程序源代码需要编译为二进制可执行文件后才能够运行使用.源代码的基本编译流程为: ./confi ...