JavaEE struts2.5的动态方法调用

来源:互联网 发布:淘宝客转换软件 编辑:程序博客网 时间:2024/06/05 00:08

当一个form表单中包含多个提交按钮时,系统需要调用Action的不同方法来处理用户请求,这就需要让同一个Action包含多个控制处理逻辑。

Action中默认使用execute()方法处理用户请求,struts2提供了DMI(Dynamic Method Invocation)技术,通过DMI可以调用同一个Action的不同方法。

一个例子

下面这个表单有两个提交按钮,而这个表单只由一个Action处理用户请求:
form

这个form表单的代码如下:

<!-- login.jsp -->  <s:form method="post" namespace="/zzw" action="login">    <s:textfield name="username" key="username"/>    <s:textfield name="password" key="password"/>    <s:submit key="login" method="login"/>    <s:submit key="register" method="register"/>  </s:form>

在两个<s:submit>标签中都有一个method属性,此属性将指定调用Action的哪个方法。

上面的form表单请求将交由下面这个Action处理:

// LoginAction.javapublic class LoginAction extends ActionSupport {    private String username;    private String password;    private static final long serialVersionUID = 1L;    public String getUsername() { return username; }    public String getPassword() { return password; }    public void setUsername(String u) { username=u; }    public void setPassword(String p) { password=p; }    public String login() throws Exception {        ServletActionContext.getRequest().setAttribute("message", "调用login方法");        return SUCCESS;    }    public String register() throws Exception {        ServletActionContext.getRequest().setAttribute("message", "调用register方法");        return SUCCESS;    }}

这里的登录和注册逻辑只是简单输出一条消息,具体的逻辑视具体情况而定。

最后还需要在struts.xml文件中配置此Action:

<!-- struts.xml --><struts>    <constant name="struts.custom.i18n.resources" value="message"/>    <constant name="struts.i18n.encoding" value="GBK"/>    <constant name="struts.devMode" value="true"/>    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>    <package name="zzw" extends="struts-default" namespace="/zzw">        <action name="login" class="com.zzw.action.LoginAction">            <result name="success">/result.jsp</result>        </action>    </package></struts>

使用DMI需要设置struts.enable.DynamicMethodInvocation常量为true

此时运行此Web应用,在登录页面点击登录/注册按钮,发现出现如下问题:
problem

原来是因为struts2.5为了提升安全性,添加了一个allowed-methods属性,此属性指定可被调用的方法。

因此需要修改struts.xml文件:

<!-- struts.xml --><struts>    <constant name="struts.custom.i18n.resources" value="message"/>    <constant name="struts.i18n.encoding" value="GBK"/>    <constant name="struts.devMode" value="true"/>    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>    <package name="zzw" extends="struts-default" namespace="/zzw">      <global-allowed-methods>login,register</global-allowed-methods>        <action name="login" class="com.zzw.action.LoginAction">            <result name="success">/result.jsp</result>        </action>    </package></struts>

现在运行此Web应用,点击登录页面的登录/注册按钮后发现Action调用了login()/register()方法。

另外,allowed-methods属性还可以使用正则表达式配置:

<global-allowed-methods>regex:.*</global-allowed-methods>

未解决的问题

上述程序可以正常使用struts2.5的DMI机制,但在开发过程中,也出现了以下几个问题:
1. 在struts.xml文件的action块中添加<allowed-methods>regex:.*</allowed-methods>元素(allowed-methods的局部配置)后,eclipse提示action不允许allowed-method元素;
2. 不论是在DMI中使用”!”(action!method),还是使用”*”(通配符),点击提交按钮后,调用的是Action的execute()方法,login()register()方法都没被调用。网上有说法是在package元素中设置strict-method-invocation属性值为false,但修改后还是没用。

参考的文章

  • 在struts2.5版本中使用DMI遇到问题
  • Struts2 2.5.2的套路
  • struts2:This method: login for action user is not allowed!
  • Action Configuration
0 0
原创粉丝点击