在 JSF 2.0 中,如何覆盖所需的消息?我正在使用 Primefaces。这是我的代码:
<h:body>
    <p:growl id="growl" showDetail="true"/>
    <h:panelGroup layout="block" styleClass="login-div">
        <h:form id="login">
            <p:panel header="Login">
                <h:panelGrid columns="2">
                    <p:outputLabel for="username" value="Username" />
                    <p:inputText id="username" value="#{authController.username}"
                        autocomplete="off" required="true"
                        requiredMessage="Username is required" />
                    <p:outputLabel for="password" value="Password" />
                    <p:password id="password" value="#{authController.password}"
                        autocomplete="off" required="true"
                        requiredMessage="Password is required" />
                </h:panelGrid>
                <p:commandButton id="submit" value="Login"
                    actionListener="#{authController.login}" update=":growl" />
            </p:panel>          
        </h:form>
    </h:panelGroup>
    <p:ajaxStatus styleClass="ajaxLodingStatus">
        <f:facet name="start">
            <p:graphicImage value="/resources/images/loading.gif" />
        </f:facet>
        <f:facet name="complete">
            <p:outputLabel value="" />
        </f:facet>
    </p:ajaxStatus>
</h:body>
现在Growl显示:“需要用户名”作为摘要和详细信息FacesMessage。密码字段也是如此。
现在从actionListener命令按钮中,我以我想要的方式显示登录尝试失败的时间:
getFacesContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid Credential"));
但我想显示“无效输入”作为摘要和“需要用户名”作为详细信息。
如果我从后端验证这两个输入字段并将其添加FacesMessage为:
if(username == null || username.trim().length() == 0) {
    getFacesContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid input", "Username is required."));
}
它显示了我需要的东西。但是这样就不需要required="true"在输入组件中指定属性了。
但我想使用这个required属性也想自FacesMessage定义Growl. 我怎样才能做到这一点?
更新:
这是我的支持bean:
@ManagedBean(name = "authController")
@ViewScoped
public class AuthController extends BaseWebController implements Serializable {
    private static final long serialVersionUID = 2894837128903597901L;
    private String username;
    private String password;
    public AuthController() {
        super();
    }
    public void login(ActionEvent event) {
        getFacesContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid Credential"));
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}
目前actionaListener只有在有一些输入时才会触发。否则,当字段为空白时,将Growl显示:

点击登录按钮后:

我想要的是,当输入用户名无法按要求验证时,Growl将显示:
- 摘要:输入无效。
- 详细信息:用户名是必需的。
对于输入密码:
- 摘要:输入无效。
- 详细信息:需要密码。
我怎样才能做到这一点?可能吗?
