java - h:message elements with for attribute do not present messages in JSF 2 -
i have following authentication form in jsf running on glassfish 3:
<h:messages globalonly="true" errorclass="erro" /> <h:form id="form-login"> <h:outputtext value="user" /> <h:message for="login-user" /> <h:inputtext id="login-user" value="#{authenticationbean.user}" showmessages="true" /> <h:outputtext value="password" /> <h:inputsecret id="login-password" value="#{authenticationbean.password}" /> <h:commandbutton id="log in" action="#{authenticationbean.login}" value="autenticar-se" /> </h:form>
the authentication.login
method 1 below:
public string login() { user = userdao.authenticate(user, password); if (user != null) { facesmessage message = new facesmessage("welcome!"); message.setseverity(facesmessage.severity_info); facescontext.getcurrentinstance().addmessage(null, message); return "ok"; } else { facesmessage message = new facesmessage("authentication failed"); message.setseverity(facesmessage.severity_error); facescontext.getcurrentinstance().addmessage(null, message); if ("".equals(user)) { message = new facesmessage("you need give user"); message.setseverity(facesmessage.severity_error); facescontext.getcurrentinstance().addmessage( "login-user", message); } return "fail"; } }
i try show specific message if user login not given, line <h:message for="login-user" />
. not work, though: if click in submit button empty user field, <h:messages globalonly="true" errorclass="erro" />
element presents "authentication failed"
<h:message for="login-user" />
component not present "you need give user"
text expected. instead presents no message @ all. also, following warning appears on log:
[lifecycle] warning: facesmessage(s) have been enqueued, may not have been displayed. sourceid=login-user[severity=(error 2), summary=(you need give user), detail=(you need give user)]
(actually, looked translation in google, since real message got in portuguese:
info: warn: facesmessage(s) foram enfileirados, mas podem não ter sido exibidos. sourceid=login-usuario[severity=(error 2), summary=(you need give user), detail=(you need give user)]
)
what doing wrong?
thanks in advance!
you need specify client id, not component id. based on given view code, that'll be:
facescontext.getcurrentinstance().addmessage( "form-login:login-user", message);
if doesn't work, need open page in browser , view generated html source. should same id
attribute of generated html <input>
element.
unrelated problem, use null
client id , use <h:messages globalonly="true">
general validation errors. may want take same approach. "you need give user" part i'd use required="true"
instead.
Comments
Post a Comment