大家帮忙看看代码,用拦截器做操作日志的存储,就是不管用,根本进不了拦截器。在线等大家帮帮忙吧
解决方案 »
- 求教一个servlet结果返回问题
- struts2 spring hibernate在ubuntu下出错!
- 在 SSH 结构中,如何在创建父对象的同时创建子对象?
- 怎么样得到CGLIB生成的代理类
- Netbean4.1 Run project报Deployment error: null. 如下, 咋整?
- java的问题,请求高手指点.....
- 老问题,Hibernate连接池,在网上没找到对口的解决方案,在此求救
- org.jboss.resource.JBossResourceException: Could not create connection; - nested
- 关于j2ee中显示中文的问题。
- 急!!请问怎么绑定数据源?
- 如何让特定日期在日历中显示?
- 求助!有谁知道HighCharts的
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.codebehind.pathPrefix" value="/app/" />
<constant name="struts.custom.i18n.resources" value="i18n/messages" />
<constant name= "struts.multipart.maxSize" value="15242880" />
<constant name="struts.action.extension" value="action,,"></constant>
<package name="default-with-exceptions" extends="struts-default" abstract="true">
<global-results>
<result name="exception-error-page">/app/errors/error.jsp</result>
<result name="input">/app/errors/fieldError.jsp</result>
<result name="invalid.token">/app/errors/repeatSubmit.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="exception-error-page" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
</package>
<package name="default" extends="default-with-exceptions">
<interceptors>
<interceptor name="actionlog" class="com.tinet.bpsc.wm.commons.ActionLogInterceptor" />
<interceptor-stack name="bpscStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="tokenSession">
<param name="includeMethods">save*,add*</param>
</interceptor-ref>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="profiling"/>
<interceptor-ref name="actionlog"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="store">
<param name="operationMode">AUTOMATIC</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="bpscStack" /> </package>
</struts>
import java.util.Date;
import java.util.Map;import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.util.ValueStack;
import com.tinet.bpsc.wm.commons.util.ContextUtil;
import com.tinet.bpsc.wm.commons.web.ComnAction;
import com.tinet.bpsc.wm.entity.Entity;
import com.tinet.bpsc.wm.entity.LogAction;
import com.tinet.bpsc.wm.inc.Const;
import com.tinet.bpsc.wm.service.LogActionService;
/**
* 操作日志拦截器。
*<p>
* 文件名: ActionLogInterceptor.java
@SuppressWarnings("serial")
public class ActionLogInterceptor extends AbstractInterceptor { private static Logger logger = LoggerFactory.getLogger(ActionLogInterceptor.class);
private LogActionService logActionService;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
String result = invocation.invoke();
Method method = invocation.getAction().getClass().getMethod(invocation.getProxy().getMethod());
final ActionLogAnnotation annotation = method.getAnnotation(ActionLogAnnotation.class);
final ActionInvocation ai = invocation;
if (annotation != null) {
new Thread(
new Runnable(){
@Override
public void run() {
recordLogInfo(annotation, ai);
}
}
).start();
}
return result;
}
private void recordLogInfo(ActionLogAnnotation annotation, ActionInvocation invocation) {
String object = annotation.object();
String comment = annotation.comment();
String params = annotation.params();
int result = Const.LOG_ACTION_RESULT_SUCCESS; // 默认执行成功
if (((ComnAction)invocation.getAction()).getActionErrors().size() > 0) {
result = Const.LOG_ACTION_RESULT_FAILURE; // action操作执行失败
}
logger.debug("annotion:object=[{}],comment=[{}],params=[{}]", new String[]{object, comment, params});
LogAction logAction = new LogAction();
try {
String[] arrParams = null;
if (params.length() > 0) {
arrParams = params.split(",");
} else {
arrParams = new String[]{};
}
for (int i = 0; i < arrParams.length; i++) {
comment = StringUtils.replaceOnce(comment, "?", getParamValue(invocation, arrParams[i]));
}
object = this.getParamValue(invocation, object);
Entity logined = (Entity)invocation.getInvocationContext().getSession().get(Constants.LOGINED_KEY);
logAction.setEntityId(logined.getId());
logAction.setOperatedObject(object);
logAction.setOperateComment(comment);
logAction.setEntityName(logined.getName());
logAction.setOperateType(invocation.getAction().getClass().getName()+ "." + invocation.getProxy().getMethod());
logAction.setCreateTime(new Date());
logAction.setResult(result);
logActionService.saveActionLog(logAction);
} catch (Exception e) {
logger.error("something error when record actionlog: {}", logAction.toString());
}
}
/**
* 解析form表单数据
* @param invocation
* @param param 操作日志要记录的执行操作时提交的参数。
* @return 返回操作对象
*/
private String getParamValue(ActionInvocation invocation, String param) {
if (param.indexOf(".") != -1) {
String scope = StringUtils.substringBefore(param, ".");
String remain = StringUtils.substringAfter(param, ".");
String theone = StringUtils.substringBefore(remain, ".");
remain = StringUtils.substringAfter(remain, ".");
ValueStack vs = invocation.getInvocationContext().getValueStack();
if ("session".equals(scope)) {
vs.push(ServletActionContext.getRequest().getSession().getAttribute(theone));
} else if ("request".equals("scope")) {
vs.push(ServletActionContext.getRequest().getAttribute(theone));
} else {
return null;
}
if (StringUtils.isEmpty(remain)) {
return vs.pop().toString();
}
String rslt = vs.findString(remain);
vs.pop();
return rslt;
} else {
Map<String, Object> mapParams = invocation.getInvocationContext().getParameters();
String[] pvobject = (String[])mapParams.get(param);
return pvobject != null && pvobject.length > 0
? (pvobject.length > 1 ? ArrayUtils.toString(pvobject) : pvobject[0])
: "";
}
}
@Override
public void init() {
super.init();
logActionService = (LogActionService)ContextUtil.getContext().getBean("logActionService");
}
}
@Retention(RetentionPolicy.RUNTIME)
public @interface ActionLogAnnotation {
String object();
String comment();
String params() default "";
}
@ActionLogAnnotation(object="trunk", comment="新增接入码,trunk=?,comment=?", params="trunk,comment")
public String save() {}
http://download.csdn.net/detail/zuxianghuang/4138590
http://callan.iteye.com/blog/182520
这句话并没有覆盖struts-default.xml下的<default-interceptor-ref name="defaultStack"/>
谁能给个答案,万分感激