使用Stripes将JSP文件移动到WEB-INF目录时出错

时间:2010-02-12 08:07:19

标签: java web-applications stripes web-inf

我有以下Stripes ActionBean:

package myapp;

import net.sourceforge.stripes.action.*;

public class WelcomeActionBean extends MyAppActionBean {
    @DefaultHandler
    public Resolution view() {
        return new ForwardResolution("/welcome.jsp");
    }
}

当我在浏览器中加载/myapp/Welcome.action时,会显示welcome.jsp的内容。

但是,当我将welcome.jsp移动到/WEB-INF/jsp/welcome.jsp并更改ForwardResolution参数以反映该更改时,即:

return new ForwardResolution("/WEB-INF/jsp/welcome.jsp");

加载/myapp/Welcome.action时出现以下错误:

net.sourceforge.stripes.exception.ActionBeanNotFoundException: Could not locate an ActionBean that is bound to the URL [/Welcome.action]. Commons reasons for this include mis-matched URLs and forgetting to implement ActionBean in your class. Registered ActionBeans are: {/controller/DefaultView.action=class net.sourceforge.stripes.controller.DefaultViewActionBean, /myapp/MyApp.action/=class myapp.MyAppActionBean, /myapp/Welcome.action/=class myapp.WelcomeActionBean, /controller/DefaultView.action/=class net.sourceforge.stripes.controller.DefaultViewActionBean, /myapp/MyApp.action=class myapp.MyAppActionBean, /myapp/Welcome.action=class myapp.WelcomeActionBean}
    net.sourceforge.stripes.controller.AnnotatedClassActionResolver.getActionBean(AnnotatedClassActionResolver.java:341)
    net.sourceforge.stripes.controller.NameBasedActionResolver.getActionBean(NameBasedActionResolver.java:264)
    net.sourceforge.stripes.controller.AnnotatedClassActionResolver.getActionBean(AnnotatedClassActionResolver.java:293)
    net.sourceforge.stripes.controller.DispatcherHelper$1.intercept(DispatcherHelper.java:106)
    net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:158)
    net.sourceforge.stripes.controller.BeforeAfterMethodInterceptor.intercept(BeforeAfterMethodInterceptor.java:113)
    net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:155)
    net.sourceforge.stripes.controller.ExecutionContext.wrap(ExecutionContext.java:74)
    net.sourceforge.stripes.controller.DispatcherHelper.resolveActionBean(DispatcherHelper.java:102)
    net.sourceforge.stripes.controller.DispatcherServlet.resolveActionBean(DispatcherServlet.java:238)
    net.sourceforge.stripes.controller.DispatcherServlet.service(DispatcherServlet.java:141)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    net.sourceforge.stripes.controller.StripesFilter.doFilter(StripesFilter.java:247)

是否需要执行任何特殊配置才能将JSP文件存储在WEB-INF目录中?

2 个答案:

答案 0 :(得分:4)

我的理解如下:WelcomeActionBean不在包中([web,www,stripes,action])由NameBasedActionResolver自动处理(读取javadoc),因此它实际上已映射至/myapp/Welcome.action(如错误消息中所述)。

因此,当您请求/Welcome.action时,没有任何现有的ActionBean绑定到该网址,并且解析程序会回退到/welcome.jsp(再次参见NameBasedActionResolver javadoc )。当你在/WEB-INF/jsp下移动你的JSP时,你运气不好,一切都失败了。

要解决此问题,请:

  • 访问“权限”(在当前状态下)网址绑定,即/myapp/Welcome.action

  • 或者,如果您希望ActionBean通过约定绑定到/Welcome.action,请将其移至NameBasedActionResolver处理的包中,例如{}。 action

    package myapp.action;
    
    import net.sourceforge.stripes.action.*;
    
    public class WelcomeActionBean extends MyAppActionBean {
        @DefaultHandler
        public Resolution view() {
            return new ForwardResolution("/WEB-INF/jsp/welcome.jsp");
        }
    }
    
  • 或者在您的操作中添加@UrlBinding以明确配置绑定:

    package myapp;
    
    import net.sourceforge.stripes.action.*;
    
    @UrlBinding("/Welcome.action")
    public class WelcomeActionBean extends MyAppActionBean {
        @DefaultHandler
        public Resolution view() {
            return new ForwardResolution("/WEB-INF/jsp/welcome.jsp");
        }
    }
    

答案 1 :(得分:-1)

WEB-INF是一个特殊目录,客户端无法访问其内容。 (这很有意义 - 您不希望客户端能够下载您的web.xml或您的.class文件。)

您需要将JSP文件移到WEB-INF之外。