一直在路上 没有放弃 2019-06-03 11:26 采纳率: 40%
浏览 759

为什么我的struts2拦截器不起作用

我在网上学了点拦截器,自己试验了一下,发现程序运行没有问题,但是拦截器貌似没有生效

网上说这么应该会输出几行文字

Pre-Processing

Inside action....

Post-Processing

但是我这只输出了一行文字

Inside action....

网上web.xml中

   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
   </filter>

   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>

filter-class中的内容不同,我改为网上的内容后,就会报错,
One or more Filters failed to start. Full details will be found in the appropriate container log file

现在,在给定的文本框中输入任何单词,然后单击“Say Hello按钮执行已定义的动作。现在,如果你将检查生成的日志,你会发现下面的文字在底部:<br>
Pre-Processing<br>
Inside action....<br>
Post-Processing<br>
struts.xml文件配置
<package name="helloworld" extends="struts-default">
   <interceptors>
         <interceptor name="Login"
            class="struts2.src.com.struts2.Interceptor.LoginInterceptor" >
            </interceptor>
      </interceptors>
        <action name="Hello" class="com.struts2.demo.HelloAction">
            <interceptor-ref name="params"/>
            <interceptor-ref name="Login" />

            <result name="success">HelloWorld.jsp</result>
        </action>
    </package>
拦截器
package com.struts2.Interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@SuppressWarnings("serial")
public class LoginInterceptor extends AbstractInterceptor {

    @Override
    public String intercept(ActionInvocation invocation)throws Exception{

          /* let us do some per-processing */
          String output = "Pre-Processing"; 
          System.out.println(output);

          /* let us call action or next intercepter */
          String result = invocation.invoke();

          /* let us do some post-processing */
          output = "Post-Processing"; 
          System.out.println(output);

          return result;

       }

}
web.xml
<?xml version="1.0" encoding="UTF-8"?>

<web-app id="starter" version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <display-name>Struts 2 Rest Example</display-name>


  <!-- Filters -->
  <!-- START SNIPPET: filter -->
    <filter>
        <filter-name>action2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <!-- END SNIPPET: filter -->

    <filter-mapping>
        <filter-name>action2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Welcome file lists -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>


  • 写回答

1条回答

  • 你就像甜甜的益达 2019-06-04 11:36
    关注

    求求你,别看struts,学习springmvc吧.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题