动态action的基本配置及实现
来源:互联网 发布:3000 左右手机 知乎 编辑:程序博客网 时间:2024/06/14 23:31
动态action是指一个action中实现多个方法,用户能够请求调用action中的某个方法。
动态action有三种实现方法,分别为:
1、请求URL为actionName!methodName形式
该方法要想开启动态action功能,必须在struts.xml中配置常量:struts.enable.DynamicMethodInvocation = true,如下所示:
<struts>
<constant name="struts.enable.DynamicMethodInvocation"value="true" />
</struts>
该方法的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache SoftwareFoundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<packagename="HelloWorld" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<actionname="HelloWorld" class="com.struts2.test.HelloWorld">
<resultname="add">/webpage/add.jsp</result>
<resultname="update">/webpage/update.jsp</result>
</action>
</package>
<constantname="struts.enable.DynamicMethodInvocation" value="true"/>
</struts>
示例代码如下:
package com.struts2.test;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
private static final longserialVersionUID = 1L;
private String message;
public StringgetMessage(){
return message;
}
public String update()throws Exception{
message ="update data!";
return"update";
}
public String add() throwsException{
message = "adddata!";
return"add";
}
}
访问URL形式为:http://localhost:8080/StrutsTest/HelloWorld!update。
注意:在struts 2.5及以上版本中为了提升安全性,该种实现方法须在struts package节点下配置:<global-allowed-methods>regex:.*</global-allowed-methods>,否则会报HTTP Status 404 - Method XXX for action XXX is not allowed!错误。
2、指定action的method属性
该方法是在package节点下的action节点中使用method来实现,配置示例如下红色部分:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache SoftwareFoundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<packagename="HelloWorld" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<actionname="HelloWorld" class="com.struts2.test.HelloWorld">
<resultname="add">/webpage/add.jsp</result>
<resultname="update">/webpage/update.jsp</result>
</action>
<actionname="HelloWorldUpdate" class="com.struts2.test.HelloWorld"method="update">
<resultname="update">/webpage/HelloWorld.jsp</result>
</action>
</package>
<constantname="struts.enable.DynamicMethodInvocation" value="true"/>
</struts>
上文红色部分配置了名称为HelloWorldUpdate的action映射到类com.struts2.test.HelloWorld中的update方法上。
访问的URL形式为:http://localhost:8080/StrutsTest/HelloWorldUpdate。
3、使用通配符
仔细看上面struts.xml中两个action的定义,可以发现他们除了name和method属性不同以外,其余的都一样,这种定义相当的冗余,为了解决这种类型的问题,Struts2提供了通配符定义方式。
在配置<action/>元素时,需要指定name、class、method等属性,这3个属性都支持通配符,在使用通配符定义Action的name属性时,相当于一个元素action定义多个逻辑Action。以上的action配置可以更改为:
<?xmlversion="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache SoftwareFoundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="HelloWorld"extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<actionname="*Action" class="com.struts2.test.HelloWorld"method="{1}">
<resultname="{1}">/webpage/{1}.jsp</result>
</action>
</package>
<constant name="struts.enable.DynamicMethodInvocation"value="true" />
</struts>
{1}表示为第一个*,也可能使用多个*,{N}表示第N个*。
代码示例如下:
package com.struts2.test;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
private static final longserialVersionUID = 1L;
private String message;
public StringgetMessage(){
return message;
}
public String update()throws Exception{
message ="update data!";
return"update";
}
public String add() throwsException{
message = "adddata!";
return"add";
}
}
注意:在struts 2.5及以上版本中为了提升安全性,该种实现方法须在struts package节点下配置:<global-allowed-methods>regex:.*</global-allowed-methods>,否则会报HTTP Status 404 - There is no Action mapped for namespace [/] andaction name [updateAction] associated with context path [/StrutsTest]错误。
访问的URL形式为:http://localhost:8080/StrutsTest/updateAction。
- 动态action的基本配置及实现
- action的基本配置
- Struts2的动态Action实现
- chapter2 详解Action的基本配置
- Struts2之Action的基本配置
- spring+ibatis【基本配置及动态sql】
- Struts2-基本action配置
- Struts2学习---基本配置,action,动态方法调用,action接收参数
- Struts2的Action配置及使用
- 动态Action跳转实现
- SSH整合中为获取表单对象Action类实现的接口及拦截器配置
- 动态表的基本实现
- 基本Action类以及配置通用Action
- struts1 与struts2 Action的配置实现
- Struct2 helloworld及基本action
- struts1.x基本action的配置与使用
- struts1.x基本action的配置与使用
- Struts基本用法<一>:MVC,Action的配置
- 电话号码上传下载流量的hadoop代码简单实现
- 分类问题中的数据不平衡问题
- 麻将项目开发--bug mj157
- android 编写json 数据
- PopupWindow(自定义)
- 动态action的基本配置及实现
- 项目管理心得:一个项目经理的个人体会、经验总结
- i2c总线2
- SpringMVC 幂等请求
- 将字符串转成可执行的代码
- POJ 1055 BULK MAILING 笔记
- 浅谈HTTP的Content-Type
- win7序列号,Windows7 通用激活序列号
- getLocationInWindow和getLocationOnScreen的区别