我在jsp页面中使用struts2标签的多个提交按钮来调用java类中的不同方法,但是没有调用这些方法。
我的jsp页面是: -
<s:form theme="simple">
<table style="width:20%;" style="float:left;" cellspacing="0" cellpadding="0" border="0">
<tr style="white-space:nowrap;">
<td><s:submit name="togglecomplete" value="togglecomplete"action="toggletodotrue"/></td>
<td><s:submit name="toggle" value="cleartodos" action="cleartodo"/></td>
<td><s:submit name="toggleincomplete" value="toggleincomplete" action="toggletodofalse"/><td>
</tr>
</table>
</s:form>
我的struts.xml是
<struts>
<package>
<action name="toggletodotrue" class="com.action.JtableAction"
method="togglecompleted">
<result name="success" type="redirect">listTodo</result>
</action>
<action name="cleartodo" class="com.action.JtableAction"
method="clearcompleted">
<result name="success" type="redirect">listTodo</result>
</action>
<action name="toggletodofalse" class="com.action.JtableAction"
method="toggleincomplete">
<result name="success" type="redirect">listTodo</result>
</action>
<package>
<struts>
java类是
public class JtableAction extends ActionSupport implements ModelDriven<TODO> {
public String togglecompleted ()throws IOException
{
try{
System.out.println("inside toggle completed");
dao.completeAllTodo();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
return Action.SUCCESS;
}
public String clearcompleted() throws IOException{
try{
System.out.println("inside clear completed");
dao.clearCompleteTodo();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
return Action.SUCCESS;
}
public String toggleincomplete()throws IOException
{
try
{
dao.toggleIncompleteTodo();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
return Action.SUCCESS;
}
这些java方法未被调用
答案 0 :(得分:1)
似乎您错过了表单中的默认操作。
更优雅的解决方案可能是通过对同一个Action使用多个映射。这样你就不需要设置&#34; struts.enable.DynamicMethodInvocation&#34; to&#34; true&#34;。
在JSP中
<s:form method="post" action="mySubmitAction">
<s:submit value="Submit"/>
<s:submit value="Clear" action="myClearAction"/>
</form>
在struts.xml中
<action name="mySubmitAction" class="MyAction" method="submit">
<result>submit.jsp</result>
</action>
<action name="myClearAction" class="MyAction" method="clear">
<result>submit.jsp</result>
</action>
然后在MyAction类中
public String submit() throws Exception {
// submit button logic here
return SUCCESS;
}
public String clear() throws Exception {
// clear button logic here
return SUCCESS;
}
对于最佳实践,如果您的操作(提交和清除)加载/管理公共数据,那么您可以定义MyBaseAction类,由MySubmitAction和MyClearAction类扩展。然后就是它们的样子:
在struts.xml中
<action name="mySubmitAction" class="MySubmitAction">
<result>submit.jsp</result>
</action>
<action name="myClearAction" class="MyClearAction">
<result>submit.jsp</result>
</action>
You don't need to specify a method name anymore, that means we will use the default execute() method.
然后在MyAction,MySubmitAction和MyClearAction类
中MyAction.java
public class MyAction extends ActionSupport {
// common data or logic here
}
MySubmitAction.java
public class MySubmitAction extends MyAction {
public String execute() throws Exception {
// submit button logic here
return SUCCESS;
}
}
MyClearAction.java
public class MyClearAction extends MyAction {
public String execute() throws Exception {
// clear button logic here
return SUCCESS;
}
}
参考:API