404 在 web.xml 中定义默认 servlet 时访问静态内容时出错

发布于 2024-11-04 19:47:02 字数 1076 浏览 3 评论 0原文

我正在 Tomcat 7 容器中运行 Web 应用程序,并在尝试访问静态内容(.css 等)时收到 404 错误。以下是我的目录结构:

  • ROOT
    • 元信息
    • 资源
      • CSS
    • WEB-INF
      • 课程

我在部署描述符中定义了一个默认 servlet,如下所示:

<servlet>
    <servlet-name>HomeController</servlet-name>
    <servlet-class>controller.HomeController</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>HomeController</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

HomeController servlet 将请求转发到 .jsp,并且正确呈现视图。

request.getRequestDispatcher("view.jsp").forward(request,
        response);

“view.jsp”有一个指向位于上面列出的 css 文件夹中的样式表 (style.css) 的链接。但是,由于 servlet 配置为默认 servlet,我现在无法访问 css 文件夹中的静态内容,并且对此样式表的任何请求都会返回 404 错误。

<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/style.css" />

有什么办法解决这个问题吗?提供静态资源但仍然能够定义默认 servlet 的最佳方法是什么?

I am running a webapp in a Tomcat 7 container, and am recieving 404 errors when attempting to access static content (.css, etc.). Below is my directory structure:

  • ROOT
    • META-INF
    • resources
      • css
    • WEB-INF
      • classes
      • lib

I have defined a default servlet in my deployment descriptor as follows:

<servlet>
    <servlet-name>HomeController</servlet-name>
    <servlet-class>controller.HomeController</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>HomeController</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

The HomeController servlet forwards the request to a .jsp, and the view is rendered properly.

request.getRequestDispatcher("view.jsp").forward(request,
        response);

"view.jsp" has a link to a stylesheet (style.css) located in the css folder listed above. However, because the servlet is configured as a default servlet, I am now not able to access the static content in the css folder, and any request for this stylesheet returns a 404 error.

<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/style.css" />

Is there any way around this? What is the best method for serving up static resources, but still being able to define a default servlet?

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

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

发布评论

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

评论(2

红焚 2024-11-11 19:47:02

不要自行开发您自己的默认 servlet。使用servlet容器的内置默认servlet。您的前端控制器应该映射到更具体的 URL 模式,例如 *.html/pages/*

如果您的目的是不更改 URL,那么您应该创建一个附加的 Filter 类,该类映射到 /* 上,并且当 /resources 时继续该链/* 被请求,否则转发到映射到 /pages/* 上的前端控制器 servlet。

例如

String uri = ((HttpServletRequest) request).getRequestURI();
if (uri.startsWith("/resources/")) {
    chain.doFilter(request, response); // Goes to container's default servlet.
} else {
    request.getRequestDispatcher("/pages" + uri).forward(request, response);
}

,将 CSS 链接到 URL 中的 /resources 路径,就像您在公共 webcontent 文件夹结构中一样。

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/css/style.css" />

另请参阅:

Don't homegrow your own default servlet. Use the servletcontainer's builtin default servlet. Your front controller should be mapped on a more specific URL pattern, e.g. *.html or /pages/*.

If your intent is to not change the URLs, then you should create an additional Filter class which is mapped on /* and just continues the chain when the /resources/* is requested and otherwise forward to the front controller servlet which is mapped on /pages/*.

E.g.

String uri = ((HttpServletRequest) request).getRequestURI();
if (uri.startsWith("/resources/")) {
    chain.doFilter(request, response); // Goes to container's default servlet.
} else {
    request.getRequestDispatcher("/pages" + uri).forward(request, response);
}

And link your CSS just with the /resources path in the URL, exactly as you have in your public webcontent folder structure.

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/css/style.css" />

See also:

揽月 2024-11-11 19:47:02

您不应该像这样从资源路径链接 css 吗:

<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/resources/css/style.css" />

Shouldn't you be linking css from resources path like this:

<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/resources/css/style.css" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文