我有一个问题,希望有人可以帮助我。嗯,我正在使用 Struts 1.3.10 开发一个应用程序,并且我有一个 struts 表单,其中包含一个包含属性的对象。该属性被声明为原始类型 int ,当我从数据库检索该数据并结果为 NULL 时,应用程序向用户显示零(0)而不是任何内容时,问题就出现了。你们中有人遇到过这个问题吗?你们如何避免这种行为?
我想到的唯一一件事是将 int
转换为 String
对象,但这意味着当您必须插入/检索/时进行一些转换和/或其他操作将数据更新到数据库中/从数据库中更新数据。
有什么帮助吗?
提前致谢,
卡洛斯
I have a question and I hope somebody can help me out here. Well, I'm developing an application with Struts 1.3.10 and I have a struts form with an object that contains a property. That property is declared as the primitive type int
and the problem comes when the app shows to the user zeroes(0) instead of nothing when I retrieve that data from the database and turns out to be NULL. Have any of you experienced this problem? How do you guys do to avoid that kind of behaviour?
The only thing it came to my mind was to turn that int
into a String
object, but that implies some casting and/or other operations when you have to insert/retrieve/update data into/from the database.
Any help with this?
Thanks in advance,
Carlos
发布评论
评论(1)
总之,这就是您想要的:
如果有人输入了值,则在文本框中显示该值,如果有人没有输入值(值为
null
)则显示空白。不幸的是,我找不到一种方法可以自然地做到这一点,因此您必须执行以下操作:
1. 在表单上为这些属性使用
Integer
,从数据库中设置这些值,使它们为空或实际值。2. 在您的 jsp 上,您必须像这样手工制作输入标记:
其中 FORM_NAME 是表单的名称,PROPERTY_NAME 是名称你的
Integer
属性这并不优雅,但它应该可以工作(虽然我还没有测试过)In summary, here's what you want:
If someone has input a value, display the value in the textbox, if someone hasn't entered a value (the value is
null
) display a blank.Unfortunately there isn't a way that I could find to do this naturally, so you have to do the following:
1. Use an
Integer
on your form for those properties, set these values from the database so that they are null or the actual value.2. On your jsp you will have to hand craft the input tag like so:
<c:set var="propertyValue">
<c:if test="${! empty FORM_NAME.PROPERTY_NAME}>
<c:out value="${FORM_NAME.PROPERTY_NAME}"/>
</c:if>
</c:set>
<input type="text" name="PROPERTY_NAME" value='<c:out value="${propertyValue}/>'/>
Where FORM_NAME is your form's name and PROPERTY_NAME is the name of your
Integer
property. It's not elegant, but it should work (I haven't tested it though)