Constants in EL 3.0 - Property not found

倾然丶 夕夏残阳落幕 提交于 2019-12-11 07:46:59

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/18449495/constants-in-el-3-0-property-not-found

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