我正在使用一些 primefaces 组件实现应用程序管理的登录机制。当正确输入凭据时,我成功保存了用户状态,并且在错误输入时使对话框抖动。
问题是,当正确输入凭据时,该对话框也应该消失。而不是那样。当凭据正确时,对话框会抖动并且不会自动消失。我该如何解决这个问题?
这是 JSF 页面的一部分,其中包含我提到的对话框:
<p:dialog id="dialog" header="Login" widgetVar="dlg" modal="true"
width="400" resizable="false" draggable="false" fixedCenter="true">
<h:form>
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="username" value="Em@il: *" />
<p:inputText value="#{securityController.email}" id="email"
required="true" label="email" validator="#{securityController.validateEmail}"/>
<h:outputLabel for="password" value="Password: * " />
<h:inputSecret value="#{securityController.password}" id="password"
required="true" label="password" validator="#{securityController.validatePassword}"/>
<f:facet name="footer">
<p:commandButton value="Login" update="growl"
oncomplete="handleLoginRequest(xhr, status, args)"
actionListener="#{securityController.logIn()}"/>
</f:facet>
</h:panelGrid>
</h:form>
</p:dialog>
<script type="text/javascript">
function handleLoginRequest(xhr, status, args) {
if(args.validationFailed || !args.loggedIn) {
jQuery('#dialog').parent().effect("shake", { times:3 }, 100);
} else {
dlg.hide();
jQuery('#loginLink').fadeOut();
}
}
</script>
这是执行部分验证的托管 bean:
@ManagedBean
@RequestScoped
public class SecurityController {
@EJB
private IAuthentificationEJB authentificationEJB;
private String email;
private String password;
private String notificationValue;
public void logIn() {
if(authentificationEJB.saveUserState(email, password)) {
notificationValue = "Dobro dosli";
}
}
public void validateEmail(FacesContext context, UIComponent validate,
Object value) {
String inputFromField = (String) value;
String simpleTextPatternText = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern textPattern = null;
Matcher emailMatcher = null;
textPattern = Pattern.compile(simpleTextPatternText);
emailMatcher = textPattern.matcher(inputFromField);
if (inputFromField.length() >= 30) {
FacesMessage msg = new FacesMessage(
"Pre dugacak email(Ne dozvoljeno)");
throw new ValidatorException(msg);
}
if (inputFromField.length() <= 0) {
FacesMessage msg = new FacesMessage("Zaboraviliste email adresu");
throw new ValidatorException(msg);
}
if (!emailMatcher.matches()) {
FacesMessage msg = new FacesMessage(
"Ne ispravan email. Napisiti email u ispravnom obliku. (Np: markomarkovic@mail.com)");
throw new ValidatorException(msg);
}
}
public void validatePassword(FacesContext context, UIComponent validate,
Object value) {
String inputFromField = (String) value;
String simpleTextPatternText = "^[a-zA-Z0-9]+$";
Pattern textPattern = null;
Matcher passwordMatcher = null;
textPattern = Pattern.compile(simpleTextPatternText);
passwordMatcher = textPattern.matcher(inputFromField);
if (inputFromField.length() <= 0) {
FacesMessage msg = new FacesMessage("Zaboraviliste lozinku");
throw new ValidatorException(msg);
}
if (!passwordMatcher.matches()) {
FacesMessage msg = new FacesMessage(
"Lozinka samo moze da zadrzi slova od A do Z i broja od 0 do 9");
throw new ValidatorException(msg);
}
}
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
public void setEmail(String email) {
this.email = email;
}
public void setPassword(String password) {
this.password = password;
}
public String getNotificationValue() {
return notificationValue;
}
public void setNotificationValue(String notificationValue) {
this.notificationValue = notificationValue;
}
最佳答案
我解决了这个问题,我修改了 login() 方法,如下所示:
public void logIn() {
RequestContext context = RequestContext.getCurrentInstance();
boolean loggedIn = false;
if(authentificationEJB.saveUserState(email, password)) {
notificationValue = "Dobro dosli";
loggedIn = true;
context.addCallbackParam("loggedIn", loggedIn);
}
}
我对 javascript 不太了解,但我想该对话框发现了来自支持 bean 的某种响应消息。
无论如何,谢谢:)
关于java - 当用户正确插入凭据时,为什么登录对话框不会消失?(JSF 2.0。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5648511/