1

I have a bean as shown

@ManagedBean
@RequestScoped
public class Demo {
private int value1;
private String value2;

..getter and setter..

this is my xhtml page demo.xhtml

<h:form>
        <p:panelGrid columns="2">
        <p:outputLabel value="id"></p:outputLabel>
        <p:inputText value="#{demo.value1}"></p:inputText>
        <p:outputLabel value="name"></p:outputLabel>
        <p:inputText value="#{demo.value2}"></p:inputText>
        </p:panelGrid>
    </h:form>

how the integer value get 0 value in the client page enter image description here

i have read that in request processing lifecycle, for first time request, jsf runtime will create new view and directly goes to render phase. how values are set to client page without process validation phase.

i read these lines from jsf 2.0 complete reference

*Initial request to view the register.xhtml page

Since this
is an initial request (also referred to as a “non-postback” request) to view the
registration page, the Restore View phase creates an empty View (UIViewRoot)
component tree and stores it in the FacesContext instance.

After the View is created, the lifecycle immediately proceeds directly to the Render Response phase , since there was no incoming field data in the request to validate or process (also referred to as a “non-postback” request).*

i think this sentence help to understand my question. expect answer in lifecycle point of view

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
vijaya kumar
  • 824
  • 2
  • 12
  • 21

4 Answers4

9

Your first Problem is that int cannot be null, it is a primitive. Use Integer instead.

Following your will run into a problem that is related to several posts here that are around COERCE_TO_ZERO.

To prevent this behaviour you can configure the context parameter javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL in your web.xml to be true

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>

Additionally you may need to add a VM parameter to the startup script of your tomcat. -Dorg.apache.el.parser.COERCE_TO_ZERO=false

There are several good write-ups on the web about this topic

cheffe
  • 9,345
  • 2
  • 46
  • 57
  • yes u r correct, using default constructor, properties are initialized. but my question is how values are set to client page without process validation phase, note: this is first time request – vijaya kumar Sep 26 '13 at 06:29
  • @vijayakumar He answered your question... `int`s cannot be `null`. They always need to have a value, by default that's 0. But even if you use `Integer`, they will be `null` initially, but then coerced to `0` unless you explicitly disable it. This is a problem independend from JSFs lifecycle. – noone Sep 26 '13 at 06:48
  • ok i think i need to workout jsf lifecycle again. – vijaya kumar Sep 26 '13 at 07:00
  • @noone The question didn't concern the way empty string will be treated during a postback. The question was, literally, "why do I get 0 when i bind a value to an uninitialized int field in a fresh view". Although I find the answer helpful, it does not concern the question at all, beside the last paragraph. Сheffe, you don't need to give helpful advice in an answer, you need to answer a given question instead. – skuntsel Sep 26 '13 at 07:02
  • @skuntsel you are right, but I have seen this several times during my work, when colleagues stumbled over this. The coerce_to_zero will be the next and harder to find question. Usually those colleagues changed their controller attributes from and to int/Integer to understand what is going on. – cheffe Sep 26 '13 at 07:44
  • I have swapped it, to make the problem of int vs Integer clear. – cheffe Sep 26 '13 at 07:50
  • @cheffe Thanks, it's better now! I do admit that failure to know this will yield the next "Why do I have 0/empty string in my managed bean" question, but the question nowhere states that as a problem. So it should be a side note but not the actual answer. – skuntsel Sep 26 '13 at 07:59
  • i updated my question.pls refer – vijaya kumar Sep 26 '13 at 08:17
1

This happens because the default value of an int field is 0. More info about this you can find in the Java Language Specification here.

If you want to display an empty field you have to use the Integer wrapper type instead of the simple int type. This way the initial value of the field will be null and it will be displayed as an empty field.

skuntsel
  • 11,624
  • 11
  • 44
  • 67
Adrian Mitev
  • 4,722
  • 3
  • 31
  • 53
  • yes u r correct, using default constructor, properties are initialized. but my question is how values are set to client page without process validation phase, note: this is first time request – vijaya kumar Sep 26 '13 at 06:25
  • Default value of `Integer` class is `null`, as you stated further. Please do not confuse beginners with `int`/`Integer` distinction. Correct your answer and I'll reverse my vote. – skuntsel Sep 26 '13 at 06:28
  • Cannot understand exactly what do you want me to correct. – Adrian Mitev Sep 26 '13 at 07:21
  • @AdrianMitev Have corrected the vagueness of `int`/`Integer` distinction on my own. Feel free to correct it the way you want! – skuntsel Sep 26 '13 at 07:40
1

Java primitives can't be null, objects on the other hand can be. Your private int value1; simply is treated as private int value1 = 0; by the compiler according to section 4.12.5 of the Java language specification "Initial Values of Variables", as the default value for int values is 0. Accordingly, your string's default value is null.

Hence, you get value1 as zero and value2 as a null string.

It's also worth noting it has nothing to do with JSF as its just a basic Java issue.

skuntsel
  • 11,624
  • 11
  • 44
  • 67
  • getting zero is not my problem, if i give any values to String property, it also displays in client page. my doubt is on jsf lifecycle. i updated the question pls refer that – vijaya kumar Sep 26 '13 at 08:22
  • @vijayakumar Your question concerns JSF lifecycle **in no way**. You request to understand why you are getting 0 in your view. It has nothing to do with lifecycle. As to the lifecycle, when an initial HTTP request is fired indeed only two phases are passed. – skuntsel Sep 26 '13 at 08:56
  • ...sorry what i thought is ..property values are applied to inputcomponent in applyrequestvalue phase, but in render phase only property values are called using getter method. – vijaya kumar Sep 26 '13 at 11:02
0

Even if you use Integer wrapper type instead of int primitive, only the display field would be NULL. However, if you don't supply any value to it i.e if the field is left blank, JSF again appends zero to the field implicitly.

theblackpearl
  • 1,164
  • 3
  • 15
  • 34