📄 baseaction.java
字号:
package com.pegasus.framework.action;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.hibernate.StaleObjectStateException;
import org.jboss.util.NestedException;
import com.pegasus.framework.acl.pojo.UserSession;
import com.pegasus.framework.bo.SequenceManager;
import com.pegasus.framework.constant.Constant;
import com.pegasus.framework.constant.ErrorConstant;
import com.pegasus.framework.constant.HttpSessionConstant;
import com.pegasus.framework.exception.WebException;
import com.pegasus.framework.filter.TokenFilter;
import com.pegasus.framework.form.BasePagingForm;
import com.pegasus.framework.pojo.IBusinessObject;
import com.pegasus.framework.service.BeanNames;
import com.pegasus.framework.service.IBasicService;
import com.pegasus.framework.service.ServiceManager;
import com.pegasus.framework.util.ExceptionUtil;
import com.pegasus.framework.util.StringUtil;
public abstract class BaseAction extends Action {
private Logger logger = LogManager.getLogger(BaseAction.class);
private static final String GLOBAL_FORWARD_ERROR = "globalError";
private static final String ACTION_PARA_SIMPLE_ADD = "simple_add";
private static final String ACTION_PARA_SIMPLE_SAVE = "simple_save";
private static final String ACTION_PARA_SIMPLE_EDIT = "simple_edit";
private static final String ACTION_PARA_SIMPLE_UPDATE = "simple_update";
private static final String ACTION_PARA_SIMPLE_VIEW = "simple_view";
private static final String ACTION_PARA_SIMPLE_LIST = "simple_list";
private static final String ACTION_PARA_SIMPLE_DEL = "simple_del";
protected IBasicService basicService;
protected SequenceManager sequenceManager;
protected static final String MAPPING_FORWARD_SUCCESS = "success";
protected static final String MAPPING_FORWARD_FAIL = "fail";
/**
* This struts method is overwritten to delagte the call to executeAction.
*/
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//利用同步令牌处理重复提交
if (StringUtil.isNotEmpty(request.getParameter(TokenFilter.TOKEN_KEY))) {
if (!super.isTokenValid(request, true)) {
this.addSaveError(request, new ActionErrors(), ErrorConstant.ERROR_GLOBAL_MULTI_REQUEST);
return mapping.getInputForward();
}
}
try {
return executeAction(mapping, form, request, response);
} catch (WebException ex) {
handleWebException(request, ex);
return mapping.findForward(GLOBAL_FORWARD_ERROR);
//return mapping.getInputForward();
} catch (Exception ex) {
handleException(request, ex);
return mapping.findForward(GLOBAL_FORWARD_ERROR);
}
}
private void handleWebException(HttpServletRequest request, Exception exception) {
logger.info("exception.getMessage(): " + exception.getMessage());
this.addError(request, new ActionMessage(ErrorConstant.ERROR_WEB_EXCEPTION, exception.getMessage()));
logger.error(ExceptionUtil.format(exception));
}
private void handleException(HttpServletRequest request, Exception exception) {
ActionMessages messages = new ActionMessages();
if (exception instanceof org.hibernate.exception.JDBCConnectionException) {
messages.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(ErrorConstant.ERROR_DATABASE_CONNECT));
} else if (exception instanceof org.hibernate.exception.GenericJDBCException) {
messages.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(ErrorConstant.ERROR_DATABASE_CONNECT));
} else {
messages.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(ErrorConstant.ERROR_WEB_EXCEPTION, exception.getMessage()));
}
addErrors(request, messages);
logger.error(ExceptionUtil.format(exception));
}
/**
* Process the specified HTTP request, and create the corresponding HTTP response. Or forward to another web component that will create it, with provision for handling exceptions thrown by the
* business logic. <br/>Return an {@link org.apache.struts.action.ActionForward} instance describing where and how control should be forwarded, or null if the response has already been completed.
*
* @param mapping
* The ActionMapping used to select this instance.
* @param aForm
* The optional ActionForm bean for this request (if any).
* @param request
* The HTTP request we are processing.
* @param response
* The HTTP response we are creating.
* @return an {@link org.apache.struts.action.ActionForward} instance describing where and how control should be forwarded, or null if the response has already been completed.
* @throws NestedException
* if the application business logic throws an exception.
*/
public ActionForward executeAction(ActionMapping mapping, ActionForm aForm, HttpServletRequest request, HttpServletResponse response) throws WebException {
logger.info("executeAction");
initService();
String paramater = mapping.getParameter();
ActionForward result = null;
if (ACTION_PARA_SIMPLE_ADD.equals(paramater)) {
result = simpleAdd(mapping,aForm,request,response);
} else if (ACTION_PARA_SIMPLE_EDIT.equals(paramater)) {
result = simpleEdit(mapping,aForm,request,response);
} else if (ACTION_PARA_SIMPLE_SAVE.equals(paramater)) {
result = simpleSave(mapping,aForm,request,response);
} else if (ACTION_PARA_SIMPLE_UPDATE.equals(paramater)) {
result = simpleUpdate(mapping,aForm,request,response);
} else if (ACTION_PARA_SIMPLE_VIEW.equals(paramater)) {
result = simpleView(mapping,aForm,request,response);
} else if (ACTION_PARA_SIMPLE_LIST.equals(paramater)) {
result = simpleList(mapping,aForm,request,response);
} else if (ACTION_PARA_SIMPLE_DEL.equals(paramater)) {
result = simpleDelete(mapping,aForm,request,response);
} else {
result = doExecute(mapping,aForm,request,response);
}
return result;
}
public abstract ActionForward doExecute(ActionMapping mapping, ActionForm aForm, HttpServletRequest request, HttpServletResponse response) throws WebException;
public abstract void initService();
/**
* 得到用户的会话期信息。
*
* @param request HttpServletRequest
* @return UserSession
*/
protected UserSession getUserSession(HttpServletRequest request) {
return (UserSession) request.getSession().getAttribute(UserSession.USERSESSION_KEY);
}
protected ActionForward simpleAdd(ActionMapping mapping, ActionForm aForm, HttpServletRequest request, HttpServletResponse response) throws WebException {
logger.info("simple add start");
BasePagingForm form = (BasePagingForm)aForm;
IBusinessObject bo = (IBusinessObject)form.getBo();
logger.info(bo.toXMLString());
return getSuccessPath(mapping);
}
protected ActionForward simpleEdit(ActionMapping mapping, ActionForm aForm, HttpServletRequest request, HttpServletResponse response) throws WebException {
logger.info("simple edit start");
BasePagingForm form = (BasePagingForm)aForm;
Long boId = ((IBusinessObject)form.getBo()).getId();
logger.info("load bo id: " + boId);
IBusinessObject bo = basicService.load(boId);
form.setBo(bo);
return mapping.getInputForward();
}
protected void checkUniqueKey(IBusinessObject updatedBo,String column,String value) throws WebException{
IBusinessObject bo = (IBusinessObject)basicService.load(column,value);
if(updatedBo.getId() == null || updatedBo.getId().longValue() == 0) {
if(bo != null) {
throw new WebException("编码已经存在,请重新修改");
}
} else {
if(bo != null && !bo.getId().equals(updatedBo.getId())) {
throw new WebException("编码已经存在,请重新修改");
}
}
}
protected ActionForward simpleSave(ActionMapping mapping, ActionForm aForm, HttpServletRequest request, HttpServletResponse response) throws WebException {
logger.info("simple save start");
sequenceManager = (SequenceManager)ServiceManager.getService(BeanNames.BEAN_NAME_SEQUENCE);
BasePagingForm form = (BasePagingForm)aForm;
IBusinessObject bo = (IBusinessObject)form.getBo();
logger.info(bo.toXMLString());
if(bo.getId() == null || bo.getId().longValue() == 0) {
bo.setId(sequenceManager.getNextID(basicService.getTableName()));
if(StringUtil.isEmpty(bo.getDeleted())) {
bo.setDeleted(Constant.NO_ID);
}
basicService.saveObject(bo);
} else {
try {
if(StringUtil.isEmpty(bo.getDeleted())) {
bo.setDeleted(Constant.NO_ID);
}
basicService.updateObject(bo);
}catch(StaleObjectStateException e) {
throw new WebException("已经有人修改,请重新修改");
}
}
return getSuccessPath(mapping);
}
protected ActionForward simpleUpdate(ActionMapping mapping, ActionForm aForm, HttpServletRequest request, HttpServletResponse response) throws WebException {
return mapping.getInputForward();
}
protected ActionForward simpleView(ActionMapping mapping, ActionForm aForm, HttpServletRequest request, HttpServletResponse response) throws WebException {
logger.info("simple view start");
BasePagingForm form = (BasePagingForm)aForm;
Serializable boId = form.getBo().getId();
logger.info("load bo id: " + boId);
IBusinessObject bo = basicService.load(boId);
form.setBo(bo);
return mapping.getInputForward();
}
protected ActionForward simpleList(ActionMapping mapping, ActionForm aForm, HttpServletRequest request, HttpServletResponse response) throws WebException {
logger.info("simple query start");
BasePagingForm form = (BasePagingForm)aForm;
List list = basicService.query(new ArrayList(),null,form.getPageAgent());
form.setBoList(list);
return getSuccessPath(mapping);
}
protected ActionForward simpleDelete(ActionMapping mapping, ActionForm aForm, HttpServletRequest request, HttpServletResponse response) throws WebException {
logger.info("simple delete start");
BasePagingForm form = (BasePagingForm)aForm;
logger.info("delete bo id: " + form.getBo().toXMLString());
//basicService.delete(form.getBo());
IBusinessObject obj = basicService.load(form.getBo().getId());
obj.setDeleted(Constant.YES_ID);
basicService.updateObject(obj);
return getSuccessPath(mapping);
}
protected ActionForward getSuccessPath(ActionMapping mapping) {
return getPath(mapping,MAPPING_FORWARD_SUCCESS);
}
protected ActionForward getFailPath(ActionMapping mapping) {
return getPath(mapping,MAPPING_FORWARD_FAIL);
}
protected ActionForward getPath(ActionMapping mapping,String path) {
ActionForward result = mapping.findForward(path);
if(result == null) {
result = mapping.getInputForward();
}
return result;
}
/**
* Add and save a message with no replacement values.
*
* @param request
* @param messages
* @param msgKey
* Key for the message in the message resouce bundle.
*/
protected void addSaveMessage(HttpServletRequest request, ActionMessages messages, String msgKey) {
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(msgKey));
saveMessages(request, messages);
}
/**
* Add and save a message with the specified replacement values.
*
* @param request
* @param messages
* @param msgKey
* Key for the message in the message resouce bundle.
* @param value0
* First replacement value.
*/
protected void addSaveMessage(HttpServletRequest request, ActionMessages messages, String msgKey, Object value0) {
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(msgKey, value0));
saveMessages(request, messages);
}
/**
* Add and save a message with the specified replacement values.
*
* @param request
* @param messages
* @param msgKey
* Key for the message in the message resouce bundle.
* @param value0
* First replacement value.
* @param value1
* Second replacement value.
*/
protected void addSaveMessage(HttpServletRequest request, ActionMessages messages, String msgKey, Object value0, Object value1) {
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(msgKey, value0, value1));
saveMessages(request, messages);
}
/**
* Add and save a message with the specified replacement values.
*
* @param request
* @param messages
* @param msgKey
* Key for the message in the message resouce bundle.
* @param value0
* First replacement value.
* @param value1
* Second replacement value.
* @param value2
* Third replacement value.
*/
protected void addSaveMessage(HttpServletRequest request, ActionMessages messages, String msgKey, Object value0, Object value1, Object value2) {
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(msgKey, value0, value1, value2));
saveMessages(request, messages);
}
/**
* Add and save a message with the specified replacement values.
*
* @param request
* @param messages
* @param msgKey
* Key for the message in the message resouce bundle.
* @param value0
* First replacement value.
* @param value1
* Second replacement value.
* @param value2
* Third replacement value.
* @param value3
* Fourth replacement value.
*/
protected void addSaveMessage(HttpServletRequest request, ActionMessages messages, String msgKey, Object value0, Object value1, Object value2, Object value3) {
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(msgKey, value0, value1, value2, value3));
saveMessages(request, messages);
}
/**
* Add and save a message with the specified replacement values.
*
* @param request
* @param messages
* @param msgKey
* Key for the message in the message resouce bundle.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -