2

i am using multiple submit buttons using struts2 tags in my jsp page to call different methods in java class but the methods are not being invoked.

my jsp page is:-

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

and my struts.xml is

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

The java class is

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

These java methods are not being called

Roman C
  • 49,761
  • 33
  • 66
  • 176
triples13
  • 206
  • 4
  • 18

1 Answers1

1

Seems you are missing the default action in form.

The more elegant solution is probably by using multiple mappings for same Action. This way you don't need to set "struts.enable.DynamicMethodInvocation" to "true".

In JSP

<s:form method="post" action="mySubmitAction">
    <s:submit value="Submit"/>
    <s:submit value="Clear" action="myClearAction"/>
</form>

In 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>

Then in MyAction class

public String submit() throws Exception {
    // submit button logic here
    return SUCCESS;
}

public String clear() throws Exception {
    // clear button logic here
    return SUCCESS;
}

For best practice, if you have common data loaded / managed by your actions (submit & clear), then for example, you can define a MyBaseAction class, extended by MySubmitAction and MyClearAction class. Then this is how they looks like:

In 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.

Then in the MyAction, MySubmitAction and 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;
    }
}

refernce : API

Sanka
  • 1,294
  • 1
  • 11
  • 20