我想做一个可以正常工作的注册表单:
<h:form id="register_form">
<h:panelGrid columns="3">
<h:outputLabel for="email" value="E-mail:" />
<h:inputText id="email" value="#{userc.userb.user.email}">
<f:ajax event="blur" render="m_email" />
</h:inputText>
<h:message id="m_email" for="email" ajaxRendered="false" />
<h:outputLabel for="password" value="Passoword" />
<h:inputSecret id="password" value="#{userc.userb.user.password}">
<f:validator validatorId="confirmPasswordValidator" />
<f:attribute name="confirm" value="#{confirmPassword.submittedValue}" />
</h:inputSecret>
<h:message id="m_password" for="password" />
<h:commandButton value="Registerr" action="#{userc.register}">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
<h:messages globalOnly="true" layout="table" />
</h:panelGrid>
</h:form>
这是我的控制器:
public void register(){
FacesMessage message;
try {
userb.encryptPassword();
userEAO.create(userb.getUser());
message = new FacesMessage("Register successfully!");
FacesContext.getCurrentInstance().addMessage(null, message);
} catch (Exception e) {
// 1062 : duplicate entry
if (e.getMessage().contains("Duplicate")){
message = new FacesMessage("User already registered, please try another email");
FacesContext.getCurrentInstance().addMessage(null, message);
}else{
message = new FacesMessage("An error occured, try again please");
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
}
因此,例如,如果某个用户尝试注册自己两次,我会返回此消息,说这是重复的,但我想知道这是否是最好的方法。
如何处理这些异常?