小编典典

如何从struts2的参数列表中排除Submit操作?

jsp

我正在尝试从参数列表中排除提交操作。以下是动作类。

@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警告警告:参数[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>


阅读 313

收藏
2020-06-08

共1个答案

小编典典

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

因为此参数位于默认情况下在其中引用您操作excludeParamsparams拦截器的列表中defaultStack

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

在此问题中,您将询问如何调用方法(而不是操作)。使用名称空间和动作名称将第一个动作与方法之间的差异映射到指定的URL。因此,要调用除动作以外的方法,应打开DMI。从2.3.16版本开始,Struts禁用了此选项。以下配置常量可用于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" />
2020-06-08