我用的eclipse配置都弄了,也能輸出就是不能輸出時(shí)間???求教。
struts.xml
<?xml?version="1.0"?encoding="UTF-8"?>
<!--?指定Strust配置文件的DTD信息?-->
<!DOCTYPE?struts?PUBLIC
"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<!--?指定Struts2配置文件的根元素?-->
<struts>
<package?name="default"??namespace="/"??extends="struts-default">
<!--?注冊攔截器?-->
<interceptors>
<interceptor?name="mytimer"?class="com.imooc.interceptor.TimerInterceptor"></interceptor>
</interceptors>
<action?name="timer"?class="com.imooc.action.TimerAction">
<result>/success.jsp</result>
<!--?引用攔截器?-->
<interceptor-ref?name="mytimer"></interceptor-ref>
</action>
</package>
</struts>
TimerAction.java
package?com.imooc.action;
import?com.opensymphony.xwork2.ActionSupport;
public?class?TimerAction?extends?ActionSupport?{
@Override
public?String?execute()?throws?Exception?{
for(int?i=0;?i<10000;i++){
System.out.println("陳京輝");
}
return?SUCCESS;
}
}
TimerInterceptor.java
package?com.imooc.interceptor;
import?com.opensymphony.xwork2.ActionInvocation;
import?com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/*
?*?計(jì)算Action花費(fèi)時(shí)間
?*/
public?class?TimerInterceptor?extends?AbstractInterceptor?{
@Override
public?String?intercept(ActionInvocation?invocation)?throws?Exception?{
//1.執(zhí)行Action之前
long?start?=?System.currentTimeMillis();
//2.執(zhí)行下一個(gè)攔截器,如果已經(jīng)是最后一個(gè)攔截器,則執(zhí)行目標(biāo)Action
String?result?=?invocation.invoke();
//3.執(zhí)行Action之后
long?end?=?System.currentTimeMillis();
System.out.println("執(zhí)行Action花費(fèi)的時(shí)間:"?+?(end?-?start)?+?"ms");
return?result;
}
}
2017-06-05
攔截器不僅要在struts.xml注冊,也要在web.xml里注冊
2017-05-19
輸不出什么?