首页 > 代码库 > MyEclippse中使用struts-default.xml中定义的拦截器(timmer,logger)
MyEclippse中使用struts-default.xml中定义的拦截器(timmer,logger)
环境:MyEclipse 2015 Stable 2.0;struts2-core-2.3.16.1.jar等
struts.xml
<struts> <package name="p1" namespace="/" extends="struts-default"> <action name="m1" class="org.ah.s2.C1" method="m1"> <interceptor-ref name="timer" /> <interceptor-ref name="logger" /> <result name="success" type="dispatcher"> /success.jsp </result> </action> </package> </struts>
有人说必须加<interceptor-ref name="defaultStack"/>,其实是不求甚解造成的。
但是,MyEclipse下,log不输出,需要配置log4j.properties,放在src下
#
# Log4J Settings for log4j 1.2.x (via jakarta-commons-logging)
#
# The five logging levels used by Log are (in order):
#
# 1. DEBUG (the least serious)
# 2. INFO
# 3. WARN
# 4. ERROR
# 5. FATAL (the most serious)
# Set root logger level to WARN and append to stdout
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller‘s file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%d %5p (%c:%L) - %m%n
# Print only messages of level ERROR or above in the package noModule.
log4j.logger.noModule=FATAL
# OpenSymphony Stuff
log4j.logger.com.opensymphony=INFO
log4j.logger.org.apache.struts2=INFO
# Spring Stuff
log4j.logger.org.springframework=INFO
timmer和logger的日志级别都是INFO,把级别设为INFO即可。
其他辅助文件:
index.jsp <s:form action="m1" method="post"> <s:submit value="http://www.mamicode.com/测试Struts控件传值"></s:submit> </s:form> Action: package org.ah.s2; import com.opensymphony.xwork2.ActionSupport; public class C1 extends ActionSupport { public String m1() { return "success"; } }
测试一下:
2016-12-31 21:51:00,312 INFO (com.opensymphony.xwork2.interceptor.LoggingInterceptor:42) - Starting execution stack for action //m1
2016-12-31 21:51:00,354 INFO (com.opensymphony.xwork2.interceptor.LoggingInterceptor:42) - Finishing execution stack for action //m1
2016-12-31 21:51:00,354 INFO (com.opensymphony.xwork2.interceptor.TimerInterceptor:42) - Executed action [//m1!m1] took 42 ms.
MyEclippse中使用struts-default.xml中定义的拦截器(timmer,logger)