Learning and Learning!-- Java all the way!
Learning and Learning!-- Java all the way!
Learning and Learning!-- Java all the way!
Vic Hood wrote:Hi all ,
I'm facing a problem while trying to implement my screen using JSTL. In order to be able to dynamically control the screen, I tie the value attribute of an html tag to the bean value. For instance, somewhat like this:
where strmeterSerial is an attribute of the bean,Consumer. That is accessed on the particular screen by placing the following include at the top of the screen
Now , the thing is that fields that have been entered as empty(as in where the user chooses not to enter any data), are replaced with '' and are inserted as '' into the database instead of a NULL.
Even worse , if I used an attribute of type int or float and tied it to a field in the jsp , when left empty it defaults to 0 and 0.0 respectively .These are then further inserted into the database , causing an inexplicably large number of zero values . Also , it becomes impossible to distinguish which of the values in the database are a result of a user manually entering zero or is it because of a default value .
It would be great if anyone could suggest a workaround for this.
Learning and Learning!-- Java all the way!
Learning and Learning!-- Java all the way!
Learning and Learning!-- Java all the way!
Paul Clapham wrote:Well, first of all you're making a common beginner mistake: Never use == and != to compare the contents of two strings. So instead of
you should have
Paul Clapham wrote:
However your DAO is inserting values into the database from a variable whose type is "float". And you want that to be null if the user leaves the HTML field empty? Then "float" wasn't really the best choice, because it can't be null. Perhaps you could do something like using the Float.NaN value to represent an unknown value, and then write null to the database if you see that value.
Learning and Learning!-- Java all the way!
Learning and Learning!-- Java all the way!
Learning and Learning!-- Java all the way!
Learning and Learning!-- Java all the way!
Vic Hood wrote:As far as the user is concerned when the page loads, he is only supposed to see blank fields when he loads a page irrespective of whether the html element is bound to a float , integer or string element.
Stefan Evans wrote:Next question: Are the values in this Consumer class being populated by you, or populated automagically from the request parameters?
ie using a <jsp:setProperty> or a framework like stripes/struts/JSF which does the translation to the bean layer for you?
If the values in the Consumer Bean can be null, perhaps you could use a Float instead of a float primitive to store the value.
Or perhaps a Double? I always prefer doubles to floats as they have more precision.
By using the Object wrapper instead of the primitive, you get the ability to represent null in your bean.
The DAO layer can then leverage that:
So then I don't understand why you are populating the fields with data from a bean, if they are supposed to be blank initially.
Learning and Learning!-- Java all the way!
Bear Bibeault wrote:What's wrong with an if statement?
Learning and Learning!-- Java all the way!
Learning and Learning!-- Java all the way!
Paul Clapham wrote:That's sort of helpful and sort of unhelpful. I see some code which basically says "If strc_id is a string which isn't empty then do some stuff and forward to another URL". But if strc_id is null or an empty string then it does nothing at all, which doesn't seem right. The user is going to just get a blank screen.
.
Paul Clapham wrote:
(And I still see != being used to compare strings there.)
.
Paul Clapham wrote:
So what I can see is various fragments of code which might or might not be related. If that code does happen to forward to pumppopup.jsp, then it shouldn't have a request attribute named "consumer". However that doesn't mean that ${consumer} won't find an attribute in that JSP; it might find a session attribute with that name, for example. You can debug that easily by just outputting ${consumer}, which will display nothing if there's no such attribute available and the toString() of the attribute if there is one.
Learning and Learning!-- Java all the way!
Vic Hood wrote:Hmm I have tried using ${consumer} and it does throw a value . Your mention of session is something i havent looked at yet though, how do I check and remove it then?
Paul Clapham wrote:
No, that isn't what you should do. You need to understand why things were written the way they were, and then fix them appropriately. Otherwise
your system is going to end up as a big pile of quick-and-dirty hacks.
Learning and Learning!-- Java all the way!
Vic Hood wrote:Could you kindly elaborate?
Vic Hood wrote:
So then I don't understand why you are populating the fields with data from a bean, if they are supposed to be blank initially.
Rightly asked Paul.In fact,this made me think , the only time I use the bindings is to populate data when I fetch data to display in the fields(that the user has previously entered in to fields).
So instead of
If I did
And in the servlet code where I prepare data to be put into a bean (all attributes of that bean were being automatically set) and then instead of doing
If i did.
Would this solve all my problems ? On initial page load i assume motorating wouldn't be initialized and there fore the fields would appear blank, when the user inserts data, its fetched from fields using request.getParameter in the servlet and forwarded to DAO operations . And whenever A record is fetched for display the value is displayed through what has been set in the request as explained above.
Learning and Learning!-- Java all the way!
We find this kind of rampant individuality very disturbing. But not this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|