16 jsf jsp exception-handling jsf-1.2
如果我的业务层中有异常(例如我的JDBC连接bean中的SQL异常),我如何将自定义消息传播到全局error.jsp
页面?
McD*_*ell 19
虽然您可以使用导航规则(假设表单发布)重定向到错误页面,但JSF 1.x不提供此类型的任何隐式错误处理...
<navigation-case>
<description>
Handle a generic error outcome that might be returned
by any application Action.
</description>
<display-name>Generic Error Outcome</display-name>
<from-outcome>loginRequired</from-outcome>
<to-view-id>/must-login-first.jsp</to-view-id>
</navigation-case>
Run Code Online (Sandbox Code Playgroud)
...或使用重定向...
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext extContext = context.getExternalContext();
String url = extContext.encodeActionURL(extContext
.getRequestContextPath()
+ "/messages.faces");
extContext.redirect(url);
Run Code Online (Sandbox Code Playgroud)
我建议查看JSF规范以获取更多详细信息.
可以根据需要将错误消息放在请求范围/会话范围/ url参数上.
假设有一个Servlet容器,您可以使用通常的web.xml错误页面配置.
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/errorPage.faces</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)
在您的支持bean中,您可以在RuntimeException中包装并抛出已检查的异常.
一些JSF实现/框架将捕获这些错误(Apache MyFaces/Facelets),因此您将不得不配置它们.
小智 6
我在站点中创建了一个错误页面,并将错误的堆栈跟踪放入此代码中,首先将其放在web.xml中
<error-page>
<error-code>500</error-code>
<location>/errorpage.jsf</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)
虽然jsf页面有这个代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<h:head>
<h:outputStylesheet name="EstilosOV.css" library="css" />
<!-- <h:outputScript name="sincontext.js" library="scripts"/> -->
</h:head>
<h:body >
<table width="338" border="0" align="center" bordercolor="#FFFFFF">
<tr bgcolor="#1D2F68">
<td colspan="4" height="35" class="TablaHeader2"> PLAN SEGURO</td>
</tr>
<tr bgcolor="#FDF2AA">
<td colspan="4" class="HeaderExcepcion">
<h:graphicImage library="images" name="errormark.jpg"/>
<font size="5px" color="#CC0000" >Error de ejecución</font>
</td>
</tr>
<tr >
<td colspan="3" class="anuncioWarning2">Ocurrio un error al realizar la operación, favor de intentarlo nuevamente, si el error aún se presenta, favor de notificarlo a soporte técnico al 5147-31-00 extensión 6893</td>
</tr>
<tr>
<td height="17" colspan="3" class="TablaHeader2"></td>
</tr>
</table>
<h:form id="formerror">
<h:inputHidden id="valuestack" value="#{errorDisplay.stackTrace}"></h:inputHidden>
</h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
请注意,motif允许您调试堆栈跟踪,以了解隐藏字段中的异常.让我们看看如何定义类恢复StackTrace
public class ErrorDisplay implements Serializable{
private static final long serialVersionUID = 6032693999191928654L;
public static Logger LOG=Logger.getLogger(ErrorDisplay.class);
public String getContacto() {
return "";
}
public String getStackTrace() {
FacesContext context = FacesContext.getCurrentInstance();
Map requestMap = context.getExternalContext().getRequestMap();
Throwable ex = (Throwable) requestMap.get("javax.servlet.error.exception");
StringWriter writer = new StringWriter();
PrintWriter pw = new PrintWriter(writer);
fillStackTrace(ex, pw);
LOG.fatal(writer.toString());
return writer.toString();
}
private void fillStackTrace(Throwable ex, PrintWriter pw) {
if (null == ex) {
return;
}
ex.printStackTrace(pw);
if (ex instanceof ServletException) {
Throwable cause = ((ServletException) ex).getRootCause();
if (null != cause) {
pw.println("Root Cause:");
fillStackTrace(cause, pw);
}
} else {
Throwable cause = ex.getCause();
if (null != cause) {
pw.println("Cause:");
fillStackTrace(cause, pw);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
并且在文件facesconfig中完成此bean定义时已经附加了
<managed-bean>
<description>
Bean que sirve para llenar la especificacion de un error. Stacktrace
</description>
<managed-bean-name>errorDisplay</managed-bean-name>
<managed-bean-class>mx.com.tdc.datos.page.bean.ErrorDisplay</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
Run Code Online (Sandbox Code Playgroud)
我希望这会有用
在 JSF 中向用户显示错误消息的一般方法是使用 FacesMessage:
在Java方面:
...
if (errorOccurred) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("An error occurred blabla..."));
}
Run Code Online (Sandbox Code Playgroud)
在 JSF(JSP 或 XHTML)页面中,只需使用<h:messages/>
页面中的组件即可显示错误。
归档时间: |
|
查看次数: |
37869 次 |
最近记录: |