Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant access some (Capitalized) fields in JSF controller?

Tags:

java

jsf

el

I Cant access some (Capitalized) fields in controllers
For Example

My controller (*.java)

package com.co.controller;

public class MyController {
    // fields
    private String FIELD;
    private String F1;

    // .... Controller code

    // Setters and Getters
    public String getFIELD() {
        return FIELD;
    }
    public void setFIELD(String fIELD) {
        FIELD = fIELD;
    }
    public String getF1() {
        return F1;
    }
    public void setF1(String f1) {
        F1 = f1;
    }
}

My Screen (*.xhtml)
When i try to use

<h:panelGrid>
    <h:outputText value="#{myController.FIELD}" />
    <h:outputText value="#{myController.F1}" />
</h:panelGrid>

The result is
F1 field: is NOT Accessible
HOWEVER
FIELD field : is Accessible

The Error appeared

Property 'F1' not found on type com.co.controller.MyController 

Also when i try to use

<h:panelGrid>
    <h:outputText value="#{myController.fIELD}" />
    <h:outputText value="#{myController.f1}" />
</h:panelGrid>

The result is
F1 field: is Accessible
HOWEVER
FIELD field : is NOT Accessible

Why that?

like image 609
ahmednabil88 Avatar asked Feb 24 '14 11:02

ahmednabil88


2 Answers

The BeanELResolver that translates your EL-Expressions to property access adheres to the JavaBean specification. It states (in Section 8.8):

Thus when we extract a property or event name from the middle of an existing Java name, we normally convert the first character to lower case. However to support the occasional use of all upper-case names, we check if the first two characters of the name are both upper case and if so leave it alone.

The resolver works by reflection on your actual class. It produces a list of FeatureDesriptor instances and matches those to your expression. So what the resolver sees from your bean is:

  1. Property FIELD, both read- and writable
  2. Transient property f1, read- and writable
  3. Unexposed property F1

Since you're trying to access F1, which has no getters and setters according to the JavaBeans specification, the resolver throws a PropertyNotFoundException.

To fix this, adhere to the normal camelCase convention and/or use longer names.

See also: Where is the JavaBean property naming convention defined?

like image 73
mabi Avatar answered Sep 18 '22 04:09

mabi


Its because that the ManagedBean has a naming pattern the get/set followed by an CamelCase letters of the word. JSF uses the setters as a way to access it in the xhtml, and not the field itself. If you have noticed, even if the field name or variable name is different, you can access it through the setters/getters.

private String FE123;

public String getFE12345() {
    return FE123;
}

public void setFE12345(String fE123) {
    this.FE123 = fE123;
}

you can access it like

   <h:outputText value="#{managedBean.FE12345}" />

not

   <h:outputText value="#{managedBean.FE123}" />

In the case of field:

getField();
getFIELD();

JSF knows that the second character in the FIELD is an uppercase I. so it identifies it as an all Uppercase field.So you can use it as managedBean.FIELD. If you use the getField, you can get it as

<h:outputText value="#{managedBean.field}" />

But in the case of

getF1();

JSF sees that the second character is 1 that does not have an uppercase or lowercase. so basing that the JSF uses setters/getters to get the field. because of the ManagedBean specification after the get/set is an uppercase, thats why it you can use it in managed bean as:

 <h:outputText value="#{managedBean.f1}" />

sorry for the bad explanation, hope you understand

like image 28
steven0529 Avatar answered Sep 21 '22 04:09

steven0529