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

发布于 2024-08-21 04:55:13 字数 2578 浏览 4 评论 0 原文

我有以下 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 目录中?

I have the following Stripes ActionBean:

package myapp;

import net.sourceforge.stripes.action.*;

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

When I load /myapp/Welcome.action in a browser, the contents of welcome.jsp are displayed.

However, when I move welcome.jsp to /WEB-INF/jsp/welcome.jsp and change the ForwardResolution argument to reflect that change, i.e.:

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

I get the following error when I load /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)

Is it necessary to perform any special configuration in order to store JSP files in the WEB-INF directory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人生戏 2024-08-28 04:55:13

我的理解如下:您的 WelcomeActionBean 不在由 NameBasedActionResolver (阅读javadoc)所以它实际上是映射到 /myapp/Welcome.action (如错误消息中所述)。

因此,当您请求 /Welcome.action 时,没有任何现有 ActionBean 绑定到该 URL,解析器会回退到 /welcome.jsp > (再次参见 NameBasedActionResolver javadoc)。当您将 JSP 移动到 /WEB-INF/jsp 下时,您就会运气不佳,一切都会失败。

要解决此问题,可以:

  • 访问“正确”(在当前状态下)URL 绑定,即 /myapp/Welcome.action

  • 或者,如果您希望 ActionBean 按照约定绑定到 /Welcome.action,请将其移动到由 NameBasedActionResolver,例如action

    package myapp.action;
    
    导入 net.sourceforge.stripes.action.*;
    
    公共类WelcomeActionBean扩展MyAppActionBean {
        @DefaultHandler
        公共分辨率视图(){
            返回新的 ForwardResolution("/WEB-INF/jsp/welcome.jsp");
        }
    }
    
  • 或者将 @UrlBinding 添加到您的操作中以显式配置绑定:

    打包myapp;
    
    导入 net.sourceforge.stripes.action.*;
    
    @UrlBinding("/Welcome.action")
    公共类WelcomeActionBean扩展MyAppActionBean {
        @DefaultHandler
        公共分辨率视图(){
            返回新的 ForwardResolution("/WEB-INF/jsp/welcome.jsp");
        }
    }
    

My understanding is the following: your WelcomeActionBean in not in a package ([web, www, stripes, action]) automagically handled by the NameBasedActionResolver (read the javadoc) so it is actually mapped to /myapp/Welcome.action (as stated in the error message).

So, when you request /Welcome.action, there isn't any existing ActionBean bound to that URL and the resolver fallbacks to /welcome.jsp (again, see the NameBasedActionResolver javadoc). And when you move your JSP under /WEB-INF/jsp, well, you run out of luck and everything just fails.

To solve this, either:

  • Access the "right" (in the current state) URL binding i.e. /myapp/Welcome.action

  • Or, if you want your ActionBean to be bound to /Welcome.action by the conventions, move it in a package handled by the NameBasedActionResolver, e.g. 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");
        }
    }
    
  • Or add a @UrlBinding to your action to configure the binding explicitly:

    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");
        }
    }
    
故人的歌 2024-08-28 04:55:13

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

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

WEB-INF is a special directory, and its contents aren't accessible to the client. (It makes sense - you wouldn't want the client to be able to download your web.xml or your .class files.)

You need to move the JSP files outside of WEB-INF.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文