- 浏览: 471291 次
- 性别:
- 来自: 武汉
文章分类
最新评论
-
excellent95:
谢楼主分享,确实是CSS图片问题
Struts2判断页面是POST方式提交还是GET方式提交 -
wangluo2:
javaBase那个包显示有问题啊pom文件那一行报错了
微信公众平台开发实战(08) 基于地理信息的服务(LBS) -
andilyliao:
jd技术这么渣也好意思出书,真是醉了
《京东技术解密》试读--十年磨一剑 -
bitray:
刘强东作序多什么,他也不会什么技术,腾讯出来帮他还不是因为腾讯 ...
《京东技术解密》试读--十年磨一剑 -
come_for_dream:
想试试搞这个
《大型分布式网站架构设计与实践》试读
Action 接口与ActionSupport 类
1. Action 接口
所在包名: com.opensymphony.xwork2 描述: public interface Action All actions may implement this interface, which exposes the execute() method. However, as of XWork 1.1, this is not required and is only here to assist users. You are free to create POJOs that honor the same contract defined by this interface without actually implementing the interface. 简单翻译: 所有的 Action 都可以实现这个接口,该接口暴露了一个 execute() 方法。 然而,在 XWork 1.1 中,这些(实现接口)不是必须的,它只是用来协助用户的。你可以自由的创建一个 POJO 来替代实现这个接口,只需要实现一个 execute() 方法,而不需要真正的实现这个接口。
代码清单 1 : Action.java
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; }
我们可以看到,Action 接口非常简单,除了定义了5 个常量以外,还声明了一个方法。要实现这个接口,只需要实现这个方法即可。
代码清单 2 : LoginAction.java
package com.coderdream.action; import com.opensymphony.xwork2.Action; public class LoginAction implements Action { private String username ; private String password ; public String getUsername() { return username ; } public void setUsername(String username) { this .username = username; } public String getPassword() { return password ; } public void setPassword(String password) { this .password = password; } public String execute() { if ("aaa" .equalsIgnoreCase(username .trim()) && "123" .equalsIgnoreCase(password .trim())) { return SUCCESS ; } else { return INPUT ; } } }
Action 中常量的简单释义:
英文注释 | 中文释义 | |
static String ERROR | The action execution was a failure. Show an error view, possibly asking the user to retry entering data. | Action执行失败。显示一个错误的视图(页面),这个页面可以要求用户再次输入相关数据。 |
static String INPUT |
The action execution require more input in
order to succeed. This result is typically used if a form handling action has
been executed so as to provide defaults for a form. The form associated with the
handler should be shown to the end user. This result is also used if the given input params are invalid, meaning the user should try providing input again. |
Action的执行成功需要更多的输入。这个结果是一个典型的结果,它表明如果一个表单通过提供默认的表单来操作一个Action。这个表单会显示给最终用户。 这个结果也用于用户输入无效,意味着用户需要再次输入。 |
static String LOGIN | The action could not execute, since the user most was not logged in. The login view should be shown. | Action不能执行,因为用户没有登录。已登录的画面会被关闭。 |
static String NONE | The action execution was successful but do not show a view. This is useful for actions that are handling the view in another fashion like redirect. | Action执行成功,但是不会显示一个视图。通常产生这种情况的原因是被其他视图重定向了。 |
static String SUCCESS | The action execution was successful. Show result view to the end user. | Action执行成功。显示结果视图(页面)给用户。 |
在struts.xml 中result 就可以直接使用Action 接口中的常量了:
代码清单 3 : struts.xml
<?xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" > <struts > <package name = "loginTest" extends = "struts-default" > <action name = "login" class = "com.coderdream.action.LoginAction" > <result name = "success" >/loginSuc.jsp </result > <result name = "input" >/loginFail.jsp </result > </action > </package > </struts >
2. ActionSupport 类
public class ActionSupport extends Object implements Action , Validateable , ValidationAware , TextProvider , LocaleProvider , Serializable Provides a default implementation for the most common actions. See the documentation for all the interfaces this class implements for more detailed information. 为最常用的 Action 的提供一个默认的实现。
下面是ActionSupport 类的代码片段,从中我们可以看到该类提供了execute() 方法的空实现,直接返回success 。
代码清单 4 : ActionSupport.java
public class ActionSupport implements Action , Validateable , ValidationAware , TextProvider , LocaleProvider , Serializable { public String input() throws Exception { return INPUT ; } public String doDefault() throws Exception { return SUCCESS ; } public String execute() throws Exception { return SUCCESS ; } public void validate() { } public Object clone() throws CloneNotSupportedException { return super .clone(); } public void pause(String result) { } }
LoginAction 继承了com.opensymphony.xwork2.ActionSupport 类。 LoginAction 覆写了父类(ActionSupport )中的execute() 方法,当然也可以直接使用该类的默认实现,该默认实现返回SUCCESS ;
代码清单 5 : LoginAction.java
package com.coderdream.action; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private static final long serialVersionUID = -8047473058927169093L; private String username ; private String password ; public String getUsername() { return username ; } public void setUsername(String username) { this .username = username; } public String getPassword() { return password ; } public void setPassword(String password) { this .password = password; } public String execute() { if ("aaa" .equalsIgnoreCase(username .trim()) && "123" .equalsIgnoreCase(password .trim())) { return SUCCESS ; } else { return INPUT ; } } }
3. 三种方式小结
1、 Login1Action 是POJO ,包含一个execute() 方法,返回值为String :
代码清单 1 : Login1Action.java
public String execute() { if ("aaa".equalsIgnoreCase(username.trim()) && "123".equalsIgnoreCase(password.trim())) { return "success"; } else { return "failure"; } }
返回的多个值对应与struts.xml 中配置的返回结果集对应:
代码清单 2 : struts.xml 片段
<action name="login1" class="com.coderdream.action.Login1Action"> <result name="success">/login1Suc.jsp</result> <result name="failure">/login1Fail.jsp</result> </action>
2 、Login2Action 实现了接口com.opensymphony.xwork2.Action ;
在Login2Action 和struts.xml 中可直接使用Action 中定义的常量;
代码清单 3 : Action.java
static String ERROR The action execution was a failure. 返回错误 static String INPUT The action execution require more input in order to succeed. 返回一个输入页面 static String LOGIN The action could not execute, since the user most was not logged in. 、 返回登录页面 static String NONE The action execution was successful but do not show a view. 返回空(例如在上传下载时) static String SUCCESS The action execution was successful. 返回成功页面
返回值为Action 中定义的静态常量:
代码清单 4 : Login2Action.java
public String execute() { if ("aaa".equalsIgnoreCase(username.trim()) && "123".equalsIgnoreCase(password.trim())) { return SUCCESS; } else { return INPUT; } }
struts.xml 中配置的返回结果集为静态常量对应的字符串:
代码清单 5 : struts.xml 片段
<action name="login2" class="com.coderdream.action.Login2Action"> <result name="success">/login2Suc.jsp</result> <result name="input">/login2Fail.jsp</result> </action>
3 、Login3Action 继承自
com.opensymphony.xwork2.ActionSupport
该类实现了接口com.opensymphony.xwork2.Action 接口;
(该类另外还实现了Validateable , ValidationAware, TextProvider,
LocaleProvider, Serializable 接口)
在Login3Action 的代码和struts.xml 中配置与Login2Action 一致;
4 、com.opensymphony.xwork2.ActionSupport 类有execute() 方法的实现,直接返回SUCCESS ,也就是说任何继承自ActionSupport 的类可以不覆写execute() 方法,永远返回成功。
4. 源代码
见 附件。
我的联系方式: 85337464
我的博客: http://coderdream.iteye.com
- 0201.Action接口与ActionSupport类.pdf (267.4 KB)
- 下载次数: 53
- ZB_S2_D0201.rar (11.5 KB)
- 下载次数: 27
发表评论
-
Maven Struts2 项目
2013-09-14 11:44 01、创建好Maven环境; 2、创建一个Maven工 ... -
【张冰Struts2学习笔记】0601.struts.xml配置详解之六 Action配置3 通配符
2010-11-26 15:19 2552struts.xml 配置详解之六 通配符 ... -
【张冰Struts2学习笔记】0502.struts.xml配置详解之五 Action配置2
2010-11-26 14:28 1897struts.xml 配置详解之五 Action ... -
【张冰Struts2学习笔记】0501.struts.xml配置详解之四 Action配置1
2010-11-26 13:10 1878struts.xml 配置详解之四 Action 配置 ... -
【张冰Struts2学习笔记】0403.struts.xml配置详解之三 拦截器配置
2010-11-25 19:03 3318struts.xml 配置详解之三 拦截器配置 ... -
【张冰Struts2学习笔记】0402.struts.xml配置详解之二 名称空间 包含
2010-11-25 18:59 1893struts.xml 配置详解之二 名称空间 包含 ... -
【张冰Struts2学习笔记】0401_struts.xml配置详解之一
2010-11-25 13:56 2295struts.xml 配置详解之一 Bean ... -
【张冰Struts2学习笔记】0302.Struts2的主要配置文件及优先级
2010-11-20 16:33 1678Struts2 的主要配置文件及优先级 ... -
【张冰Struts2学习笔记】0301_struts.properties 配置详解(表格版)
2010-11-17 15:23 1571注:附件pdf中有35个属性! ... -
【张冰Struts2学习笔记】0202_Struts2的三种接收用户输入的方法
2010-11-17 12:32 2038Struts2的三种接收用户输入的方法 ... -
【张冰Struts2学习笔记】0102_第一个Struts2实例
2010-11-14 19:17 2969基于Struts2 的简单登录项目 ... -
【张冰Struts2学习笔记】0101_Struts1与Struts2的不同点
2010-11-14 14:12 1377Struts1与Struts2的不同点 ... -
基于Struts2的Web应用程序所需要的最少类库
2010-08-30 16:17 1869Struts 2.0.11版: 文件名 ... -
Struts2判断页面是POST方式提交还是GET方式提交
2010-06-03 11:06 9115在项目开发过程中,发现有个页面会提交两次,一直没有找到原因,后 ... -
Struts2的一个问题!no found ApplicationContextAware
2009-06-15 16:39 1704问题: Caused by: java.lang.NoC ... -
struts2的select用法
2009-02-11 09:59 66461、在页面对应的Action中有typeBase属性,改变下拉 ... -
子页面刷新父窗口
2009-02-09 11:50 3118下面的例子是一个子窗体调用方法改变父窗体 主页面 cc.ht ... -
Struts2结合JavaScript控制单选框的状态
2008-12-26 11:48 2055需求: 某个页面有3种操作,新增、修改和删除,其中某个字段为只 ... -
《Struts2深入详解》笔记--第三章 由“登录开始”
2008-12-02 19:36 17191、使用领域对象来接收用户输入 【工程1:sx_ch ... -
Struts2使用经验
2008-11-27 14:35 1412、显示某个对象的某个属性值: 两种方式: ${enterpri ...
相关推荐
NULL 博文链接:https://coderdream.iteye.com/blog/811262
NULL 博文链接:https://coderdream.iteye.com/blog/811139
NULL 博文链接:https://coderdream.iteye.com/blog/813784
Struts2_张冰_视频教程课件_PPT,张冰配套教学视频课件。
张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)
张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)
张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)
张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)
张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)
张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)张冰struts原代码(全部1-6)
张冰主讲的struts2 ppt 比较详细的一份ppt
struts2的介绍,struts框架结构、控制器组件、标签库以及详细的配置文件描述
张冰主讲的Struts2的PPT,主要介绍Struts2和Struts1的区别,还介绍了简单的登录功能的实现。建议初学者看,可以让你从宏观上把握Struts2.
我主要参考了《深入浅出Struts2》、《Struts2.0中文教程》以及张冰老师的视频教程,这里借机很感谢他们,大家可以在网上收到这些资料,当然可以向我索要,我会很乐意给大家的。当然我不认为这是最好、最全的学习...
张冰_struts2_PPT 2009 ...............
C面向对象程序设计张冰PPT学习教案.pptx
该文档是我看张冰老师的整合SSH视频的心得,我自己好不容易整合好了,将详细的步骤记录了下来,和大家分享,希望对大家有用!
阿里巴巴网信用交易模式研究_张冰晔
C++面向对象程序设计 张冰 课件、源代码