在以下有关Struts动作类的代码中,
@Namespace("/admin_side") @ResultPath("/WEB-INF/content") // Default. public final class TestAction extends ActionSupport implements Serializable { private static final long serialVersionUID = 1L; private static final String SUCCESS = "success"; private String name; private String email; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override @Action(value="test",results={@Result(name="success",location="Test.jsp")}) public String execute() throws Exception { System.out.println("name = "+name); System.out.println("email = "+email); return SUCCESS; } // Some annotations to map this method to <s:submit> public String postAction() { System.out.println("postAction() invoked."); System.out.println("name = "+name); System.out.println("email = "+email); return SUCCESS; } }
我想像这样postAction()在给定的提交按钮上调用方法,
postAction()
<s:form action="test"> <s:textfield id="name" name="name" label="Enter your name"/> <s:textfield id="email" name="email" label="Enter your email"/> <s:submit value="Submit" method="postAction"/> </s:form>
我曾见过这样一些问题这一个,但是所有的人都在使用XML配置struts.xml。
struts.xml
如何 使用Conventional插件 使其成为可能?
您需要使用namespace属性,因为您的操作是用映射的@Namespace。
namespace
@Namespace
<s:form namespace="/admin_side" action="test"> <s:textfield id="name" name="name" label="Enter your name"/> <s:textfield id="email" name="email" label="Enter your email"/> <s:submit value="Submit" method="postAction"/> </s:form>
如果按照WW-4023的最新版本无法使用此方法,则可以直接调用方法
<s:url var="myUrl" namespace="/admin_side" action="test" method="postAction"/> <s:form action="%{#myUrl}"> <s:textfield id="name" name="name" label="Enter your name"/> <s:textfield id="email" name="email" label="Enter your email"/> <s:submit value="Submit"/> </s:form>
或配置params拦截器参数excludeParams以method从排除的参数中删除参数。
params
excludeParams
method
请注意,您要么使用DMI及其它上面的方法,要么不使用它,在这种情况下,您应该将一个操作映射到该方法,并且不使用属性method或submit标记,method并且action由于上述问题限制了使用特殊参数。如果遵循该限制,则有多个提交按钮,则必须form使用JavaScript onclick事件动态更改标签的action属性。
submit
action
form
onclick