`
fslingmo
  • 浏览: 26366 次
  • 性别: Icon_minigender_2
  • 来自: 厦门
社区版块
存档分类
最新评论

Action详解

 
阅读更多

在传统的MVC框架如Struts1、Apring等,Action都需要实现特定的接口。这些接口都是MVC框架定义的。实现MVC的接口会与MVC框架耦合。Struts2的Action要灵活得多,可以实现Struts2的接口,也可以不实现。

 

1.ActionSupport类

自定义Action一般直接继承ActionSupport类,并定义变量,覆盖execute()方法。变量的值会被Struts2通过setter方法自动赋值,execute中直接使用即可。execute()方法的返回值为配置在Struts.xml中的<result>配置。

 

 

import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
               private String account;
               private String password;
              public String execute(){
                   if(...){return ....}
                   return ...
              }          

}
 

 

 提示:ActionSupport中实现了其他的方法,例如数据校验等,继承ActionSupport的好处是可以直接使用数据校验Struts2集成的方法。

 

2.Action接口

自定义Action还可以直接实现Action接口。事实上,Struts2的ActionSupport类也实现自该接口。Action接口只定义了一个execute()主方法,以及几个常用的结果名称(success、none、error、input、login等)。编程中尽量使用这些预定义的结果名称。

 

Action接口代码如下

 

 

 

package com.opensymphony.xword2;
public interface Acion{
            public abstract String execute() throws Exception;//主方法
            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";
}

 

直接实现该接口以及execute()方法即可。

 

3.不继承任何类的Action

 

Struts2的Action并不一定要实现Action接口,任何的POJO都可以用做Action,只要这个Action具有public String execute()方法。如果Struts2发现Action类没有实现Action接口,会通过反射来调用execute()方法,例如(getter 、setter等)

 

package com.action;

public class LoginAction {
	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;
	}
}
 

 

4.Action的可执行方法

execute()是Action的默认方法。Struts2还可以执行Action的其他方法,只要这些方法没有参数,并返回String类型。这些方法也可以有Throws声明,也可以没有。Struts2会在运行时根据方法的特征判断是否是可执行方法(参数、返回值),并通过反射执行。

例如:

 

 

package com.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
	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;
	}

	@Override
	public String execute() throws Exception {
		if ("sling".equalsIgnoreCase(username) && "sling".equals(password)) {
			System.out.println("成功登录");
			return SUCCESS;
		}
		System.out.println("登录失败");
		return LOGIN;
	}

	public String login() throws Exception {
		return execute();
	}

	public String logout() {
		System.out.println("成功退出");
		return "logout";
	}
}
 

提示:login()与logout()并不是默认的可执行方法。可将其配置到struts.xml中,或者通过特定 的URL(例如login!login.action 、 login!logout.action等)直接执行这些非默认方法。

 

5.通过URL执行Action的方法

 

 

执行Action的非默认方法,例如logout(),可以使用action!method.action的URL形式访问,其中,action为struts.xml中配置的Action的名字,method为Action的方法名,中间用“!”隔开,例如:http://localhost:8080/struts2/loginPerson!logout.action将执行loginPerson的logout()方法。

 

6.将执行方法配置到Action

 

也可以把方法用method配置刀片Action中,省去!符号。这时的Action名称可以随便指定。可以为每个方法均定义一种Action名称,然后使用该Action名称对应的访问方式访问Action。这样的缺点是同一个Action需要重复配置多次。例如

 

 

<action name="login"  class="com.action.LoginAction" method="login">
	 	<result name="success">/newsuccess.jsp</result>
	 	<result name="login">/login.jsp</result>
</action>
<action  name="logout"class="com.action.LoginAction"method="logout">
	<result name="logout">/logout.jsp</result>
</action>

 

注意配置的都是LoginAction,只是name属性、method属性不一样,请求login.action时会执行login()方法,请求logout.action时会执行logout()方法。或者直接用通配符配置。

 

<action name="*person" class="com.action.LoginAction" method="{1}">
	 	<result name="success">/newsuccess.jsp</result>
	 	<result name="{1}">/{1}.jsp</result>
 </action>
 
分享到:
评论

相关推荐

    2.2 JFinal控制器详解_Action详解.mp4

    2.2 JFinal控制器详解_Action详解.mp4

    Java实训教程 Java软件开发实战 Java开发框架介绍 struts2_2_Action详解 共43页.pptx

    #### 二、Struts2框架Action详解 在Struts2框架中,Action是核心组件之一,主要负责接收用户的请求并进行相应的业务逻辑处理,最后返回一个结果给用户。下面将详细介绍Action的相关知识点: ##### 2.1 Struts2 ...

    Struts2之Action详解2

    在本篇文章中,我们将深入探讨Struts2中的Action及其相关知识点。 Action是Struts2框架中处理用户请求的核心实体,它是业务逻辑与视图层之间的桥梁。一个Action类通常对应一个特定的用户操作或者业务场景。Action类...

    Struts 2 配置Action详解

    Struts 2 配置Action详解 Struts 2 框架中,配置Action是实现用户请求和Action类之间的对应关系的关键步骤。配置Action主要包括Action基本配置、配置逻辑视图和物理视图之间的映射关系、动态方法调用等几个方面。 ...

    Struts2的配置 struts.xml Action详解

    Struts2 的配置 struts.xml Action 详解 Struts2 框架是一个基于 Java 语言的 Web 应用程序框架,它提供了一个灵活的架构,允许开发者快速构建基于 Web 的应用程序。在 Struts2 框架中,struts.xml 文件扮演着核心...

    jsp自动调用action

    ### jsp自动调用action详解 #### 一、概述 在Web开发中,尤其是在使用Java Server Pages (JSP)与Struts框架时,有时需要在用户访问某个初始页面(如欢迎页)时自动触发一系列后端操作(如执行某个Action)。这种...

    Intent.action_大全

    #### 二、Intent Action详解 1. **ADD_SHORTCUT_ACTION** - **描述**:此Action用于在系统中添加一个快捷方式。 - **Action**:"android.intent.action.ADD_SHORTCUT" - **示例**:开发者可以利用此Action创建...

    三大框架详解

    **Action详解和配置** 1. **不继承任何类的Action**:可以自定义Action类,不依赖任何特定基类。 2. **实现Action接口**:直接实现Struts2的Action接口,定义execute()方法。 3. **继承ActionSupport类**:推荐做法...

    Struts2的Action讲解

    ### Struts2的Action详解 #### 一、引言 在Web开发领域,Struts框架因其优秀的MVC架构设计而广受欢迎。随着技术的发展,Struts框架也经历了从1.x到2.0的重大升级。本文将重点介绍Struts 2.0中的核心组件——Action...

    spring管理struts的action的代码

    ### Spring管理Struts的Action详解 #### 一、Spring与Struts框架整合概述 在Java Web开发中,Spring和Struts是两个非常重要的框架。Spring框架主要负责业务逻辑层的管理,提供依赖注入(DI)和面向切面编程(AOP)...

    action in wxpython中文版

    《wxPython中的Action详解》 在Python的GUI编程领域,wxPython是一个强大的工具,它提供了丰富的控件和组件,使得开发者能够创建出美观且功能齐全的桌面应用。而在wxPython中,`Action`是一个重要的概念,它用于...

    react-native-circular-action-menu, 由CircularFloatingActionMenu启发的路径esque循环菜单.zip

    react-native-circular-action-menu, 由CircularFloatingActionMenu启发的路径esque循环菜单 react-native-circular-action-menu路径esque循环菜单由 CircularFloatingActionMenu 激发。安装npm i react-native-...

    Android system action

    ### Android System Action详解 在Android开发中,系统Action(或称为Intent Action)是开发者们经常用到的一个功能,它能够帮助我们实现应用间的交互与跳转。本文将围绕标题“Android system action”,对描述中...

    Delegate与Action.docx

    ### C#中的Delegate与Action详解 #### 背景与问题根源 对于C#学习者而言,理解并熟练掌握委托(Delegate)的概念及其在多线程环境中的应用至关重要。在开发过程中,经常会遇到需要在非主线程中更新UI的情况。由于...

    Zend Framework教程之动作的基类Zend_Controller_Action详解

    主要介绍了Zend Framework教程之动作的基类Zend_Controller_Action的用法,结合实例形式详细分析了动作的基类Zend_Controller_Action具体功能,使用方法与相关注意事项,需要的朋友可以参考下

    对pyqt5之menu和action的使用详解

    如下所示: exitAct = QAction(QIcon('exit.png'), '&Exit', self) exitAct.setShortcut('Ctrl+Q') exitAct.setStatusTip('Exit application') QAction is an abstraction for actionsperformed with a menubar, ...

    Java实训教程 Java软件开发实战 Java开发框架struts2介绍 共12个章节.rar

    struts2_2_Action详解 共43页.pptx struts2_3_配置参数详解 共47页.pptx struts2_4_OGNL 共71页.pptx struts2_5_标签 共113页.pptx struts2_6_国际化 共34页.pptx struts2_7_数据验证 共56页.pptx struts2_8_文件...

Global site tag (gtag.js) - Google Analytics