JSF和Struts框架的錯誤控制與封裝處理_JSP教程
推薦:介紹JSP中request屬性的用法一、request.getParameter() 和request.getAttribute() 區別 (1)request.getParameter()取得是通過容器的實現來取得通過類似post,get等方式傳入的數據,request.setAttribute()和getAt
在struts中,通常采用的全局錯誤控制模式是構建一個baseAction,在其execute方法中完成前臺傳回方法的dispatch操作,并由 try……catch……捕獲程序錯誤,實現錯誤的控制和展示。一個典型的BaseAction例子如下:
代碼
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
……
ActionForward forwardPage = null;
try {
String parameter = mapping.getParameter;
if (parameter == null) {
String message = messages.getMessage("dispatch.handler",mapping.getPath);
response.sendError(500, message);
return null;
}
String name = processReqCode(request.getParameter(parameter));
forwardPage = dispatchMethod(mapping, form, request, response, name);
} catch (BaseException ex) {
if (log.isDebugEnabled)
log.debug("發生錯誤:", ex);
forwardPage = processBaseException(request, mapping, ex);
} catch (Throwable ex) {
log.error("發生錯誤:", ex);
ActionMessages errors = new ActionMessages;
ByteArrayOutputStream ostr = new ByteArrayOutputStream;
ex.printStackTrace(new PrintStream(ostr));
errors.add("org.apache.struts.action.GLOBAL_MESSAGE", new ActionMessage
(ostr.toString));
saveErrors(request, errors);
forwardPage = mapping.findForward("syserror");
output.setStatus("fail");
output.setError(ex.getMessage);
}
……
}
由于JSF采用了managed bean,JSP頁面直接通過調用managed bean中的方法完成數據交互,不能像struts一樣通過捕獲dispatch操作過程拋出的異常來完成錯誤的處理(因為根本就沒有dispatch方法),似乎jsf根本就不支持全局的錯誤處理。
如果在managed bean中throw 一個exception(這里是AppException),觀察一下控制臺的日志,可以看到其實錯誤是從一個ActionListener的實現中拋出的(針對myfaces,這里是ActionListenerImpl),參考jsf的生命周期過程,方法出來了:
代碼
public class GlobalActionListener extends ActionListenerImpl {
public void processAction(ActionEvent event) throws AbortProcessingException {
FacesContext facesContext = FacesContext.getCurrentInstance;
Application application = facesContext.getApplication;
ActionSource actionSource = (ActionSource) event.getComponent;
MethodBinding methodBinding = actionSource.getAction;
String fromAction = null;String outcome = null;
if (methodBinding != null) {
fromAction = methodBinding.getExpressionString;
try {
outcome = (String) methodBinding.invoke(facesContext, null);
} catch (EvaluationException e) {
Throwable cause = e.getCause;
if (cause != null && cause instanceof AppException) {
//這里需要根據框架的不同,判斷實例是否是程序中手動拋出的錯誤
FacesUtils.addErrorMessage(event.getComponent.getClientId(facesContext),
cause.getMessage);}
else {
throw (AbortProcessingException) cause;
}
} catch (RuntimeException e) {
throw new FacesException("Error calling action method of component with id "
event.getComponent.getClientId(facesContext), e);
}
NavigationHandler navigationHandler = application.getNavigationHandler;
navigationHandler.handleNavigation(facesContext, fromAction, outcome);
// Render Response if needed
facesContext.renderResponse;
}
}
監聽器配置,faces-config-application.xml:
代碼
org.springframework.web.jsf.DelegatingVariableResolver
org.snailportal.webframework.listener.GlobalActionListener
這樣,開發人員只需要在action和managed bean里面根據業務的需要拋出指定基礎類型的Exception實例,由BaseAction和ActionListener完成錯誤的封裝處理,再傳遞給前臺進行顯示,從而減少開發的代碼量,提高框架的可維護性。
分享:J2EE應用服務器Jboss Tomcat安裝攻略JBoss Tomcat已經成為一個免費的開源的穩定的J2EE服務器,雖然在JBoss中部署J2EE沒有商用J2EE服務器那么方便,基本都是通過手工編寫XML配置文件,但是這樣可以讓我們更容易理解J2EE的來龍去
- jsp response.sendRedirect不跳轉的原因分析及解決
- JSP指令元素(page指令/include指令/taglib指令)復習整理
- JSP腳本元素和注釋復習總結示例
- JSP FusionCharts Free顯示圖表 具體實現
- 網頁模板:關于jsp頁面使用jstl的異常分析
- JSP頁面中文傳遞參數使用escape編碼
- 基于jsp:included的使用與jsp:param亂碼的解決方法
- Java Web項目中連接Access數據庫的配置方法
- JDBC連接Access數據庫的幾種方式介紹
- 網站圖片路徑的問題:絕對路徑/虛擬路徑
- (jsp/html)網頁上嵌入播放器(常用播放器代碼整理)
- jsp下顯示中文文件名及絕對路徑下的圖片解決方法
- 相關鏈接:
- 教程說明:
JSP教程-JSF和Struts框架的錯誤控制與封裝處理
。