我想在我的 jsf 页面中通过 <h:messages/>
通知用户 SQLException。
我有以下功能
public String createAccount() throws RollbackFailureException,SQLIntegrityConstraintViolationException,Exception{
String status = "failure";
EntityManager em = null;
try {
logger.log(Level.INFO,"Registering the user");
utx.begin();
em = getEntityManager();
em.persist();
status = "success";
utx.commit();
} catch (Exception ex) {
try {
utx.rollback();
} catch (Exception re) {
throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
}
if (findDetails(details.getId()) != null) {
throw new SQLIntegrityConstraintViolationException("Your Account cannot be setup as the Login Id already exists.Kindly choose a different Login ID", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
如何用jsf显示错误消息?
谢谢:)
最佳答案
在支持 bean 中使用以下内容来添加消息:
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
...
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
FacesContext.getCurrentInstance().addMessage(null, facesMsg);
严重性也可以是 SEVERITY_WARN
或 SEVERITY_INFO
。您可以将 clientId 作为 addMessage
的第一个参数。如果为null
,则该消息是全局消息,并显示在h:messages
中。
关于java - 在 Jsf 中显示有关 SQLIntegrityConstraintViolationException 异常的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9430748/