我在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";
}
}
答案 0 :(得分:1)
您的操作方法会在所有路径中返回导航案例。我猜你实际上没有failure
导航(即failure.xhtml
视图)所以当你的client
对象为空时,你仍会看到没有更新消息的相同视图。应该发生的是重定向到failure
视图,这样,浏览器就没有机会显示错误消息。检查服务器日志以查找导航错误。
由于您希望浏览器保持在同一页面(查看)并更新错误消息,因此您的方法应在添加消息后返回null
,而不是failure
。
答案 1 :(得分:-1)
您需要在回发后更新邮件组件。给它一个id为
<h:message for="log" id="msgLog" style="color:red"/><br/>
然后将按钮更改为
<h:commandButton action="#{filesystemCtrl.identifier()}" value="Login"
update="msgLog" />
</h:form>
答案 2 :(得分:-1)
addMessage的javadoc写道:
如果clientId不为null,则将FacesMessage附加到与指定客户端标识符关联的消息集。
您已通过组件ID。那不一样。因此,您的消息可能与不存在的组件相关联,因此不会显示。
用于形成客户端标识符的算法由UIComponent.getClientId()
的javadoc描述。
在更实际的说明中,我在h:message的渲染器中设置了一个断点,以发现它希望显示该消息的标识符。