如何在服务器验证失败后重新填充jsp页面上的表单字段 [英] How to re-populate form fields on a jsp page after failed server validation

查看:147
本文介绍了如何在服务器验证失败后重新填充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.

这篇关于如何在服务器验证失败后重新填充jsp页面上的表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆