SoulRayder
SoulRayder

Reputation: 5166

Passing data from jsp page to action class using struts upon clicking a href link

I have a JSP page wherein I am displaying some text in the form of a link.

<div class="col-sm-3 pull-left">
      <a href="getCityDetails.action" name = "cityName"><s:property value="cityName" /></a>
        </div>

Wherein I am fetching the city Name from some particular java class file through an action.

When I click the text I want it to redirect to the action getCityDetails.action, which in turn calls some method of another java class file. But along with this, I want to pass data into the action, that is the text that I have clicked. What changes should I make to accommodate this behaviour?

Upvotes: 0

Views: 3961

Answers (3)

Andrea Ligios
Andrea Ligios

Reputation: 50203

Use the <s:url> tag to build an action URL:

<s:url value="getCityDetails.action" var="url">
    <s:param name="cityName" value="%{cityName}" />
</s:url>

then use it in a standard <a>:

<a href="<s:property value="%{#url}"/>">
    <s:property value="cityName" />
</a>

or in an <s:a>:

<s:a href="%{url}">
    <s:property value="cityName" />
</s:a>

Upvotes: 2

Si Kelly
Si Kelly

Reputation: 703

You can pass the data as a query parameter:

<div class="col-sm-3 pull-left">
  <a href="getCityDetails.action?cityname=<s:property value="cityName" />" name = "cityName"><s:property value="cityName" /></a>
</div>

Upvotes: 2

Neeraj Jain
Neeraj Jain

Reputation: 7730

Pass Parameter within the URL

Try This

 <a href="getCityDetails.action?paramToPassToAction=Value" name = "cityName"><s:property value="cityName" /></a>

Upvotes: 0

Related Questions