Java 登录表单没有';密码错误时不显示验证消息
我有一个由JSF页面+托管bean+EJB组成的登录机制,它工作得很好,但唯一的错误是当密码输入错误时,我看不到错误消息。有什么想法吗 当密码输入错误时,我会在浏览器中看到: 如果我没有在EJB中的捕获处抛出ValidationException(请参见下面的EJB代码),那么我可以避免这种情况,但是当输入错误密码时,我在表单中根本看不到任何验证消息 这是页面上表单的代码:Java 登录表单没有';密码错误时不显示验证消息,java,jsf,jakarta-ee,jsf-2,java-ee-6,Java,Jsf,Jakarta Ee,Jsf 2,Java Ee 6,我有一个由JSF页面+托管bean+EJB组成的登录机制,它工作得很好,但唯一的错误是当密码输入错误时,我看不到错误消息。有什么想法吗 当密码输入错误时,我会在浏览器中看到: 如果我没有在EJB中的捕获处抛出ValidationException(请参见下面的EJB代码),那么我可以避免这种情况,但是当输入错误密码时,我在表单中根本看不到任何验证消息 这是页面上表单的代码: <h:form> <p:panel>
<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>
在EJB中,如果在catch块中抛出ValidationException,我认为我犯了一个错误,因为我认为在前端显示错误消息应该是托管bean的任务。我不明白为什么表单没有像密码错误时字段为空时那样显示错误(电子邮件错误时正确显示验证消息)
更新
将EJB上的saveUserState()方法更改为:
// Login
public boolean saveUserState(String email, String password) {
// 1-Send query to database to see if that user exist
Query query = em
.createQuery("SELECT r FROM Role r WHERE r.email=:emailparam AND r.password=:passwordparam");
query.setParameter("emailparam", email);
query.setParameter("passwordparam", password);
// 2-If the query returns the user(Role) object, store it somewhere in
// the session
List<Object> tmpList = query.getResultList();
if (tmpList.isEmpty() == false) {
Role role = (Role) tmpList.get(0);
if (role != null && role.getEmail().equals(email)
&& role.getPassword().equals(password)) {
FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap().put("userRole", role);
// 3-return true if the user state was saved
System.out.println(role.getEmail() + role.getPassword());
return true;
}
}
// 4-return false otherwise
return false;
}
您不应该在EJB方法中抛出JSF
ValidatorException
,而应该在JSFvalidate()方法中抛出
我不确定导致此异常的EJB中出现了什么错误,可能是getSingleResult()
根本没有返回一个结果(因此为零或多个)。您需要通过使用getResultList()
或修改您的数据模型来相应地更改/处理此问题,因为用户没有单一角色。我想您只需要getResultList()
,因为您显然允许null
结果。我现在使用getResultsList(),还检查了查询返回的内容。只返回了一个Role实例。当AuthenticationEJB.checkCredentials(emailInput、passwordInput)的计算结果为false时,validate方法已引发验证异常。我不明白虫子在哪里。我认为我的数据模型还可以。我有3个用户(买方、卖方和管理员),每个用户都与角色有一个OneOne关系。角色实体保存变量email和password。另外(当我检查凭证时,查询只访问角色实体),这只是一个猜测。答案在捕获的异常中,您通过抛出验证异常来忽略该异常。您需要记录捕获的异常以了解原因。如果你能应付,就去做。如果您无法处理它,请将其作为一般/业务运行时异常或其他内容重新引用。我认为我们相互误解了。你现在到底有什么问题?编辑:当没有单一结果时,更新的checkCredentials()
将引发异常。您需要将其全部替换为返回checkEmailExists.getResultList().size()==1代码>现在的确切问题是,当凭据错误时,登录不会显示验证错误。我确实修改了方法saveUserState()和checkUserCredentials(),但我认为错误并不存在。我认为问题出在托管bean中,由于某种原因,当验证为true时,错误不会被记录:如果(authenticationjb.checkCredentials(emailInput,passwordInput)没有异常,我确定返回列表的大小始终为1(请参阅更新)。另外,我确定我刚刚检查的数据库中没有重复项。
@Stateful(name = "ejbs/AuthentificationEJB")
public class AuthentificationEJB implements IAuthentificationEJB {
@PersistenceContext
private EntityManager em;
// Login
public boolean saveUserState(String email, String password) {
// 1-Send query to database to see if that user exist
Query query = em
.createQuery("SELECT r FROM Role r WHERE r.email=:emailparam AND r.password=:passwordparam");
query.setParameter("emailparam", email);
query.setParameter("passwordparam", password);
// 2-If the query returns the user(Role) object, store it somewhere in
// the session
try {
Role role = (Role) query.getSingleResult();
if (role != null && role.getEmail().equals(email)
&& role.getPassword().equals(password)) {
FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap().put("userRole", role);
// 3-return true if the user state was saved
System.out.println(role.getEmail() + role.getPassword());
return true;
}
} catch (Exception e) {
FacesMessage msg = new FacesMessage("Pogresan email ili lozinka");
throw new ValidatorException(msg);
}
// 4-return false otherwise
return false;
}
// Logout
public void releaseUserState() {
// 1-Check if there is something saved in the session(or wherever the
// state is saved)
if (!FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap().isEmpty()) {
FacesContext.getCurrentInstance().release();
}
// 2-If 1 then flush it
}
// Check if user is logged in
public boolean checkAuthentificationStatus() {
// 1-Check if there is something saved in the session(This means the
// user is logged in)
if ((FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap().get("userRole") != null)) {
// 2-If there is not a user already loged, then return false
return true;
}
return false;
}
@Override
public boolean checkCredentials(String email, String password) {
Query checkEmailExists = em
.createQuery("SELECT COUNT(r.email) FROM Role r WHERE r.email=:emailparam AND r.password=:passwordparam");
checkEmailExists.setParameter("emailparam", email);
checkEmailExists.setParameter("passwordparam", password);
long matchCounter = 0;
matchCounter = (Long) checkEmailExists.getSingleResult();
if (matchCounter > 0) {
return true;
}
return false;
}
}
// Login
public boolean saveUserState(String email, String password) {
// 1-Send query to database to see if that user exist
Query query = em
.createQuery("SELECT r FROM Role r WHERE r.email=:emailparam AND r.password=:passwordparam");
query.setParameter("emailparam", email);
query.setParameter("passwordparam", password);
// 2-If the query returns the user(Role) object, store it somewhere in
// the session
List<Object> tmpList = query.getResultList();
if (tmpList.isEmpty() == false) {
Role role = (Role) tmpList.get(0);
if (role != null && role.getEmail().equals(email)
&& role.getPassword().equals(password)) {
FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap().put("userRole", role);
// 3-return true if the user state was saved
System.out.println(role.getEmail() + role.getPassword());
return true;
}
}
// 4-return false otherwise
return false;
}
public boolean checkCredentials(String email, String password) {
Query checkEmailExists = em
.createQuery("SELECT COUNT(r) FROM Role r WHERE r.email=:emailparam AND r.password=:passwordparam");
checkEmailExists.setParameter("emailparam", email);
checkEmailExists.setParameter("passwordparam", password);
int matchCounter = 0;
matchCounter = checkEmailExists.getResultList().size();
if (matchCounter == 1) {
return true;
}
return false;
}