struts tag for button call Action

KPS picture KPS · Jun 22, 2012 · Viewed 13.3k times · Source

Just wondering if anyone has ever seen or written a customer tag to call Struts2 Actions.

What I'm looking for is something like this:-

<s:button value="Click Me!" action="thisIsMyAction" >
    <s:param name="productId" value="%{productId}" />
    <s:param name="userId" value="%{userId}" />
</s:button>

So that then in the Action you have

public String thisIsMyAction() {

   String productId = getServletRequest.getParameter("productId");
   String userId= getServletRequest.getParameter("userId");

   // Do some stuff here.

   return SUCCESS;
}

Reason being, we don't alays want to use href or images and we're not always submitting a form.

Cheers in advance

Answer

Atropo picture Atropo · Jun 22, 2012

You can do a form with two hidden parameters, and a submit of type button.

<s:form action="myAction">
<s:hidden name="productId" value="%{productId}" />
<s:hidden name="userId" value="%{userId}" />
<s:submit  value="Click me!" type="button"/>
</s:form>

In my action you declare the properties productId and userId with relative getters and setters.