日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

JSF和Struts框架的錯誤控制與封裝處理_JSP教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:介紹JSP中request屬性的用法
一、request.getParameter() 和request.getAttribute() 區(qū)別 (1)request.getParameter()取得是通過容器的實現(xiàn)來取得通過類似post,get等方式傳入的數(shù)據(jù),request.setAttribute()和getAt

在struts中,通常采用的全局錯誤控制模式是構(gòu)建一個baseAction,在其execute方法中完成前臺傳回方法的dispatch操作,并由 try……catch……捕獲程序錯誤,實現(xiàn)錯誤的控制和展示。一個典型的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ā)生錯誤:", ex);
forwardPage = processBaseException(request, mapping, ex);
} catch (Throwable ex) {
log.error("發(fā)生錯誤:", 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頁面直接通過調(diào)用managed bean中的方法完成數(shù)據(jù)交互,不能像struts一樣通過捕獲dispatch操作過程拋出的異常來完成錯誤的處理(因為根本就沒有dispatch方法),似乎jsf根本就不支持全局的錯誤處理。

如果在managed bean中throw 一個exception(這里是AppException),觀察一下控制臺的日志,可以看到其實錯誤是從一個ActionListener的實現(xiàn)中拋出的(針對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) {
//這里需要根據(jù)框架的不同,判斷實例是否是程序中手動拋出的錯誤
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

resources.application

en


org.snailportal.webframework.listener.GlobalActionListener


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

分享:J2EE應(yīng)用服務(wù)器Jboss Tomcat安裝攻略
JBoss Tomcat已經(jīng)成為一個免費的開源的穩(wěn)定的J2EE服務(wù)器,雖然在JBoss中部署J2EE沒有商用J2EE服務(wù)器那么方便,基本都是通過手工編寫XML配置文件,但是這樣可以讓我們更容易理解J2EE的來龍去

來源:模板無憂//所屬分類:JSP教程/更新時間:2008-08-22
相關(guān)JSP教程