一、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>