0

In my Struts2 application, I want to pass a hidden value from a JSP file to an Action class. This will be the sole purpose of this JSP page, so in fact it will act as a redirection page, with a hidden value in it. My code:

<form action="editexperiment" method="post">
    <s:hidden name="id" value="%{id}"/>
    <button type="submit">Submit</button>
</form>

The above code works, but obviously I do not want to click on a submission button, so how can I remove it and auto-submit this (hidden) form? I know I can achieve this easily with JavaScript, but that's not my preferred solution.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
João Fernandes
  • 558
  • 3
  • 11
  • 29
  • ... How else would you do it besides with JavaScript? And what's even the point of using a form for this? – Dave Newton Mar 02 '15 at 15:43
  • Dave, that's precisely my doubt. Concerning your second question, If there is another way of sending a variable from one Action to another Action directly, without using a form, I'm curious to know. I also haven't mentioned using the session object because this variable will be precisely used as a key to obtain an object from the session Map. – João Fernandes Mar 02 '15 at 15:54
  • 1
    As a URL parameter? And where it's stored (session or passed as a parameter) isn't really relevant. If you have it available in the JSP page then it's obviously available on the server side, and you don't need to expose it at all. Having a page that exists solely to redirect with an ID you provided from the server side seems weird, since you could just do a redirect from the action's configuration. – Dave Newton Mar 02 '15 at 16:09
  • I agree that this solution wasn't the more elegant, but it was the best I could figure out by myself. The URL parameter solution wasn't an option for me because I didn't want to expose the variable to users. Thanks for your input, please see the accepted answer below. – João Fernandes Mar 02 '15 at 16:24
  • ... There's nothing "not exposed" about this solution; if you have an authorization issue that's orthogonal. Security through obscurity isn't. Note that this stuff is covered in the S2 wiki. – Dave Newton Mar 02 '15 at 16:51
  • @Dave, I'm afraid I haven't fully understood your last couple of comments. If you could enlighten me on that, I'd appreciate. – João Fernandes Mar 04 '15 at 09:43
  • 1
    In [this gist](https://gist.github.com/davelnewton/177be8ef1ba42f0e8b9b) because too long for a legible comment. – Dave Newton Mar 04 '15 at 13:57
  • @DaveNewton: why not posting that "gist" as an answer to this question ? It *is* relevant and it *can* help future visitors wondering about the same problem; you've also put effort and spent time in it, and a link in a buried comment is not enough IMHO... please consider copy-pasting it here – Andrea Ligios Mar 04 '15 at 16:24
  • Sorry for taking so long to answer, I only had time to carefully read your detailed comment now. Thanks for it, I finally got your point. Also, I think that Andrea's suggestion (turn it into a separated answer) can be highly valuable to other users, in the future. – João Fernandes Mar 12 '15 at 15:45

1 Answers1

1

Instead of dispatching a JSP page from ActionOne, that automatically sends a new request to ActionTwo, have you considered using the redirectAction result to pass from ActionOne to ActionTwo without the need of the above JSP ?

You can pass parameters (even dynamically, up to a certain point), like follows:

<action name="actionOne" class="foo.bar.ActionOne" >
    <result name="success" type="redirectAction">
        <param name="actionName">actionTwo</param>
        <param name="id">${id}</param>
    </result>
</action>

Where id must match to a getter in your actionOne, and a setter in your actionTwo.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • 1
    This is precisely what I was looking for, a much more elegant solution. I've being using the `redirectAction` result a lot, but I didn't knew it was possible to pass parameters within it. Thank you very much. I love SO, marked as accepted. – João Fernandes Mar 02 '15 at 16:16