Struts 2 How to pass parameter to action

15.3k Views Asked by At

I use Struts 2 Framework and i want to pass a parameter in my action like this localhost:8080/MyApp/ModifierMessage.action?id=9. In my jsp page i have the following text with the action:

<a href=\"/OCC/ModifierMessage.action\">Modifier</a>

Someone could help me to add dynamic id to my action ?

2

There are 2 best solutions below

0
On

Use <s:url> and <s:a> tags. For example is your dynamic id is called dynamic_id:

<s:url var="myUrl" action="ModifierMessage.action" namespace="/OCC">
    <s:param name="id">%{dynamic_id}</s:param>
</s:url>

<%-- The link --%>

<s:a href=%{#myUrl}>Modifier</s:a>
0
On
public class MyAction extends ActionSupport {
   private int id;

   public String execute() {
       ...
      this.id = 123;
      return SUCCESS; 
   }

   public int getId() { return this.id; }
   public void setId(int id) { this.id = id; }
   ...
}

In the above code if it returns SUCCESS then the browser will be forwarded to

/<app-prefix>/myNamespace/otherAction.action?id=123