Tap*_*ose 4 input messages requiredfieldvalidator primefaces jsf-2
在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>
Run Code Online (Sandbox Code Playgroud)
现在Growl
显示:"用户名是必需的"作为摘要和详细信息FacesMessage
.密码字段的内容相同.
现在,从actionListener
命令按钮我显示登录尝试失败时,以我想要的方式:
getFacesContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid Credential"));
Run Code Online (Sandbox Code Playgroud)
但我希望将"无效输入"显示为摘要,并将"用户名是必需的"显示为详细信息.
如果我从后端验证这两个输入字段并添加FacesMessage
as:
if(username == null || username.trim().length() == 0) {
getFacesContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid input", "Username is required."));
}
Run Code Online (Sandbox Code Playgroud)
它展示了我的需求.但是,不需要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;
}
}
Run Code Online (Sandbox Code Playgroud)
而且目前actionaListener
只有在有一些投入时才会开火.否则,当字段为空时,Growl
显示:
点击登录按钮后:
我想要的是,当输入用户名无法按要求验证时,Growl
将显示:
并输入密码:
我怎样才能做到这一点?可能吗?
Tap*_*ose 11
我找到了解决方案:
我需要自定义验证错误消息.
我创建了messages.properties文件,其中我写了:
javax.faces.component.UIInput.REQUIRED=Invalid input.
javax.faces.component.UIInput.REQUIRED_detail={0} is required.
Run Code Online (Sandbox Code Playgroud)
并在faces-config中注册为:
com.edfx.adb.common.properties.messages
我得到了这个:
我也requiredMessage
从输入组件中删除了该属性.
一些有用的链接: