Cant access some (Capitalized) fields in JSF controller?

谁说胖子不能爱 提交于 2019-12-04 06:02:04
mabi

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?

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

The attribute name does not interfere.

But your method declaration should follow the convention.

private String FIELD;

 public String getField() {
        return FIELD;
    }

xhtml

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

The javadoc documentation does not explicitly tells us that naming convention is mandatory. Rather it tell us it's optional and recommended

For simple properties the accessor type signatures are:

void setFoo(PropertyType value); // simple setter PropertyType getFoo(); //

simple getter GetFoo and setFoo are simply example names. Accessor methods can have arbitrary names. However for standard naming conventions for accessor methods see the design patterns described in Section 8.3.

8.3.1 Simple properties

By default, we use design patterns to locate properties by looking for methods of the form:

public <PropertyType> get<PropertyName>(); 
public void set<PropertyName>(<PropertyType> a);

If we discover a matching pair of “get” and “set” methods that take and return the same type, then we regard these methods as defining a read-write property whose name will be “”. We will use the “get” method to get the property value and the “set” method to set the property value. The pair of methods may be located either in the same class or one may be in a base class and the other may be in a derived class.

If we find only one of these methods, then we regard it as defining either a read-only or a writeonly property called “” By default we assume that properties are neither bound nor constrained (see Section 7). So a simple read-write property “foo” might be represented by a pair of methods:

public Wombat getFoo(); 
public void setFoo(Wombat w);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!