views:

237

answers:

1

Hi everyone,

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  A: 

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)

StevenWilkins
Hi Steven, thanks for your answer. Yes, what you said is what I want. And the approach you're talking about is perfect, but I finally set up the property as a String and when I'm going to operate with the database (read or write) I transform the data to what I need (to String to create the model object and to Integer to set and retrieve the data from the database).
Carlos Pastor