📄 baseaction.java
字号:
package com.set.appframe.web;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.commons.fileupload.FileUpload;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.set.appframe.business.SpringBeanFactory;
import com.set.appframe.data.GenericValueObject;
import com.set.appframe.exception.SystemException;
import com.set.utils.AppSetting;
import com.set.utils.DateTimeUtils;
import com.set.utils.StringUtils;
/**
* BaseAction is the Struts Action class that all application Action classes
* should extend. It also provides routines to perform application level
* operations, such as:<br>
* (1) executes filters process all requests and responses in an Web
* application;<br>
* (2) handles errors in application with a standard error page;<br>
* (3) formats output values to the display
*
* @author Oliver Yip
* @version 1.0, 11/04/03
* @since 1.0
*/
public abstract class BaseAction extends DispatchAction {
// private static ApplicationContext ctx = null;
/**
* application setting for application.
*
*/
protected AppSetting as = null;
/**
* constructs a <code>BaseAction</code> object.
*
* @param as
* Application setting for the application
*
*/
public BaseAction(AppSetting as) {
this.as = as;
}
/**
* get a bean specificed in a xml file from spring bean factory
*
* @param name
* String
* @return Object
* @author zhifeng history : modified by zhifeng, change the implementation
* of getBean() 2004-11-11
*/
public Object getBean(String name) throws SystemException {
return SpringBeanFactory.getInstance().getBean(name);
}
/**
* process an HTTP request from Web user.
* <p>
* The method also handles validation errors from Action Form, and runs the
* pre-process and post-process filters.
*
* @param mapping
* Struts Action mapping
* @param form
* Struts Action form
* @param request
* Web HTTP request
* @param response
* Web HTTP response
* @return Struts Action forward indicating JSP destination to reach after
* the current HTTP request.
* @throws Exception
* exceptions happened in handling request
*
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) throws Exception {
ActionForward af = null;
// Applying pre-process filters
preProcessFilters(mapping, form, request, response);
// Checking multipart
boolean isMultipart = FileUpload.isMultipartContent(request);
if (isMultipart) {
af = uploadFile(mapping, form, request, response);
} else {
af = super.execute(mapping, form, request, response);
}
// Applying post-process filters
postProcessFilters(mapping, form, request, response);
return af;
}
protected ActionForward uploadFile(ActionMapping mapping, ActionForm form,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) throws Exception {
return null;
}
/**
* executes pre-process filters before a Web request is processed
*
* @param mapping
* Struts Action mapping
* @param form
* Struts Action form
* @param request
* Web HTTP request
* @param response
* Web HTTP response
*
*/
protected abstract void preProcessFilters(ActionMapping mapping,
ActionForm form, javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response);
/**
* executes post-process filters after a Web request is processed
*
* @param mapping
* Struts Action mapping
* @param form
* Struts Action form
* @param request
* Web HTTP request
* @param response
* Web HTTP response
*
*/
protected abstract void postProcessFilters(ActionMapping mapping,
ActionForm form, javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response);
/**
* handles errors that happen in the application
*
* @param mapping
* Struts Action mapping
* @param request
* Web HTTP request
* @param errKey
* resource key for the error
* @param param
* parameters to replace variables in resource string for the
* error
* @param user
* current user that experiences the error
* @param backURL
* the URL link to return to after the error page has been
* displayed
* @param additionalInfo
* additional information for the error
* @param excp
* exception that is associated with the error
* @return Struts Action forward indicating JSP destination to reach after
* the current HTTP request.
*
*/
protected abstract ActionForward handleError(ActionMapping mapping,
javax.servlet.http.HttpServletRequest request, String errKey, String user, String backURL,Exception excp);
/**
* handles validation errors that happen in the application
*
* @param mapping
* Struts Action mapping
* @param request
* Web HTTP request
* @param err
* validation errors that come from Action Form
* @return Struts Action forward indicating JSP destination to reach after
* the current HTTP request.
*
*/
protected ActionForward handleError(ActionMapping mapping,
javax.servlet.http.HttpServletRequest request) {
return handleError(mapping, request,"","","",null);
}
/**
* formats date value to display string according to application setting
*
* @param d
* date to format
* @return formatted display string for date
*
*/
protected String formatDate(Date d) {
return (d == null ? "" : as.dFormat(d));
}
/**
* formats date values in a GenericValueObject to display strings according
* to application setting.
*
* @param gvo
* the GenericValueObject to format
* @param fields
* array of key strings for the name-value pairs in the
* GenericValueObject to format
* @return the resulting GenericValueObject that has date values formatted
*
*/
protected GenericValueObject formatDate(GenericValueObject gvo,
String[] fields) {
if (fields != null && gvo != null) {
for (int i = 0; i < fields.length; i++) {
Object o = gvo.getItem(fields[i]);
if (o instanceof Date) {
gvo.add(fields[i], formatDate((Date) o));
}
}
}
return gvo;
}
/**
* format long date field to date format
*
* @param gvo
* GenericValueObject
* @param fields
* String[]
* @return GenericValueObject
*/
protected GenericValueObject formatDate2(GenericValueObject gvo,
String[] fields) {
if (fields != null && gvo != null) {
for (int i = 0; i < fields.length; i++) {
String o = gvo.getItemString(fields[i]);
if (!"0".equals(o) && !"".equals(o)) {
gvo.add(fields[i], DateTimeUtils.long2DateString(o));
}
}
}
return gvo;
}
/**
* @author hj
* @CREATEDDATE: 2005-06-17
* @param vgvo
* List
* @param fields
* String[]
* @return List
* @DISCRIPTION: 返回带几点几分的日期
*/
protected List formatDate3(List vgvo, String[] fields) {
if (vgvo == null || fields == null) {
return null;
}
List vRet = new ArrayList();
Iterator eGVO = vgvo.iterator();
while (eGVO.hasNext()) {
GenericValueObject gvo = (GenericValueObject) eGVO.next();
vRet.add(formatDate3(gvo, fields));
}
return vRet;
}
protected GenericValueObject formatDate3(GenericValueObject gvo,
String[] fields) {
if (fields != null && gvo != null) {
for (int i = 0; i < fields.length; i++) {
String o = gvo.getItemString(fields[i]);
if (!"0".equals(o) && !"".equals(o)) {
Date date = new Date(Long.parseLong(o));
gvo.add(fields[i], DateTimeUtils.dateToLangString(date));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -