如何从Struts 2的参数列表中排除Submit操作? [英] How to exclude the submit action from a list of parameters in Struts 2?

查看:72
本文介绍了如何从Struts 2的参数列表中排除Submit操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从参数列表中排除提交操作.

I'm trying to exclude a submit action from a parameter list.

以下是动作类:

@Namespace("/admin_side")
@ResultPath("/WEB-INF/content")
@InterceptorRefs({
    @InterceptorRef(value="validation", params={"excludeMethods", "test"}),
    @InterceptorRef(value="params", params={"excludeParams", "action:postAction"})})
public final class TestAction extends ActionSupport implements Serializable, ValidationAware
{
    private static final long serialVersionUID = 1L;
    private static final String SUCCESS = "success";

    private String name;

    @Action(value = "test", results = {
    @Result(name="success", location="Test.jsp"),
    @Result(name = "input", location = "Test.jsp")})
    public String test() throws Exception
    {
        System.out.println("name = "+name);
        return SUCCESS;
    }

    @Action(value = "postAction", results = {
    @Result(name="success", location="Test.jsp"),
    @Result(name = "input", location = "Test.jsp")})
    public String postAction()
    {
        System.out.println("Post invoked");
        System.out.println("name = "+name);
        return SUCCESS;
    }

    @RequiredStringValidator(type= ValidatorType.FIELD, message = "The name is required.")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void validate()
    {
        if(!hasErrors()&&name.length()<2)
        {
            addFieldError("name", "The name must compose of 2 letters.");
        }
    }
}

Test.jsp 页面:

The Test.jsp page:

<s:form namespace="/admin_side" action="test" validate="true">
    <s:textfield id="name" name="name" label="Name"/>
    <s:submit id="btnSubmit" name="btnSubmit" value="Submit" action="postAction"/>
</s:form>

< s:submit> 生成的HTML代码如下所示.

The generated HTML code for <s:submit> would be like as follows.

<input type="submit" id="btnSubmit" name="action:postAction" value="Submit"/>


该类上方的 @InterceptorRef :

@InterceptorRef(value="params", params={"excludeParams", "action:postAction"})

似乎不起作用.永远不会调用方法 postAction()导致发出以下警告.

doesn't seem to work. The method postAction() is never invoked causing the following warning to be issued.

2013年12月24日晚上10:49:16com.opensymphony.xwork2.interceptor.ParametersInterceptor警告警告:参数[action:postAction]在以下对象的excludeParams列表中模式!

Dec 24, 2013 10:49:16 PM com.opensymphony.xwork2.interceptor.ParametersInterceptor warn WARNING: Parameter [action:postAction] is on the excludeParams list of patterns!


到目前为止,在 struts.properties 文件中,我具有以下属性.


In the struts.properties file, I have the following properties until now.

struts.enable.DynamicMethodInvocation=false
struts.devMode=false
struts.ui.theme=simple

struts.convention.package.locators=actions
struts.convention.action.suffix=Controller
struts.convention.action.mapAllMatches=true

struts.convention.result.path=/WEB-INF/content //No need. It is the default.
struts.mapper.action.prefix.enabled=true

我正在使用Struts 2.3.16.

I'm using Struts 2.3.16.

为什么不排除提交"按钮的参数?当单击< s:submit> 时,如何调用 postAction()方法?

Why doesn't it exclude the submit button's parameter? How to invoke the postAction() method, when <s:submit> is clicked?

推荐答案

为什么不排除提交"按钮的参数?

Why doesn't it exclude the submit button's parameter?

因为此参数位于 defaultStack params 拦截器的 params 拦截器的 excludeParams 列表中,默认情况下会引用您的操作.

Because this parameter is in the excludeParams list of the params interceptor in the defaultStack which your action is referenced by default.

单击< s:submit> 时如何调用postAction()方法?

How to invoke the postAction() method, when <s:submit> is clicked?

在此问题中,您询问如何调用方法(而不是操作).使用名称空间和操作名称将第一个操作与方法之间的差异映射到指定的URL.因此,要调用除动作之外的其他方法,您应该打开 DMI .从2.3.16版本开始,Struts禁用了此选项.在 struts.xml 中使用的以下配置常量:

In this question you ask how to invoke a method (not an action). The difference between the action and method the first is mapped to a specified URL using a namespace and action name. So, to invoke a method other than an action you should turn DMI on. Struts, since 2.3.16 turned off this option. The following configuration constant to use in struts.xml:

<constant name="struts.enable.DynamicMethodInvocation" value="true"/>

,并使用 method 属性而不是 action 属性.

and use a method attribute instead of action attribute.

<s:form namespace="/admin_side" action="test">
  <s:submit value="Submit" method="postAction"/>
</s:form> 

正如我在答案中已经告诉过的.

as I already told in this answer.

如果您不想使用DMI,则可以选择启用参数的 action:前缀

If you don't want to use DMI, then you have an option to enable action: prefix to the parameter

<constant name="struts.mapper.action.prefix.enabled" value="true"/>

并使用映射到方法 postAction

<s:form namespace="/admin_side" action="test">
  <s:submit value="Submit" action="postAction"/>
</s:form> 

,并使用不带 params.excludeParams 的注释.

@InterceptorRef(value="defaultStack" params={"validation.excludeMethods", "test"})

action:postAction 参数在排除列表上的警告仍然存在,但仅在 struts.devMode = true 时显示.您不必担心,因为它会警告通过的 excludeParams 列表中的所有参数.要关闭 devMode ,您应该设置

The warning that action:postAction parameter is on exclude list still persist, but it appears only if the struts.devMode=true. You shouldn't worry about it because it warns all parameters from the excludeParams list that passed through. To turn off devMode you should set

<constant name="struts.devMode" value="false" />

这篇关于如何从Struts 2的参数列表中排除Submit操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆