无法在多个提交按钮中调用我的方法
Not able to call my methods in multiple submit buttons
我在 jsp 页面中使用多个使用 struts2 标签的提交按钮来调用 java class 中的不同方法,但这些方法没有被调用。
我的 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 class 是
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 方法没有被调用
您似乎缺少表单中的默认操作。
更优雅的解决方案可能是对同一操作使用多个映射。这样你就不需要将 "struts.enable.DynamicMethodInvocation" 设置为 "true".
在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中class
public String submit() throws Exception {
// submit button logic here
return SUCCESS;
}
public String clear() throws Exception {
// clear button logic here
return SUCCESS;
}
为了最佳实践,如果您有通过操作(提交和清除)加载/管理的公共数据,那么例如,您可以定义一个 MyBaseAction class,由 MySubmitAction 和 MyClearAction class.然后这是他们的样子:
在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中class
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
我在 jsp 页面中使用多个使用 struts2 标签的提交按钮来调用 java class 中的不同方法,但这些方法没有被调用。
我的 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 class 是
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 方法没有被调用
您似乎缺少表单中的默认操作。
更优雅的解决方案可能是对同一操作使用多个映射。这样你就不需要将 "struts.enable.DynamicMethodInvocation" 设置为 "true".
在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中class
public String submit() throws Exception {
// submit button logic here
return SUCCESS;
}
public String clear() throws Exception {
// clear button logic here
return SUCCESS;
}
为了最佳实践,如果您有通过操作(提交和清除)加载/管理的公共数据,那么例如,您可以定义一个 MyBaseAction class,由 MySubmitAction 和 MyClearAction class.然后这是他们的样子:
在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中class
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