jsp - 如何从 Struts 2 中的参数列表中排除提交操作?

标签 jsp configuration struts2 dmi

我试图从参数列表中排除提交操作。
以下是 Action 类:

@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>
<s:submit> 生成的 HTML 代码如下所示。
<input type="submit" id="btnSubmit" name="action:postAction" value="Submit"/>
@InterceptorRef类(class)之上:
@InterceptorRef(value="params", params={"excludeParams", "action:postAction"})
似乎不起作用。方法postAction()永远不会调用导致发出以下警告。

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文件,到目前为止我有以下属性。
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>被点击?

最佳答案

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


因为这个参数在excludeParams params名单defaultStack 中的拦截器默认情况下会引用您的操作。

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


在这个问题中,您询问如何调用方法(而不是操作)。第一个操作和方法之间的区别使用命名空间和操作名称映射到指定的 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> 
正如我在 this 中已经说过的回答。
如果您不想使用 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" />

关于jsp - 如何从 Struts 2 中的参数列表中排除提交操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20764609/

相关文章:

java - dispatcher.forward() 和 <jsp :forward>? 有区别吗

java - 当用户访问网站时以编程方式编译java类

java - 当用户通过搜索引擎请求 jsp 时是否调用 struts2 操作

java - 自定义 Spring :form to render different value and tag content

java - 在 Struts Action 类中获取 Request 对象

javascript - 将隐藏的输入值获取到jquery变量

javascript - JS 中按下键和 home 不起作用

mysql - 如何在openempi中配置mysql?

c# - 安装期间强制覆盖 App.config

iis - web.config的System.webServer和System.web部分