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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Sreeram
  • 3,160
  • 6
  • 33
  • 44

2 Answers2

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
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 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? – Sreeram Sep 20 '11 at 14:25
  • `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: http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html – BalusC Sep 20 '11 at 14:26
  • Thanks for the help BalusC.It worked.Can you tell where to add the above command? – Sreeram Sep 20 '11 at 14:30
  • As startup VM argument or as `JAVA_OPTS` environment variable. But if it works, then you don't need to add that argument anyway. – BalusC Sep 20 '11 at 14:34
  • I also have another small clarification.Today i went through this question which you answered http://stackoverflow.com/questions/2524514/how-to-use-jsfs-hselectbooleancheckbox-with-hdatatable-to-create-one-object-pe[link] It is working fine.But the check boxes remain selected even after submission.How to avoid that? – Sreeram Sep 20 '11 at 14:39
  • 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? – Sreeram Sep 21 '11 at 10:32
  • Hi @BalusC , 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?? – Ye Win Sep 19 '16 at 09:07
  • @YeWin: Performance? Are you still using 30 year old 386 based PCs over there? – BalusC Sep 19 '16 at 09:30
  • @BalusC , 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. – Ye Win Sep 19 '16 at 09:37
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.

demonz demonz
  • 641
  • 1
  • 15
  • 32