📄 bizaction.java
字号:
else {
throw new Exception(Log.PROCESS_RESULT_NULL);
}
} // end checkOutcome
// --------------------------------------------------------- Public Methods
/**
* Stores informational messages for display by the presention device.
*
* @param mapping The ActionMapping used to select this instance
* @param request The HTTP request we are processing
* @param bizResponse The BizResponse we are handling
*/
protected void checkMessages(
ActionMapping mapping,
HttpServletRequest request,
BizResponse bizResponse) {
// saveMessages(request,bizResponse.getMessages());
} // end checkMessages
/**
* Return the appropriate ActionForward for error or failure
* conditions.
* First checks for a FAILURE ActionForward stored in the request.
* If an override is not found, returns the result of the
* superclass method.
*
* @param mapping The ActionMapping used to select this instance
* @param form The optional ActionForm bean for this request
* @param request The HTTP request we are processing
* @param response The resonse we are creating
* @return The ActionForward representing FAILURE
* or null if a FAILURE forward has not been specified.
*/
protected ActionForward findFailure(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
// Did someone leave us a forward?
ActionForward forward = (ActionForward)
request.getAttribute(Tokens.FAILURE);
if (null==forward) {
// No override, use default
forward = super.findFailure(mapping,form,request,response);
}
else {
// Clear advice from the request
request.setAttribute(Tokens.FAILURE,null);
}
return forward;
} // end findFailure
/**
* Optional extension point for pre-processing.
* Default method does nothing.
* To branch to another URI, return an non-null ActionForward.
* If errors are logged (getErrors() et al),
* default behaviour will branch to findFailure().
*
* @param mapping The ActionMapping used to select this instance
* @param form The optional ActionForm bean for this request
* @param request The HTTP request we are processing
* @param response The resonse we are creating
*/
protected void preProcess(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
// Check for cancelled
ActionForward forward = mapping.findForward(Tokens.CANCEL);
if ((null!=forward) && (isCancelled(request))) {
// Our findFailure looks for this
request.setAttribute(Tokens.FAILURE,forward);
// Post cancel error message
ActionErrors errors = getErrors(request,true);
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError(Tokens.ERROR_CANCEL));
return;
}
// Check for missing token
forward = mapping.findForward(Tokens.GET_TOKEN);
if ((null!=forward) && (!isTokenValid(request))) {
// Our findFailure looks for this
request.setAttribute(Tokens.FAILURE,forward);
// Post token error message
ActionErrors errors = getErrors(request,true);
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError(Tokens.ERROR_TOKEN));
return;
}
if (null!=forward) {
// reset to guard against duplicate request
resetToken(request);
}
// Check for save token directive (do this last)
forward = mapping.findForward(Tokens.SET_TOKEN);
if (null!=forward) saveToken(request);
} // end preProcess
/**
* Return the appropriate ActionForward for the nominal,
* non-error state.
* First checks for a SUCCESS ActionForward stored in the request.
* If an override is not found, returns the result of the
* superclass method.
*
* @param mapping The ActionMapping used to select this instance
* @param form The optional ActionForm bean for this request (if any)
* @param request The HTTP request we are processing
* @param response The response we are creating
* @return The ActionForward representing SUCCESS
* or null if a SUCCESS forward has not been specified.
*/
protected ActionForward findSuccess(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
// Did someone leave us a forward?
ActionForward forward = (ActionForward)
request.getAttribute(Tokens.SUCCESS);
if (null==forward) {
// No override, use default
forward = super.findSuccess(mapping,form,request,response);
}
else {
// Clear advice from the request
request.setAttribute(Tokens.SUCCESS,null);
}
return forward;
} // end findSuccess
/**
* The set of argument type classes for the reflected method call.
* These are the same for all calls, so calculate them only once.
*/
private static final Class types[] = { Object.class };
/**
* Error handler.
* Posts a message template and two parameters in a BizResponse.
*/
private BizResponse processError(
ActionMapping mapping,
String template) {
// Any other way to do this?
BizResponse bizResponse =
new org.apache.commons.scaffold.util.BizResponseImpl();
Message message =
new org.apache.commons.scaffold.util.MessageImpl(
template,
mapping.getPath(),
mapping.getParameter()
);
bizResponse.addMessage(message);
return bizResponse;
}
/**
* Invoke an alternate method on BizService.
*/
protected BizResponse processDispatch(
ActionMapping mapping,
BizService bizService,
BizRequest bizRequest,
String methodName) throws Exception {
Method method = bizService.getClass().getMethod(methodName,types);
Object args[] = { bizRequest };
BizResponse bizResponse = null;
try {
bizResponse = (BizResponse) method.invoke(bizService,args);
}
catch (ClassCastException e) {
bizResponse = processError(mapping,Tokens.ERROR_DISPATCH_RETURN);
}
catch (IllegalAccessException e) {
bizResponse = processError(mapping,Tokens.ERROR_DISPATCH_RETURN);
}
catch (InvocationTargetException e) {
// Rethrow the target exception if possible so that the
// exception handling machinery can deal with it
Throwable t = e.getTargetException();
if (t instanceof Exception) {
throw ((Exception) t);
} else {
bizResponse = processError(mapping,Tokens.ERROR_DISPATCH);
}
}
return bizResponse;
}
/**
* Obtain the business request, invoke the business service,
* and process the outcome.
*
* @param mapping The ActionMapping used to select this instance
* @param form The ActionForm
* @param request The HTTP request we are processing
* @param response The HTTP response we are creating
* @param helpers The object instantiated from type given as parameter.
* @exception Exception if a error occurs
*/
protected void executeLogic(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response,
Object[] helpers) throws Exception {
BizResponse bizResponse = null;
servlet.log(Log.HELPER_PROCESSING,Log.DEBUG);
try {
// Cast our form bean; snag our request bean
BizForm bizForm = (BizForm) form;
BizRequest bizRequest = bizForm.getBizRequest();
// Munge the parameter property
servlet.log(Log.TOKENS_PARSING,Log.DEBUG);
String[] tokens = tokenize(mapping.getParameter());
// Create our business service helper
Object helper = createHelperObject(request,tokens[0]);
BizService bizService = (BizService) helper;
// Process business logic
servlet.log(Log.HELPER_EXECUTING,Log.DEBUG);
if (tokens.length>1) {
// Pass along the helper's parameter, if any
if (tokens.length>2) {
bizRequest.setParameter(tokens[2]);
}
bizResponse = processDispatch(
mapping,
bizService,
bizRequest,
tokens[1]);
}
else {
bizResponse = (BizResponse) bizService.process(bizRequest);
}
}
// Gracefully trap any kinky class-cast or NPE type exceptions
catch (Throwable t) {
throw new ParameterException(t);
}
// Analyze result of business logic
checkOutcome(mapping,request,bizResponse);
} // end executeLogic
} // end BizAction
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -