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

4

1 回答 1

0

首先,请参阅BalusC 的更新答案,了解如何在 EL 3.0 表达式中正确使用常量。

现在,话虽如此,如果您只想让您的代码立即使用已发布的 GlassFish 4.0 版本,您可以通过以下方式修改您的支持 bean。您的支持 bean 没有针对您的字段的 getter/setter。支持 bean 需要具有带有 getter/setter 的 JavaBeans 样式的属性。

@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;
    }

}

然后你的 Facelets 标签:

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

也就是说,不要使字段静态。一旦 JSF 在 RI/GlassFish 4.0 中更新,我确实建议将其切换为正确的语法。

编辑:input要使用的固定标签jsf:maxlength

于 2013-08-27T17:21:41.497 回答