1

I'm trying to reference constant with EL on my JSF page (https://java.net/projects/el-spec/pages/StaticField), but I'm stuck on this exception:

javax.servlet.ServletException: /faces/signup.xhtml @18,85 maxlength="#{signUpBean.USERNAME_MAXLENGTH}": Property 'USERNAME_MAXLENGTH' not found on type com.foo.SignUpBean

I'm using Tomcat 8.0.0-RC1 and here is my backing bean and input declaration:

Bean:

@ManagedBean
@RequestScoped
public class SignUpBean implements Serializable {

    public static final int USERNAME_MAXLENGTH = 30;
    ...

}

Input field on my page:

<input type="text" jsf:id="username" jsf:value="#{signUpBean.username}" maxlength="#{signUpBean.USERNAME_MAXLENGTH}" />

Update:

With maxlength="#{(com.foo.SignUpBean).USERNAME_MAXLENGTH}" I'm getting java.lang.NullPointerException: Argument Error: Parameter value is null

perak
  • 1,310
  • 5
  • 20
  • 31
  • possible duplicate of [Having a "constants"-class in JSF](http://stackoverflow.com/questions/12668263/having-a-constants-class-in-jsf) – Alex Edwards Aug 26 '13 at 17:46

1 Answers1

0

First, off, see BalusC's updated answer for how to properly use constants in EL 3.0 expressions.

Now, with that said, if you just want to get your code working right now with the released version of GlassFish 4.0, you can modify your backing bean in the following way. Your backing bean doesn't have a getter/setter for your field. Backing beans need to have JavaBeans-style properties with getters/setters.

@ManagedBean
@RequestScoped
public class SignUpBean implements Serializable {

    private final int USERNAME_MAXLENGTH = 30;
    private String username;

    ...

    public int getUSERNAME_MAXLENGTH() {
        return USERNAME_MAXLENGTH;
    }

    public void setUSERNAME_MAXLENGTH(int i) {
        // don't set anything, because this is a constant
    } 

    public String getUsername() {
        return username;
    }

    public void setUsername(String u) {
        username = u;
    }

}

Then your Facelets tag:

<input type="text" 
       jsf:id="username" 
       jsf:value="#{signUpBean.username}" 
       jsf:maxlength="#{signUpBean.USERNAME_MAXLENGTH}" />

That is, don't make the field static. I do recommend switching this over to the correct syntax once JSF is updated in the RI/GlassFish 4.0.

Edit: fixed input tag to use jsf:maxlength.

Community
  • 1
  • 1
Ian Evans
  • 1,357
  • 10
  • 10