Search code examples
jspstruts2form-submithidden-field

Auto-submit form with hidden value


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.


Solution

  • 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.