在struts中
代碼
public ActionForward execute(ActionMapping mapping
……
ActionForward forwardPage = null
try {
String parameter = mapping
if (parameter == null) {
String message = messages
response
return null
}
String name = processReqCode(request
forwardPage = dispatchMethod(mapping
} catch (BaseException ex) {
if (log
log
forwardPage = processBaseException(request
} catch (Throwable ex) {
log
ActionMessages errors = new ActionMessages()
ByteArrayOutputStream ostr = new ByteArrayOutputStream()
ex
errors
saveErrors(request
forwardPage = mapping
output
output
}
……
}
由於JSF采用了managed bean
如果在managed bean中throw 一個exception(這裡是AppException)
代碼
public class GlobalActionListener extends ActionListenerImpl {
public void processAction(ActionEvent event) throws AbortProcessingException {
FacesContext facesContext = FacesContext
Application application = facesContext
ActionSource actionSource = (ActionSource) event
MethodBinding methodBinding = actionSource
String fromAction = null
if (methodBinding != null) {
fromAction = methodBinding
try {
outcome = (String) methodBinding
} catch (EvaluationException e) {
Throwable cause = e
if (cause != null && cause instanceof AppException) {
//這裡需要根據框架的不同
FacesUtils
else {
throw (AbortProcessingException) cause
}
} catch (RuntimeException e) {
throw new FacesException(
}
NavigationHandler navigationHandler = application
navigationHandler
// Render Response if needed
facesContext
}
}
監聽器配置
代碼
<application>
<variable
<message
<locale
<default
</locale
<action
</application>
這樣
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28909.html