[英]Custom message with FacesContext.getCurrentInstance().addMessage is not displayed in page (JSF)
[英]h:message not displayed in jsf page
我在jsf中使用登录字段制作了一个简单的登录页面。 如果登录不正确(在数据库中未找到用户),则页面上应出现一条消息,但不显示任何内容...
ps:如果用户不输入任何内容,则显示“ requiredMessage”中指定的消息。
这是我的jsf页面的代码:
<h:body>
<p>Identification</p>
<h:form prependId="false">
login : <h:inputText id="log" value="#{filesystemCtrl.login}" required="true" requiredMessage="Please enter your username"/>
<h:message for="log" style="color:red"/><br/>
<h:commandButton action="#{filesystemCtrl.identifier()}" value="Login"/>
</h:form>
<div id="footer" align="center">
<ui:insert name="footer">
<ui:include src="footer.xhtml" />
</ui:insert >
</div>
</h:body>
这是managedBean的一部分:
public String identifier() {
client = filesystemFacade.identifier(login);
if (client == null) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage("log", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "No user found !"));
return "failure";
} else {
return "success";
}
}
您的操作方法将在所有路径中返回一个导航用例。 我猜您实际上没有failure
导航(即failure.xhtml
视图),因此当您的client
对象为null时,您仍然会看到相同的视图而没有更新消息。 应该发生的是重定向到failure
视图,这样浏览器将没有机会显示错误消息。 检查服务器日志中是否存在导航错误。
由于您希望浏览器停留在同一页面(视图)上并更新错误消息,因此您的方法应在添加消息后返回null
,而不是failure
。
回发后,您需要更新消息组件。 给它一个ID作为
<h:message for="log" id="msgLog" style="color:red"/><br/>
然后将您的按钮更改为
<h:commandButton action="#{filesystemCtrl.identifier()}" value="Login"
update="msgLog" />
</h:form>
addMessage的Javadoc写道:
如果clientId不为null,则将FacesMessage追加到与指定的客户端标识符关联的消息集。
您已通过组件ID。 不一样 因此,您的消息可能与一个不存在的组件相关联,因此不显示。
构成客户端标识符的算法由UIComponent.getClientId()
的javadoc描述。
在更实际的说明中,我将在h:message的呈现器中设置一个断点,以发现它期望显示消息的标识符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.