Redisplay submitted value in input field

2020-04-24 16:02发布

I have search functionality in my jsp page.. When user types "abc" and search for that. I get the result back but is there a way to retrieve that abc back to that search textbox along with the search results? Thank you.

标签: jsp input
2条回答
一夜七次
2楼-- · 2020-04-24 16:56

Just prefill the input value with the submitted value. Submitted input values are available in EL via the ${param} request parameter map keyed by input field names.

<input type="text" name="query" value="${fn:escapeXml(param.query)}" />

Note the importance of JSTL fn:escapeXml(). It's to prevent your page from XSS attacks while redisplaying user-controlled input.

The above example will prefill the input value with the result of request.getParameter("query"). As EL is null-safe, it won't display anything if it returns null.

See also:

查看更多
3楼-- · 2020-04-24 17:03
<input type="text" name="foo" value="${param.foo}" />

This will also work if you want something more basic, but I am not sure about the security

查看更多
登录 后发表回答