views:

521

answers:

2

In a Struts Action, I want to get a parameter from the URL or from a hidden field, when the parameter in the URL is not set. To retrieve the parameter from the URL is as follows:

String userId = request.getParameter("userId");

But the problem is now, the HTML is based on forms (legacy code). And I observerd that the parameter userId may be reset to null. Is it an option to set the parameter in a form in a hidden field as well? Would I need to define a parameter in the dynaForm for this? How would I retrieve the parameter from the hidden field?

+1  A: 

The parameters in hidden fields are set as any other field, also they can be retrieve the same way.

String userID = request.getParameter("userId");

To check if it is null you just:

if(StringUtils.isNotBlank(userId)) {
    // do stuff
}

http://commons.apache.org/lang/apidocs/org/apache/commons/lang/StringUtils.html#isNotBlank%28java.lang.String)

*Edit adding more info request.getParameter doesn't distinguish between get nor post variables, so if you have a parameter from the URL you can get it with the same method.

Juparave
+1  A: 

As far as the server is concerned, hidden parameters are no different to any other parameters. They are just parameter on the request.

In HTML, the type attribute of an input field (which you'll use to say that the form input is hidden) is used by the browser - it tells the browser not to display the input field. But the input field is there in the form.

You are free to change the value of the hidden field using JavaScript is so needed.

When you submit the form, the server doesn't know anything about whether the parameter is from a hidden field or not.

A_M