JSF和Struts框架的錯(cuò)誤控制與封裝處理_JSP教程
推薦:介紹JSP中request屬性的用法一、request.getParameter() 和request.getAttribute() 區(qū)別 (1)request.getParameter()取得是通過(guò)容器的實(shí)現(xiàn)來(lái)取得通過(guò)類似post,get等方式傳入的數(shù)據(jù),request.setAttribute()和getAt
在struts中,通常采用的全局錯(cuò)誤控制模式是構(gòu)建一個(gè)baseAction,在其execute方法中完成前臺(tái)傳回方法的dispatch操作,并由 try……catch……捕獲程序錯(cuò)誤,實(shí)現(xiàn)錯(cuò)誤的控制和展示。一個(gè)典型的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("發(fā)生錯(cuò)誤:", ex);
forwardPage = processBaseException(request, mapping, ex);
} catch (Throwable ex) {
log.error("發(fā)生錯(cuò)誤:", 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頁(yè)面直接通過(guò)調(diào)用managed bean中的方法完成數(shù)據(jù)交互,不能像struts一樣通過(guò)捕獲dispatch操作過(guò)程拋出的異常來(lái)完成錯(cuò)誤的處理(因?yàn)楦揪蜎](méi)有dispatch方法),似乎jsf根本就不支持全局的錯(cuò)誤處理。
如果在managed bean中throw 一個(gè)exception(這里是AppException),觀察一下控制臺(tái)的日志,可以看到其實(shí)錯(cuò)誤是從一個(gè)ActionListener的實(shí)現(xiàn)中拋出的(針對(duì)myfaces,這里是ActionListenerImpl),參考jsf的生命周期過(guò)程,方法出來(lái)了:
代碼
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) {
//這里需要根據(jù)框架的不同,判斷實(shí)例是否是程序中手動(dòng)拋出的錯(cuò)誤
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;
}
}
監(jiān)聽器配置,faces-config-application.xml:
代碼
org.springframework.web.jsf.DelegatingVariableResolver
org.snailportal.webframework.listener.GlobalActionListener
這樣,開發(fā)人員只需要在action和managed bean里面根據(jù)業(yè)務(wù)的需要拋出指定基礎(chǔ)類型的Exception實(shí)例,由BaseAction和ActionListener完成錯(cuò)誤的封裝處理,再傳遞給前臺(tái)進(jìn)行顯示,從而減少開發(fā)的代碼量,提高框架的可維護(hù)性。
分享:J2EE應(yīng)用服務(wù)器Jboss Tomcat安裝攻略JBoss Tomcat已經(jīng)成為一個(gè)免費(fèi)的開源的穩(wěn)定的J2EE服務(wù)器,雖然在JBoss中部署J2EE沒(méi)有商用J2EE服務(wù)器那么方便,基本都是通過(guò)手工編寫XML配置文件,但是這樣可以讓我們更容易理解J2EE的來(lái)龍去
- jsp response.sendRedirect不跳轉(zhuǎn)的原因分析及解決
- JSP指令元素(page指令/include指令/taglib指令)復(fù)習(xí)整理
- JSP腳本元素和注釋復(fù)習(xí)總結(jié)示例
- JSP FusionCharts Free顯示圖表 具體實(shí)現(xiàn)
- 網(wǎng)頁(yè)模板:關(guān)于jsp頁(yè)面使用jstl的異常分析
- JSP頁(yè)面中文傳遞參數(shù)使用escape編碼
- 基于jsp:included的使用與jsp:param亂碼的解決方法
- Java Web項(xiàng)目中連接Access數(shù)據(jù)庫(kù)的配置方法
- JDBC連接Access數(shù)據(jù)庫(kù)的幾種方式介紹
- 網(wǎng)站圖片路徑的問(wèn)題:絕對(duì)路徑/虛擬路徑
- (jsp/html)網(wǎng)頁(yè)上嵌入播放器(常用播放器代碼整理)
- jsp下顯示中文文件名及絕對(duì)路徑下的圖片解決方法
JSP教程Rss訂閱編程教程搜索
JSP教程推薦
猜你也喜歡看這些
- Windows 2000 server下搭建JSP網(wǎng)站環(huán)境
- J2EERI Pointbase數(shù)據(jù)庫(kù)遠(yuǎn)程命令執(zhí)行漏洞
- 解析JSP中標(biāo)簽庫(kù)
- jsp中freemarker 數(shù)字格式化
- jsp中實(shí)現(xiàn)上傳圖片即時(shí)顯示效果功能
- JSP分頁(yè)顯示的實(shí)例代碼
- Struts初級(jí)教程(3)-結(jié)合數(shù)據(jù)庫(kù)應(yīng)用
- 使用靜態(tài)類實(shí)現(xiàn)JSP自定義標(biāo)簽
- Spring學(xué)習(xí)基礎(chǔ)---多框架集成
- JavaBean 在jsp和serlvle中傳遞技術(shù)要點(diǎn)
- 相關(guān)鏈接:
- 教程說(shuō)明:
JSP教程-JSF和Struts框架的錯(cuò)誤控制與封裝處理
。