4

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

下面是动作类:

@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页面:

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

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

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

以上@InterceptorRef类:

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

似乎不起作用。永远不会调用该方法postAction(),从而导致发出以下警告。

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


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

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。


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

4

1 回答 1

4

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

因为此参数在默认情况下引用您的操作的拦截器excludeParams列表中。paramsdefaultStack

如何调用 postAction() 方法,什么时候<s:submit>被点击?

在这个问题中,您询问如何调用方法(而不是操作)。动作和方法之间的区别首先是使用命名空间和动作名称映射到指定的 URL。因此,要调用动作以外的方法,您应该打开DMI。Struts,自 2.3.16 起关闭了这个选项。在 中使用以下配置常量struts.xml

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

并使用method属性而不是action属性。

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

正如我在这个答案中已经说过的那样。

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

<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您应该设置

<constant name="struts.devMode" value="false" />
于 2013-12-25T11:16:01.887 回答