Avoid default long value of 0 to display in h:inputText
Asked Answered
G

2

5

I have a <h:inputText> which accepts a long value like this

<h:inputText value="#{ServiceTable.ID}" />

The property is declared like this

public class ServiceTable {

    private long ID;

    // Getter and setter for ID.
}

When I open the page, I always see 0 in the textbox. How can I avoid it? I just need an empty textbox. I am using JSF 1.2.

Goddard answered 20/9, 2011 at 14:21 Comment(0)
F
8

Use Long instead of long. It defaults to null.

private Long ID;

And, if you're running Tomcat 6.0.16 or newer or a fork of it, then you need to add the following VM argument to server startup arguments as well to disable EL coercion of primitives and their wrappers:

-Dorg.apache.el.parser.COERCE_TO_ZERO=false
Fr answered 20/9, 2011 at 14:22 Comment(9)
Thanks BalusC for the response.I read your blog regularly.It is very informative and useful for newbees. Can you explain me what is the difference?Goddard
long is a primitive which has a default value of 0 when declared as class/instance variable. Long is a wrapper object which has a default value of null. This is just basic Java. See also the Java tutorial on primitive types: download.oracle.com/javase/tutorial/java/nutsandbolts/…Fr
Thanks for the help BalusC.It worked.Can you tell where to add the above command?Goddard
As startup VM argument or as JAVA_OPTS environment variable. But if it works, then you don't need to add that argument anyway.Fr
I also have another small clarification.Today i went through this question which you answered #2525014 It is working fine.But the check boxes remain selected even after submission.How to avoid that?Goddard
Hi BalusC,By linking a HashMap to a h:selectbooleancheckbox how do i change individual datatable cell with h:inputtext to editing mode so that i can edit all the columns and rows at a time?Goddard
Hi @Fr , Thank you for your answer, I just want to know using reference type Long performance is not good instead of using primitive type long. So, this solution will be effected to performance? Or there is any other alternative ways to avoid display 0 case by using jsf converter??Anh
@YeWin: Performance? Are you still using 30 year old 386 based PCs over there?Fr
@Fr , No. I just thought using Long instead of long is expensive code according to Effective Java and Clean Code books references. If you have any lessons to me and references for that, please let me know.Anh
A
1

awful! many developers do not have access to the actual server, and sometimes you just cant go to the client and tell him :" stop your server and restart everything with this start up options".

How come people in the apache team never thought about that?

If you are in such situation - like myself - another solution is to get the field as a String and parse it manually in your backing bean.

Auk answered 15/12, 2011 at 22:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.