Java 当用户输入错误密码时,登录表单中未显示验证消息
我试图理解为什么我的登录表单在密码输入错误时不显示显示“错误的电子邮件或密码”的验证消息。在所有其他情况下,它工作正常(仅情况4不工作): 案例1工作正常(未提供输入): 案例2工作正常(仅为电子邮件提供输入): 案例3工作正常(仅输入密码): 案例4不起作用(两个输入都错误) 案例4无法正常工作。以下是源代码: JSF页面上的表单:Java 当用户输入错误密码时,登录表单中未显示验证消息,java,jsf,jakarta-ee,jsf-2,java-ee-6,Java,Jsf,Jakarta Ee,Jsf 2,Java Ee 6,我试图理解为什么我的登录表单在密码输入错误时不显示显示“错误的电子邮件或密码”的验证消息。在所有其他情况下,它工作正常(仅情况4不工作): 案例1工作正常(未提供输入): 案例2工作正常(仅为电子邮件提供输入): 案例3工作正常(仅输入密码): 案例4不起作用(两个输入都错误) 案例4无法正常工作。以下是源代码: JSF页面上的表单: <h:form> <p:panel> <h:outpu
<h:form>
<p:panel>
<h:outputText value="*Em@il:" />
<h:inputText id="email" value="#{securityController.email}" binding="#{emailComponent}"/>
<br/>
<h:outputText value="*Lozinka: " />
<h:inputSecret id="password" value="#{securityController.password}" validator="#{securityController.validate}">
<f:attribute name="emailComponent" value="#{emailComponent}" />
</h:inputSecret>
<br/>
<span style="color: red;"><h:message for="password"
showDetail="true" /></span>
<br/>
<h:commandButton value="Login" action="#{securityController.logIn()}"/>
</p:panel>
</h:form>
从输入字段获取值的托管bean
@ManagedBean
@RequestScoped
public class SecurityController {
@EJB
private IAuthentificationEJB authentificationEJB;
private String email;
private String password;
private String notificationValue;
public String logIn() {
if (authentificationEJB.saveUserState(email, password)) {
notificationValue = "Dobro dosli";
return "main.xhtml";
} else {
return "";
}
}
public void validate(FacesContext context, UIComponent component,
Object value) throws ValidatorException {
UIInput emailComponent = (UIInput) component.getAttributes().get(
"emailComponent");
String email = "";
String password = "";
email = (String) emailComponent.getValue();
password = (String) value;
String emailInput = email;
String emailPatternText = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern emailPattern = null;
Matcher emailMatcher = null;
emailPattern = Pattern.compile(emailPatternText);
emailMatcher = emailPattern.matcher(emailInput);
String passwordInput = password;
String alphanumericPattern = "^[a-zA-Z0-9]+$";
Pattern passwordPattern = null;
Matcher passwordMatcher = null;
passwordPattern = Pattern.compile(alphanumericPattern);
passwordMatcher = passwordPattern.matcher(passwordInput);
if (!emailMatcher.matches() && !passwordMatcher.matches()) {
if (authentificationEJB.checkCredentials(emailInput, passwordInput) == false) {
FacesMessage msg = new FacesMessage(
"Pogresan email ili lozinka");
throw new ValidatorException(msg);
}
}
if(emailInput == null || passwordInput == null) {
FacesMessage msg = new FacesMessage("Pogresan email ili lozinka");
throw new ValidatorException(msg);
}
if (passwordInput.length() <= 0 || emailInput.length() <= 0) {
FacesMessage msg = new FacesMessage("Pogresan email ili lozinka");
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;
}
}
@ManagedBean
@请求范围
公共类安全控制器{
@EJB
私有IAAuthentificationEJB AuthenticationEJB;
私人字符串电子邮件;
私有字符串密码;
私有字符串notificationValue;
公共字符串登录(){
if(authentificationEJB.saveUserState(电子邮件、密码)){
notificationValue=“Dobro dosli”;
返回“main.xhtml”;
}否则{
返回“”;
}
}
公共无效验证(FacesContext上下文、UIComponent组件、,
对象值)引发异常{
UIInput emailComponent=(UIInput)component.getAttributes().get(
“电子邮件组件”);
字符串email=“”;
字符串密码=”;
email=(字符串)emailComponent.getValue();
密码=(字符串)值;
字符串emailInput=电子邮件;
字符串emailPatternText=“^[[U A-Za-z0-9-]+(\\.[U A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$;
Pattern=null;
Matcher emailMatcher=null;
emailPattern=Pattern.compile(emailPatternText);
emailMatcher=emailPattern.matcher(emailInput);
字符串密码输入=密码;
字符串alphanumericPattern=“^[a-zA-Z0-9]+$”;
Pattern passwordPattern=null;
Matcher passwordMatcher=null;
passwordPattern=Pattern.compile(alphanumericPattern);
passwordMatcher=passwordPattern.matcher(passwordInput);
如果(!emailMatcher.matches()&&!passwordMatcher.matches()){
if(authentificationEJB.checkCredentials(emailInput,passwordInput)==false){
FacesMessage msg=新的FacesMessage(
“Pogresan电子邮件ili lozinka”);
抛出新的验证异常(msg);
}
}
if(emailInput==null | | passwordInput==null){
FacesMessage msg=新的FacesMessage(“Pogresan电子邮件ili lozinka”);
抛出新的验证异常(msg);
}
if(passwordInput.length()
因此,当电子邮件不匹配,密码不匹配,凭据不匹配时,将显示错误消息
这不是您想要的。在案例4中,电子邮件确实匹配。您想要:
if (!emailMatcher.matches() || !passwordMatcher.matches() || !authentificationEJB.checkCredentials(emailInput, passwordInput)) {
FacesMessage msg = new FacesMessage("Pogresan email ili lozinka");
throw new ValidatorException(msg);
}
这将在电子邮件不匹配、密码不匹配或凭据不匹配时显示错误
因此,当电子邮件不匹配,密码不匹配,凭据不匹配时,将显示错误消息
这不是您想要的。在案例4中,电子邮件确实匹配。您想要:
if (!emailMatcher.matches() || !passwordMatcher.matches() || !authentificationEJB.checkCredentials(emailInput, passwordInput)) {
FacesMessage msg = new FacesMessage("Pogresan email ili lozinka");
throw new ValidatorException(msg);
}
当电子邮件不匹配、密码不匹配或凭据不匹配时,这将显示错误。是。错误的条件是:)谢谢你的帮助:)不客气。请注意,这使得另外两个如果检查也是多余的,我相信。你可以删除它们。是的,我会在后面做一些重构:)@上帝,你的讽刺有点双重性。我不确定如何解释。这是对我还是对sfrj?巴卢斯克,我不是故意让它混淆的。我刚刚听到了在OP的另一篇文章中,一开始我引用了你的博客,所以我很高兴看到你的名字出现在这个答案上。现在我看到你也回答了这个问题,这是我之前没有注意到的。如果我担心你或其他什么的话,我道歉;我不是故意的。是的,就是这样。错误的条件是:)谢谢你的help:)不客气。请注意,这使得另外两个如果检查也是多余的,我相信。你可以删除它们。是的,我会在后面做一些重构:)@上帝,你的讽刺有点双重。我不知道如何解释。这是对我还是对sfrj的?@BalusC,我不是故意让它混淆的。我刚才看到了另一篇帖子这篇文章一开始就引用了你的博客,所以我很高兴看到你的名字出现在这个答案上。现在我看到你也回答了这个问题,这是我之前没有注意到的。如果我担心你或其他什么,我向你道歉;我的意思不是这样的。
if (!emailMatcher.matches() || !passwordMatcher.matches() || !authentificationEJB.checkCredentials(emailInput, passwordInput)) {
FacesMessage msg = new FacesMessage("Pogresan email ili lozinka");
throw new ValidatorException(msg);
}