操作表单中使用的数据类型

发布于 2024-08-04 21:56:26 字数 60 浏览 1 评论 0原文

你好,有人可以解释一下,如果我在 bean 表单中声明其他类型的变量(字符串除外),我可能会遇到什么问题?

Hi can somebody explain that if i declare variables of other types (except string) in the form beans what problems I might face ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心意如水 2024-08-11 21:56:26

您的请求参数是String。如果您的表单中有不同类型的参数,则在绑定请求时,Struts 将执行从 String 到您的参数类型的转换。

这就是可能出现问题的地方。

想象一下,您有一个 int 类型的属性,并且您请求一些无法转换为 int 的 String (例如包含字母 - 但这已经是一个数据验证问题) 。

现在,int 必须始终有一个值,因为它是一个原语,因此 Struts 会将 0 放入其中并默默地失败。当您使用值为 0 的该字段时,您将不知道是否有错误或用户自己插入了 0。对于其他类型的字段,您也可能会遇到此问题。

您必须控制绑定,不要总是依赖 Struts。

Your request parameters are Strings. If you have different type of parameters in your form, when binding the request, Struts will perform conversions from String to your parameter types.

This is where problems can occur.

Imagine you have a property that is of type int and you request some String that fails to convert to an int (e.g. contains letters – but this is already a data validation issue).

Now, an int must always have a value since it is a primitive, so Struts will put 0 in it and fail silently. When you will use that field with the value 0 you won’t know if you had an error or the user inserted 0 on his own. You can face this problems with other types of fields also.

You have to be in control of the binding, don’t always count on Struts.

焚却相思 2024-08-11 21:56:26

看看http://jtute.com/struts/0401.html

摘要:您应该使用哪些数据类型
用于操作表单的属性?
这里有一些建议。

Take a look at http://jtute.com/struts/0401.html

SUMMARY: What data types should you
use for an action form’s properties?
Here are some suggestions.

对你而言 2024-08-11 21:56:26

从浏览器到服务器的信息通过 HTTP 传输,并且不了解数据类型或对象,因此最好的选择是使用 String 类型。

The information from the browser to the server goes over HTTP, and there is no awareness of data types or objects, so your best bet is to use String types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文