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配置 . 这其中 常量配置 和 包含其他配置文件的配置 二 ...
随机推荐
- UVA 1638 Pole Arrangement
https://vjudge.net/problem/UVA-1638 题意: n根长度分别为1,2,3,4……n的木棍 将这些木棍竖着排成一列 问从左边看能看到L根,从右边看能看到R根的方案数 将木 ...
- 洛谷2944 [USACO09MAR]地震损失2Earthquake Damage 2
https://www.luogu.org/problem/show?pid=2944 题目描述 Wisconsin has had an earthquake that has struck Far ...
- 【BZOJ】1031 [JSOI2007]字符加密Cipher
[算法]后缀数组 [题解]把数组复制一遍然后SA处理即可. 后缀数组 #include<cstdio> #include<algorithm> #include<cstr ...
- python dlib 面部轮廓实时检测
1.dlib 实现动态人脸检测及面部轮廓检测 模型下载连接 : http://dlib.net/files/ # coding:utf-8 import cv2 import os import dl ...
- error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'
error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System. ...
- poj 1298 The Hardest Problem Ever
题目链接:http://poj.org/problem?id=1298 题目大意:按照所给的顺序要求将输入的字符串进行排列. #include <iostream> #include &l ...
- linux内核启动分析(2)
-----以下内容为从网络上整理所得------ 主要介绍kernel_init线程(函数),这个线程在rest_init函数中被创建,kernel_init函数将完成设备驱动程序的初始化,并调用in ...
- 1003: FFF团的情侣活动--课程作业--找出N个数字中唯一出现奇数次的数
1003: FFF团的情侣活动 Time Limit: 1 Sec Memory Limit: 2 MB Description 圣诞节快到了,Water作为大FFF团团长,组织许多对情侣进行电影院 ...
- 学习1:python输入输出
1. 输出 >>> print "hello world" hello world >>> print 'hello world' hello ...
- 使用 Visual Studio 部署 .NET Core 应用 ——.Net Core 部署到Ubuntu 16.04
.Net Core 部署到Ubuntu 16.04 中的步骤 1.安装工具 1.apache 2..Net Core(dotnet-sdk-2.0) 3.Supervisor(进程管理工具,目的是服务 ...