Spring MVC JSP无法找到Javascript或CSS [英] Spring MVC JSPs Cannot Locate Javascript or CSS

查看:99
本文介绍了Spring MVC JSP无法找到Javascript或CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题似乎已经被问过几次了,但是在检查了太多以至于无法计数之后,我仍然看到了这个问题.

This question seems to have been asked a few times, but after reviewing too many of them to count, I am still seeing this problem.

我的Spring MVC项目中的.jsp不能找到任何.css或.js文件,即使它们处于战争状态并且给定的文件路径正确.

The .jsp's in my Spring MVC project are unable to locate any .css or .js files even though they are in the war and the given file paths are correct.

根据我的配置,我看到两个错误.第一个是:

Depending on my configuration I see two errors. The first being:

WARNING: No mapping found for HTTP request with URI [/waypoint/css/bootstrap.min.css] in DispatcherServlet with name 'waypoint'

请注意,我的控制器已到达并且正在查看页面,只是没有任何格式或JavaScript.

Note that my controller is being reached and I am seeing the page, just without any formatting or javascript.

我在以下配置下看到此错误:

I see this error with the following configuration:

web.xml

<servlet-mapping>
    <servlet-name>waypoint</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

waypoint-servlet.xml

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

文件结构:

WEB-INF
| - css
|   ` - all .css files
| - js
|   ` - you guessed it...
| - pages
|   ` - all .jsp files
| - waypoint-servlet.xml
` - web.xml

我已经阅读了许多其他文章,这些文章已使用mvc:resource标记解决了此问题.

I have read a lot of other posts that have resolved this issue using the mvc:resource tag.

当包含mvc:resource标记时,.jsp出现404错误.即使我将servlet映射更改为/*/**/<some-name>//<some-name>/*,也会发生这种情况.

When I include the mvc:resource tag, I see 404 errors for my .jsp's. This occurs even when I change the servlet mapping to /*, /**, /<some-name>/, and /<some-name>/*.

我也尝试过将静态文件移到WEB-INF文件夹之外:

I have also tried moving the static files outside of the WEB-INF folder:

| - resources
|  | - css
|  |   ` css...
|  | - js
|      ` js...
| - WEB-INF

正如我提到的,我为上述两个文件结构尝试了mvc:resource标记的许多不同映射组合.一些示例包括:

As I mentioned, I tried many different mapping combinations for the mvc:resource tag, for both of the above file structures. Some examples include:

<mvc:resources mapping="waypoint/resources/css/**" location="/resources/css/" />
<mvc:resources mapping="resources/css/**" location="/resources/css/" />
<mvc:resources mapping="css/**" location="/resources/css/" />
<mvc:resources mapping="css/**" location="WEB-INF/css/" />
<mvc:resources mapping="css/**" location="css/" />

我也在.jsp方面尝试了以下内容的组合:

I tried the above with combinations of the following on the .jsp side as well:

<link href="classpath:/resources/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="../css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="<c:url value="../resources/css/bootstrap.min.css"/>" rel="stylesheet" type="text/css" />
<link href="<c:url value="../css/bootstrap.min.css"/>" rel="stylesheet" type="text/css" />

在此之前有人有能力提供帮助吗?这将不胜感激.如果您需要其他任何信息,请告诉我.

Has anyone experienced this before that might be able to lend a hand? It would be greatly appreciated. If you need any other info please let me know.

谢谢.

解决方案

我能够通过将* .css和* .js的servlet映射添加到fefault servlet中来解决此问题:

I was able to resolve this issue by adding servlet mappings for *.css and *.js to the fefault servlet:

<servlet-mapping>
    <servlet-name>waypoint</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>

这需要删除<mvc:resource>标签.我还需要使用WEB-INF文件夹之外的resource/css/目录.我在JSP中引用了js和css文件,如下所示:

This required the removal of the <mvc:resource> tags. I also required the use of the resource/css/ directory outside of the WEB-INF folder. I referenced the js and css files in the JSPs as follows:

<link href="<c:url value="/resources/css/bootstrap.min.css"/>" rel="stylesheet" type="text/css" />

感谢阅读!

推荐答案

我不确定为什么<mvc:resources>标记在您的情况下不起作用,因为它应从3.2.0春季开始使用.在您的web.xml中添加以下几行,这些是不安网址的常见问题

I am not sure why the <mvc:resources> tag is not working in your case, as it should from the spring 3.2.0. Add the below lines in your web.xml, these are the common problems with the restless urls

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/images/*</url-pattern>
</servlet-mapping>

您需要将文件放置在web-inf之外,以使其对截获的请求可见.

You need to place your files outside the web-inf to make it visible to the intercepted requests.

一个不错的 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆