开发者

How do I get parameter from URL or hidden field

开发者 https://www.devze.com 2022-12-08 01:45 出处:网络
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.

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 pa开发者_如何学运维rameter from the hidden field?


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(java.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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号