vivmal
vivmal

Reputation: 317

Struts2: Hidden field and anchor tag <s:a> values In Action Class

I am working on a struts2 application. In my jsp page I have 2-3 anchor tag and 2-3 hidden fields like

<s:a href="#">File 1</s:a>
<s:a href="#">File 2</s:a>
<s:a href="#">File 3</s:a>

and

<s:hidden name=" hidden1" />
<s:hidden name=" hidden2" />
<s:hidden name=" hidden3" />

Now please let me know, In my action class how can I get value of all the hidden fields and anchor tag which was clicked.

I had tried following

<s:a href="#" action=”someAction”>File 1</s:a>

Its working but didn’t transfer value of hidden fileds. Also

<s:a href="#" name=”File1” onclick=”submit”>File 1</s:a>

But no gain.

Looking for your reply.

Upvotes: 1

Views: 27122

Answers (1)

Brian Yarger
Brian Yarger

Reputation: 1975

Like Boris said, you need to put the hidden fields inside a form, and submit that form, OR you can add them as URL parameters to your links. The best method is probably using a form with POST so the hidden fields aren't on your browser's location bar.

Here's an example

<s:form id="myform" name="myform" action="someAction" method="POST">
<s:hidden name=" hidden1" value="first value"/>
<s:hidden name=" hidden2" value="second value"/>
<s:hidden name=" hidden3" value="third value"/>

<a href="#" name="file1" onclick="document.forms['myform'].submit();">Submit with link</a>

<s:submit value="%{'Submit with button'}" />

</s:form>

Since this really has nothing to do with struts2, here's an example with pure HTML:

<form id="myform" name="myform" action="someAction.action" method="POST">
<input type="hidden" name=" hidden1" value="first value"/>
<input type="hidden" name=" hidden2" value="second value"/>
<input type="hidden" name=" hidden3" value="third value"/>

<a href="#" name="file1" onclick="document.forms['myform'].submit();">Submit with a link</a>
<br/>
<input type="submit" value="Submit with a button"/>

</form>

Upvotes: 2

Related Questions