I have seen many tutorials in which action has many methods other than execute
public class UserAction {
public TestAction() {
}
public String execute() throws Exception {
System.out.println("hello i aam action");
return "success";
}
public String addUser(String name)
{
return "success";
}
public String listUser(String name)
{
return "success";
}
}
the action mapping is done like this
<action name="addUser" method="addUser" class="com.vaannila.web.UserAction">
<result name="success" type="redirect">listUser</result>
</action>
<action name="listUser" method="listUser" class="com.vaannila.web.UserAction">
<result name="success">/register.jsp</result>
</action>
I a unable to understand it.How listUser is getting called?
I don’t think this works because your mapping is pointing to methods (add, list) that do not exist. You have
addUserandlistUserin your action so your mapping have to point to the same methods.Back to your question I have to say Struts2 has a feature that let’s you pack a couple of methods into the same action. This is specially useful when you want to perform CRUD operations. One way is to explicitly predetermine the method you want to call in you mapping:
If you point your browser to
http://yourDomainHere/listUseryourlistmethod in yourUserActionaction will be executed. You can also use wildcard mapping here like:In this case you can replace asterisk (*) with any method in your action. This mapping will take first placeholder ({1}) in the mapping as a method and call it. So you can point your browser to:
http://yourDomainHere/listUserandhttp://yourDomainHere/addUserknowing the same mapping will handle both and your respective methods will be called.Edit for the comment
I think I got your point. In the example you provided
listCustomeris never called. It’s theaddCustomerthat populates thecustomerListbefore sendingsuccessand since it has provided a getter method for the list, back in the page you can access it and iterate it and show the values.