简体   繁体   English

无法确定拦截器在Struts2中是否正常工作

[英]Unable to determine whether the interceptor is working or not in Struts2

I have configured struts.xml file as follows : 我已将struts.xml文件配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!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.devMode" value="true" />
   <package name="helloworld" extends="struts-default">

      <action name="hello" 
            class="com.struts2examples.HelloWorldAction" 
            method="execute">
            <result name="success">/HelloWorld.jsp</result>
            <result name="error">/AccessDenied.jsp</result>
            <interceptor-ref name="params"/>
            <interceptor-ref name="timer" />
      </action>
   </package>
</struts>

It should log the time of execution action hello . 它应该记录执行动作的时间hello When I am calling the hello action on server, Logs get generated in tomcat logs as : 当我在服务器上调用hello操作时,tomcat日志中生成的日志为:

0:0:0:0:0:0:0:1 - - [18/Nov/2013:17:24:38 +0530] "GET /StrutsHelloWorld/Login.jsp HTTP/1.1" 404 1033
0:0:0:0:0:0:0:1 - - [18/Nov/2013:17:29:30 +0530] "POST /Struts2HelloWorld/hello HTTP/1.1" 200 129
0:0:0:0:0:0:0:1 - - [18/Nov/2013:17:29:41 +0530] "POST /Struts2HelloWorld/hello HTTP/1.1" 200 105
0:0:0:0:0:0:0:1 - - [18/Nov/2013:17:31:04 +0530] "POST /Struts2HelloWorld/hello HTTP/1.1" 200 105

Nowhere the interceptor logs are shown. 没有显示拦截器日志的位置。 I am accessing the localhost_access_log.2013-11-18.txt file in tomcat logs. 我正在访问tomcat日志中的localhost_access_log.2013-11-18.txt文件。 Am I accessing the wrong file? 我访问的文件错误吗? If it is then where would be the logs generated? 如果是,那么将在哪里生成日志?

You didn't specified which library you are using (eg. Log4j), nor how it's configured, but you should look inside your log file, not in tomcat one. 您没有指定正在使用哪个库(例如Log4j),也没有指定其配置方式,但是您应该查看日志文件内部,而不是在tomcat目录中。

From the documentation : 文档中

This interceptor logs the amount of time in milliseconds. 该拦截器以毫秒为单位记录时间。 In order for this interceptor to work properly, the logging framework must be set to at least the INFO level. 为了使此拦截器正常工作,必须将日志记录框架至少设置为INFO级别。 This interceptor relies on the Commons Logging API to report its execution-time value . 该拦截器依靠Commons Logging API报告其执行时间值

Parameters 参数

  • logLevel (optional) - what log level should we use (trace, debug, info, warn, error, fatal)? logLevel (可选)-我们应该使用什么日志级别(跟踪,调试,信息,警告,错误,致命)? - defaut is info -defaut信息

  • logCategory (optional) - If provided we would use this category (eg. com.mycompany.app). logCategory (可选)-如果提供,我们将使用此类别(例如com.mycompany.app)。 Default is to use com.opensymphony.xwork2.interceptor.TimerInterceptor . 默认是使用com.opensymphony.xwork2.interceptor.TimerInterceptor

The parameters above enables us to log all action execution times in our own logfile . 上面的参数使我们能够将所有操作执行时间记录在我们自己的日志文件中

That said, you are using only TWO interceptors; 也就是说,您仅使用两个拦截器; it would be better to add the Timer Interceptor to the defaultStack or at least to the basicStack (or to a custom stack of your). 最好将Timer Interceptor添加到defaultStack或至少添加到basicStack(或您的自定义堆栈)。 Putting it after the stack will log only Action's execution time, while before the stack will log Interceptors's execution time too: 将其放在堆栈之后将仅记录Action的执行时间,而在堆栈之前也将记录拦截器的执行时间:

Log only Action's execution time: 仅记录Action的执行时间:

<action name="hello" class="com.struts2examples.HelloWorldAction">
    <interceptor-ref name="defaultStack"/>
    <interceptor-ref name="timer" />
    <result>/HelloWorld.jsp</result>
    <result name="error">/AccessDenied.jsp</result>
</action>

Log Action's and Interceptor's execution time: 记录操作和拦截器的执行时间:

<action name="hello" class="com.struts2examples.HelloWorldAction">
    <interceptor-ref name="timer" />
    <interceptor-ref name="defaultStack"/>
    <result>/HelloWorld.jsp</result>
    <result name="error">/AccessDenied.jsp</result>
</action>

Note that method="execute" or result name="success" are not necessary because they're the default. 请注意,因为它们是默认设置,所以不需要method="execute"result name="success"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM