@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
@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.
为什么不排除提交按钮的参数?当< s:submit>时,如何调用postAction()方法被点击了吗?
Why doesn’t it exclude the submit button’s parameter?
因为此参数位于defaultStack中params拦截器的excludeParams列表中,默认情况下会引用您的操作.
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,则可以选择启用操作:参数的前缀
<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" />
