2016-09-11 43 views
2

我正在使用多個提交按鈕使用我的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方法不被稱爲

回答

1

似乎你是錯的在表單中唱默認動作。

更優雅的解決方案可能是對同一個Action使用多個映射。這樣你就不需要將「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類

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; 
    } 
} 

refernce:API

+0

將我的其他行爲置於默認行動中 – shashank