aspose file tools*
The moose likes JSP and the fly likes Possible work around for displaying blanks as default  values for text fields tied to float-JSTL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Reply locked New topic
Author

Possible work around for displaying blanks as default values for text fields tied to float-JSTL

Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi folks,
Im trying to develop a web page with a form where Im tying the fields of the form to a generic bean to get the value.
Some what Like this:

Where 'factualFlow' corresponds to a field with a float datatype in the bean . Now , when the user loads up the screen for the first time the value of the bean is null and so the values of these fields(in the jsp) seem to be defaulting to '0.0' as they are empty . Giving an ugly look . Is there a way to set these fields to ' blank' values so that the UI improves and nulls maybe inserted in the DB without affecting the look and feel of the UI , rather than inserting a set 0.0 and 0 's


Learning and Learning!-- Java all the way!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

Wrong question. You don't want to set the value of the fields to blank (which wouldn't be possible anyway). You want to display blank if the field is empty. Like so:


Or you could consider using fmt:formatNumber with a pattern which does zero suppression instead of allowing the system's default formatting to be used.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Another option is to use the c:out tag with a 'default' attribute.

Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Thanks for the quick replies folks! . Another question on these lines, since we are only showing "" here , would the value of '0.0' be inserted into the database? As currently Im passing the value of the entire bean for insertion into a method, and wouldnt want '0.0' or 0 to be entered against any of these fields unless the user explicitly inserts or updates as' 0 .0' or 0 explicitly
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

Nothing in that code will ever cause anything to be written to any database ever.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hm okay thats great !. Will get back after trying it out!
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Okay here's something I tried. However Im not getting it to work

And

However , both of these methods seem to be failing , as in I still see '0.0' being printed against them as default values. Any pointers as to where I could be going wrong?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

You said the bean was null. Apparently it isn't. A simple test like

will display nothing if "consumer" is null and the result of the bean's toString() method if it isn't.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hmm , I agree.my bad. I did get toString () method of the bean upon printing it . The thing is that some of the fields of the bean do need initialization such as the name of the user of the screen etc.(which is an attribute in the bean). However others such as these numbers dont , it would be great if you could suggest a work around
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

To provide some additional details . The particular page in question is a popup and loads through the doGet() of a particular servlet .
And below I've placed the code of it , here I explicitly set the bean to null , but the problem still persists.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

What I dont seem to understand is despite doing this before loading the page

And the following in my jsp code


Why am I getting '' and 0's to be taken for String and int attributes of that bean?Am I not explicitly assgining the bean to a null value?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

Firstly, what did you expect setting the bean to null to do for you?

Secondly, the code in your servlet and the code in your JSP have no relationship to each other. What makes you think that they do?

And lastly, what's with the needless concatenation in String url=""+"/pages/pumppopup.jsp"; ?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Well by setting the bean to null , I hoped to get rid of the '' ,0 and 0.0's that were being displayed as a result of the bean being initialized.
As for the second point , like i said above I was under the impression that setting the bean to null onload of the page through the doGet method of the servlet, I would be doing the necessary.
I guess I'm looking at it the wrong way.Please do let me know , how I could get rid of these default values.
Yeah and as for the concatenation ,by specifying an extra space Im just trying to refer to the root.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

Vic Hood wrote:Well by setting the bean to null , I hoped to get rid of the '' ,0 and 0.0's that were being displayed as a result of the bean being initialized.

Setting the local variable to null will have no effect except setting the local variable to null. It's a useless act.

Yeah and as for the concatenation ,by specifying an extra space Im just trying to refer to the root.

Root? I have no idea what you mean by this.

As posted earlier, if you want to affect the display, do it with testing in the JSP. I have no idea why you are reverting to trying weird things in the servlet that have no relation to things in the JSP and why you would think that they would have any effect.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Okay I was under the impression that doing so in the servlet would help.
So how exactly do I go about doing this in the jsp
This is the code that I use to instantiate the consumer bean and the dao instance using usebean


And I've used the same pattern in other pages too. and they all work well .
Could you further elaborate on what I could test to figure out where Im going wrong.
Thank you, for your time
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

The topic has meandered so much that I'm not even sure what you're after anymore. Please post a small example of what you are trying to accomplish. I gather that under some circumstances you want to show a blank or some other value when a numeric value is zero? In that case, the ternary operator will achieve that nicely.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Bear,
Thank you for taking the time out to reply.
And sorry about meandering from the topic , as I rellay dont know what the problem is my first post was possibly only one if its after effects .
My problem currently is that upon loading a page with the following code(I've posted only some of it to get the point across).
The problmeatic JSP below.

Now , the problem is when the page loads , all html elements with value attributes tied to bean variables that are of type int or float are showing as 0 and 0.0 respectively. I was initially under the impression that this was because of their default values . However none of the work arounds suggested earlier worked for me , And upon further looking at the data inserted into the database from this screen I noticed that all the fields with string values were being substituted with '', float with 0.0 and int with 0 before being inserted into the database (if left empty). I hope I'm being able to communicate my problem clearly
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

What gets displayed and that gets inserted into the database are two very different things and should be handled respectively differently.

When displaying, use the ternary operator to offer a different display value when you don't want to show zero. Do not change the value of the model to suit the display.

For example: ${ x == 0 ? '' : x }

You should never change the value of the model to suit the display. If that's what you are doing, it's likely the cause of your database woes.

Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Thanks for the reply. But I think I've tried this method before as suggested by Paul.I've quoted my earlier post , is it not the same?
Vic Hood wrote:Okay here's something I tried. However Im not getting it to work


However , both of these methods seem to be failing , as in I still see '0.0' being printed against them as default values. Any pointers as to where I could be going wrong?


And as for changing values of the model , the above is all Im doing , nothing more than that .And another doubt , when we say
${ x == 0 ? '' : x }
Are we not passing '' to the model, and changing its value ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

Vic Hood wrote:Okay here's something I tried. However Im not getting it to work

Then you find out why it's not working rather than try to find some convoluted other way to do it. The empty test is likely not the right expression.

when we say
${ x == 0 ? '' : x }
Are we not passing '' to the model

No. How on earth is that passing anything to anywhere? All it's doing is formatting display text. the value of x is in no way affected. How could it be?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

${ x == 0 ? '' : x }
Sounds fine . But what if the user has explicitly entered 0 would he not be getting '' in return then?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

Return what? If it is set as as the value of a form element, then yes, a blank will be "returned" unless it is changed. So what? Easy to test for.

If the user explicit sets it to "0" then that's what will be returned. How could it be otherwise?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Uh Im saying that In the case of the page load where we replace 0 with '' using
${ x == 0 ? '' : x } , in the value attribute of the html element , we would be fine BUT
when the page loads with user data and say , the user has explicitly set 0 to one of these fields , would we not be printing '' instead of '0'?In sum , how do we differentiate whether the fetched 0 is from user filled data or the default value, while3 checking using EL?
Sorry , if I may be sounding thick skulled there , but I just dont seem to get that
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

zero is zero. Is there anyplace else in Java where you can tell where the zero came from? Of course not. Why is it significant?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Bear Bibeault wrote:zero is zero. Is there anyplace else in Java where you can tell where the zero came from? Of course not. Why is it significant?

The only reason I think it is significant is , I dont want to be displaying '' (upon fetching inserted records) when the user has himself earlier entered a 0 there(while inserting data ) . How do I test whether the zero is the default value or an earlier value that was entered by the user?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

How can the user enter the value "earlier" than when the form is displayed?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Uh when the user has to edit a record? He will click an edit button and populate all the fields with the the data he initially entered , and here if he had entered a '0' when inserting data would he not get a'' upon data retrieval ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

So you want to show 0 or blank later depending upon whether the user explicitly specified zero, or let it default? Well, as I said, zero is zero, so there's no way to make that distinction with just the value. Personally, I think this all sounds crazy and over-complicated, but if you need to make this distinction you'll need to carry around and extra bit of information modeling whether the value was explicit or defaulted.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hmm I get you , unfortunately it seems to be messed up. I'm not particularly over joyed about doing anything complex and would be more than happy if you were able to help me out with a better method.
Also, by additional logic, do you mean setting a flag when the fields are fetched for editing and then doing this ?
${(testflag!=edit) &&x == 0 ? '' : x }
Im kinda very new to EL.
Thank you for your time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Possible work around for displaying blanks as default values for text fields tied to float-JSTL