?? baseaction.java
字號:
package edu.buptsse.sxjd.actions;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.util.Locale;
import java.util.List;
import java.util.Iterator;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import edu.buptsse.sxjd.common.Constants;
import edu.buptsse.sxjd.dao.DBManager;
import edu.buptsse.sxjd.service.BusinessException;
public abstract class BaseAction extends Action {
protected DataSource dataSource = null;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ServletContext context = servlet.getServletContext();
//取得數(shù)據(jù)源
dataSource = (DataSource)context.getAttribute(Constants.DATASOURCE_JTDS_KEY);
DBManager.setDs(dataSource);
ActionForward forwardPage = null;
try {
forwardPage = executeAction(mapping, form, request, response);
} catch (BusinessException ex) {
forwardPage = processExceptions(request, mapping, ex);
} catch (Throwable ex) {
ex.printStackTrace();
forwardPage = mapping.findForward(Constants.SYSTEM_ERROR_PAGE);
}
return forwardPage;
}
abstract public ActionForward executeAction(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws BusinessException;
protected ActionForward processExceptions(HttpServletRequest request,
ActionMapping mapping, BusinessException ex) {
ActionMessages errors = new ActionMessages();
ActionForward forward = null;
Locale locale = getLocale(request);
processBaseException(errors, ex, locale);
// Either return to the input resource or a configured failure forward
String inputStr = mapping.getInput();
ActionForward failureForward = mapping
.findForward(Constants.APP_ERROR_PAGE);
if (inputStr != null) {
forward = new ActionForward(inputStr);
} else if (failureForward != null) {
forward = failureForward;
}
// See if this exception contains a list of subexceptions
List exceptions = ex.getExceptions();
if (exceptions != null && !exceptions.isEmpty()) {
// int size = exceptions.size();
Iterator iter = exceptions.iterator();
while (iter.hasNext()) {
// All subexceptions must be BaseExceptions
BusinessException subException = (BusinessException) iter.next();
processBaseException(errors, subException, locale);
}
}
// Tell the Struts framework to save the errors into the request
saveErrors(request, errors);
// Return the ActionForward
return forward;
}
protected void processBaseException(ActionMessages errors,
BusinessException ex, Locale locale) {
// Holds the reference to the ActionError to be added
ActionMessage newActionMessage = null;
// The errorCode is the key to the resource bundle
String errorCode = ex.getMessageKey();
/**
* If there are extra arguments to be used by the MessageFormat object,
* insert them into the argList. The arguments are context sensitive
* arguments for the exception; there may be 0 or more.
*/
Object[] args = ex.getMessageArgs();
/**
* In an application that had to support I18N, you might want to format
* each value in the argument array based on its type and the user
* locale. For example, if there is a Date object in the array, it would
* need to be formatted for each locale.
*/
// Now construct an instance of the ActionError class
if (args != null && args.length > 0) {
// Use the arguments that were provided in the exception
newActionMessage = new ActionMessage(errorCode, args);
} else {
newActionMessage = new ActionMessage(errorCode);
}
errors.add(ActionMessages.GLOBAL_MESSAGE, newActionMessage);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -