国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JSP > 正文

JSF和Struts框架的錯(cuò)誤控制與封裝處理

2024-09-05 00:16:54
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

在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)聽(tīng)器配置,faces-config-application.xml:

代碼



org.springframework.web.jsf.DelegatingVariableResolver

resources.application

en


org.snailportal.webframework.listener.GlobalActionListener


這樣,開(kāi)發(fā)人員只需要在action和managed bean里面根據(jù)業(yè)務(wù)的需要拋出指定基礎(chǔ)類(lèi)型的Exception實(shí)例,由BaseAction和ActionListener完成錯(cuò)誤的封裝處理,再傳遞給前臺(tái)進(jìn)行顯示,從而減少開(kāi)發(fā)的代碼量,提高框架的可維護(hù)性。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 淄博市| 镇坪县| 开原市| 江永县| 基隆市| 安化县| 元氏县| 新疆| 肇庆市| 家居| 元朗区| 丰县| 凤冈县| 庄浪县| 镇宁| 祁门县| 社旗县| 嘉定区| 桃园市| 龙南县| 邢台县| 建阳市| 池州市| 育儿| 防城港市| 仙游县| 辛集市| 陕西省| 山西省| 黄大仙区| 舒兰市| 富顺县| 泰安市| 淳化县| 贵阳市| 禹州市| 巨鹿县| 健康| 巴里| 青神县| 徐闻县|