如何在服务器验证失败后重新填充jsp页面上的表单字段
我有一个非常简单的Java MVC Web应用程序,并且正在使用servlet来处理表单验证。如果表单已经过验证,请求将转发到适当的视图。但是,如果表单未通过验证,请求将转发回表单,然后显示相应的错误消息。
I have a very simple Java MVC web application and am using a servlet to handle form validation. If the form is validated, the request is forwarded to the appropriate view. However, if the form fails validation, the request is forwarded back to the form, which then displays the appropriate error message(s).
我的问题是这个 - 什么使用最初由用户在表单中输入的数据重新填充所有表单字段的最有效方法是什么?
My question is this -- what is the most efficient way to re-populate all of the form fields with the data that was originally entered in the form by the user?
我没有使用MVC框架,只需简单的HttpServlets作为控制器,并以.jsp作为视图。
I am not using an MVC framework, just simple HttpServlets as the controller with .jsp as the view.
最简单也可能是最简单的努力就是使用
The easiest and probably least effort is to just use
<input name="foo" type="text" value="${param.foo}"/>
当用户第一次访问表单时,这应默认为。
This should default to "" when the user first visits the form.
可以创建一个绑定到请求的自定义标记。然而,这可能不是您正在寻找的解决方案。
A little more can be done to create a custom tag which binds to the request. However this is probably not the solution you were looking for.
编辑:你可能想要使用< c:out value =$ {param.foo}/>
防止XSS攻击。
You may want to use <c:out value="${param.foo}"/>
to protect against XSS attack.