404 在 web.xml 中定义默认 servlet 时访问静态内容时出错
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要自行开发您自己的默认 servlet。使用servlet容器的内置默认servlet。您的前端控制器应该映射到更具体的 URL 模式,例如
*.html
或/pages/*
。如果您的目的是不更改 URL,那么您应该创建一个附加的
Filter
类,该类映射到/*
上,并且当/resources 时继续该链/*
被请求,否则转发到映射到/pages/*
上的前端控制器 servlet。例如
,将 CSS 链接到 URL 中的
/resources
路径,就像您在公共 webcontent 文件夹结构中一样。另请参阅:
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.
And link your CSS just with the
/resources
path in the URL, exactly as you have in your public webcontent folder structure.See also:
您不应该像这样从资源路径链接 css 吗:
Shouldn't you be linking css from resources path like this: