首页 技术 正文
技术 2022年11月18日
0 收藏 399 点赞 2,622 浏览 2867 个字

一、使用Action的动态方法调用

    如果一个页面包含多个按钮,系统分别提交给Action的不同方法处理.此时可以采用DMI(Dynamic Method Invocation,动态方法调用)来处理这种请求。动态方法调用是指表单元素的Action并不是直接等于某个Action名字,而且以如下形式指定表单的Action属性:

<!-- action属性为actionNmae!methodName的形式
其中ActionName指定提交到哪个Action,而methodName指定提交到指定方法-->
action="ActionName!methodName"

 

其中一个按钮的代码如下:

<input type="submit" value="注册" onclick="regist();" />

上述代码中指定“注册”按钮被单击时触发regist函数,该函数的代码如下:

<script type="text/javascript">
function regist(){
targetForm = document.forms[0];
targetForm.action = "login!regist";
}
</script>

 

上述JavaScript代码改变了表单元素的Action属性,修改后的属性为login!regist,其实质就是将该表单提交给login Action的regist方法处理。

struts.xm中的配置:

<package name="lee" extends="struts-default">
<action name="login" class="org.crazyit.app.action.LoginAction">
<!-- 定义三个逻辑视图和物理资源之间的映射 -->
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>

 

注意:使用动态方法调用前,必须设置Struts2允许动态方法调用,需要设置struts.enable.DyanmicMethodInvocation常量为true。

二、指定method属性及使用通配符

    1)使用method属性

<action name="login" class="org.crazyit.app.action.LoginAction" method="execute">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
<action name="regist" class="org.crazyit.app.action.LoginAction" method="regist">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>

 

     对应的javaScript代码为:

 

function regist(){
targetForm = document.forms[0];
targetForm.action = "regist";
}

 

    2)使用通配符

在上述的struts.xml文件中,两个Action定义绝大部分相同,可见这种定义相当冗余,为了解决这个问题,Struts2可以使用通配符。

在配置<action ../>时,允许在指定name属性时使用通配符,然后在class、method属性及<result…/>子元素中使用{N}的形式来代表前面第N个星号所匹配的子串。

示例一:

<action name="*Action" class="org.crazyit.app.action.LoginAction" method="{1}">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>

 

例如,如果请求URL为loginAction.action,则调用LoginAction类的login方法,如果请求URL为registAction.action,则调用LoginAction类的regist方法。

示例二:

<action name="*_*" class="org.crazyit.app.action.{1}Action" method="{2}">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>

 

例如,如果请求URL为Book_save.action,则对应的处理类为BookAction,处理方法为save方法。

    3)对子元素使用通配符

Struts2不仅允许在class属性、name属性中使用表达式,还可以在<result…/>子元素中使用{N}表达式。

<action name="*" >
<result>/{1}.jsp</result>
</action>

 

上面的Action的定义可以匹配任意的Action,所用的用户请求都可通过Action来处理,因为没有指定class属性,该Action使用ActionSupport来作为处理类,而且因为该ActionSupport类的execute方法返回success字符串, 即该Action总是直接返回result中指定的JSP资源,JSP资源使用表达式来生成资源名。上面Action定义的含义是如果请求a.aciton,则进入a.jsp页面;如果请求b.action,则进入b.jsp页面……

通过这种方式,可以避免让浏览者直接访问系统的JSP页面,而是让Struts2框架来管理所有用户请求。

对于使用Struts2框架的应用而言,尽量不要让超级链接直接连接到某个视图资源,因为这种方式增加了额外的风险。推荐将所有请求都发送给Struts2框架,让框架来处理用户请求,即使只是简单的超级链接。

    4)关于因通配符带来的优先级的问题。

假设有URL为abcAction.action的请求,在struts.xml文件中配置了如下三个Action,它们的name的值分别为:abcAction、*Action和*,则这个请求将会被名为abcAction的Action处理。

假设有URL为defAction.action的请求,在struts.xml文件中配置了如下三个Action,它们的name的值分别为:abcAction、*Action和*,则*Action不会比*更优先匹配defAction.action的请求,而是先找到那个Action,就会由那个Action来处理用户请求。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900