Java轻量级MVC框架Struts2 2.5详解(2)Action的定义、动态调用

本文详细介绍了Struts2框架中Action的三种定义方式:使用POJO类、实现Action接口、继承ActionSupport类,并展示了代码示例。同时,深入探讨了Action的动态调用方法,包括通过配置DMI和SMI实现的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Action的定义方式

 (1) struts2的action类定义的三种方法:
  1.使用公共的POJO类作为ACTION,提供公共的无参数的ACTION方法(不推荐使用)

  2.实现com.opensymphony.xwork2.Action接口,并覆写execute方法.
    该接口不仅提供了Action方法的声明,还提供了常用的逻辑视图名称如:
    2.1.public static final String SUCCESS="success";
    2.2.public static final String NONE="none";
    2.3.public static final String ERROR="error";
    2.4.public static final String INPUT="input";
    2.5.public static final String LOGIN="login";
    
  3.定义一个类,继承com.opensymphony.xwork2.ActionSupport类
    该类实现了多个接口定义的功能,如:
    3.1.1.public class ActionSupport implements Action, Validateable, ValidationAware, 
                           TextProvider, LocaleProvider, Serializable{}                           
    3.1.2.先创建一个继承ActionSupport的基本父类,如BaseAction extends ActionSupport   
    3.1.3.再创建业务的XXXAction extends BaseAction ,便于扩展和继承

(2)实现Action接口的代码示例

    1.java代码

package org.openbox.action;

import com.opensymphony.xwork2.Action;
public class ImplAction implements Action{

	@Override
	public String execute() throws Exception {
		System.out.println("实现Action接口,并覆盖execute()方法。SUCCESS...");
		return NONE;
	}
}

   2.struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
	<package name="actionpkg" namespace="/" extends="struts-default" >
		<action name="impl" class="org.openbox.action.ImplAction" method="execute" >
		</action>
	</package>
</struts>

(3)继承ActionSupport类的代码示例

     1.java代码

package org.openbox.action;
import com.opensymphony.xwork2.ActionSupport;

public class ExtAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	public String list() throws Exception {
		System.out.println("列类。");		
		return NONE;
	}	
	public String save() throws Exception {
		System.out.println("保存。");		
		return NONE;
	}
	public String delete() throws Exception {
		System.out.println("删除。");		
		return NONE;
	}
	public String update() throws Exception {
		System.out.println("更新。");		
		return NONE;
	}
}

     2.struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>	
	<package name="actionpkg" namespace="/" extends="struts-default" >
		<action name="impl" class="org.openbox.action.ImplAction" method="execute" > 
        </action>
    
    	<!-- 使用通配符-->
		<action name="*_*" class="org.openbox.action.{1}Action" method="{2}">
			<allowed-methods>list,save,delete,update</allowed-methods>
		</action>
	</package>
</struts>

 

二、Action的动态调用

(1)struts.xml文件的引用方式 

在项目的struts.xml文件中,可以分别引用每个包的struts-xxx.xml文件,方便管理action的注册

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
	<!-- 1.struts2默认常量修改,不能直接修改default.propertis中的常量 -->
	<constant name="struts.action.extension" value="action,do,neld,"></constant>
	<constant name="struts.serve.static.browserCache" value="false"></constant>
	<constant name="struts.devMode" value="true"></constant>

	<!-- 2.主struts.xml文件,引用所有包中的struts-xxx.xml文件 -->
	<include file="org/openbox/hello/struts-hello.xml"></include>
	<include file="org/openbox/action/struts-action.xml"></include>
</struts>

(2)struts2.5的动态调用有二种方式:
  方法1:
     1.1.在struts.xml文件中配置DMI为打开方式:<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
     1.2.并且在struts.xml文件中,在使用了通配符的Action标签中配置:<allowed-methods>list,save,delete,update</allowed-methods> ,这是为安全起见,将方法名都列在其中。

  1.主配置文件struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
	<!-- 1.struts2默认常量修改,不能直接修改default.propertis中的常量 -->
	<constant name="struts.action.extension" value="action,do,neld,"></constant>
	<constant name="struts.serve.static.browserCache" value="false"></constant>
	<constant name="struts.devMode" value="true"></constant>
	<!-- 1.2.使用通配符,在struts2.5中需要配置以下,并且在Action中加入<allowed-methods>list,save,delete,update</allowed-methods> -->
	<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

	<!-- 2.主struts.xml文件,引用所有包中的struts-xxx.xml文件 -->
	<include file="org/openbox/hello/struts-hello.xml"></include>
	<include file="org/openbox/action/struts-action.xml"></include>
</struts>

2.包配置文件struts-action.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
	<!-- 1.为每一个包创建一个单独的struts-xxx.xml文件,方便主struts.xml文件引用 -->
	<!-- 2.在一个Action中,有多个方法时,如何调用:使用*通配符,{1}表示第1个*的参数:注意类名首字母大写, *的具体值在URL地址栏中输入-->			
		<action name="*_*" class="org.openbox.action.{1}Action" method="{2}">
			<allowed-methods>list,save,delete,update</allowed-methods>
		</action>
	</package>
</struts>


 方法2:
      2.1.SMI关闭,这个时候通过默认的通配符来调用任何的Action方法都被允许。
      2.2.SMI的关闭设置方法:<package strict-method-invocation="false">…</package>

    1.主配置文件struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
	<!-- 1.struts2默认常量修改,不能直接修改default.propertis中的常量 -->
	<constant name="struts.action.extension" value="action,do,neld,"></constant>
	<constant name="struts.serve.static.browserCache" value="false"></constant>
	<constant name="struts.devMode" value="true"></constant>
	<!-- 1.2.使用通配符,在struts2.5中需要配置以下,并且在Action中加入<allowed-methods>list,save,delete,update</allowed-methods> -->
	<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

	<!-- 2.主struts.xml文件,引用所有包中的struts-xxx.xml文件 -->
	<include file="org/openbox/hello/struts-hello.xml"></include>
	<include file="org/openbox/action/struts-action.xml"></include>
</struts>

    2.包配置文件struts-action.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
	<!-- 1.为每一个包创建一个单独的struts-xxx.xml文件,方便主struts.xml文件引用 -->	
	<!-- 2.使用通配符,动态谳用action方法 -->
	<package name="hellopkg" namespace="/" extends="struts-default" strict-method-invocation="false">
		<action name="*_*" class="org.openbox.hello.{1}World" method="{2}">
			<result name="hello">
				<param name="location">/WEB-INF/views/hello.jsp</param>
			</result>
		</action>
	</package>
</struts>

 

 

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="helloworld" class="com.mytest.HelloWorldAction"> <result> /result.jsp </result> </action> </package> <package name="LoginForm" extends="struts-default"> <action name="login" class="com.mytest.LoginAction" method="execute"> <result> /login.jsp </result> </action> </package> </struts> <!--1.使用 struts2.5.16 版本 2.lib 文件夹下放置:工程所需jar包 3.xml标签库为远程获取,路径:http://struts.apache.org/dtds/struts-2.5.dtd 可设置为本地【xml输入语法快捷提示】,就不用远程获取了:window-->preference-->输入Catalog-->xml下的Catalog-->Add-->location:解压缩struts-core-2.5.16.jar 后,文件struts-2.5.dtd文件路径。 4.设置开发者模式: <constant name="struts.devMode" value="true" /> <constant name="struts.i18n.encoding" value="utf-8" /> 每次HTTP请求系统都重新加载资源文件,有助于开发 5.struts配置文件改动后,是否重新加载 <constant name="struts.configuration.xml.reload" value="true" /> 6.查看源码:Build path 后的类库中,奶瓶图标找到struts-core-2.5.16.jar 右键-->properties-->java Source Attachment-->External location :源码路径 7.查看文档API:Build path 后的类库中,奶瓶图标找到struts-core-2.5.16.jar 右键-->properties-->javadoc location :输入网址 或选择源码DOC目录 8.拦截器:web.xml 配置拦截器<filter> struts2.5的filter-class 与struts2.5以前版本有所不同 <!-- 浏览器访问 http://localhost:8080/MyWeb/helloworld --> --> <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置核心拦截器 --> <filter> <filter-name>struts2</filter-name> <!-- Filter的实现类 struts2.5 --> <filter-class> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <!-- 拦截所有的url --> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值