javascript Spring找不到资源文件(css、jsp...)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10495571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring not finding resource files (css, jsp...)
提问by Marta
I'm using a jsp file as a model from a Controller and I want to use an css styles and js libraries
我使用 jsp 文件作为控制器的模型,我想使用 css 样式和 js 库
- Proyect
- Webcontent
- assets
- WEB-INF
- jsp
- 项目
- 网页内容
- 资产
- 网络信息
- jsp
In web.xml:
在 web.xml 中:
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>
<!-- Processes application requests -->
<servlet>
<servlet-name>MyProject</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyProject</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
In applicationContext.xml:
在 applicationContext.xml 中:
<context:annotation-config />
<context:component-scan base-package="main.mypack.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"/>
</bean>
And in my jsp file: href="/assets/css/jquery.mobile.fixedToolbar.polyfill.css"
在我的jsp文件中:href="/assets/css/jquery.mobile.fixedToolbar.polyfill.css"
Noy working, any help?
没有工作,有什么帮助吗?
EDIT: I'm using 2.5 Spring version, and I have errors like: No mapping found for HTTP request with URI [/MyProject/assets/js/jqm-project.js] in DispatcherServlet with name 'MyProject'
编辑:我使用的是 2.5 Spring 版本,并且出现以下错误:在 DispatcherServlet 中未找到带有 URI [/MyProject/assets/js/jqm-project.js] 的 HTTP 请求的映射,名称为“MyProject”
回答by Japan Trivedi
The problem is that your requests for CSS and JS files are going through Dispatcher Servlet, which is not correct. Hence Spring won't find the mapping for those files it will not load them.
问题是你对 CSS 和 JS 文件的请求是通过 Dispatcher Servlet,这是不正确的。因此 Spring 不会找到这些文件的映射,它不会加载它们。
You need to add the resourceHandler for your application in the applicationContext.xml file as follows. This configuration will bypass the requests for CSS and JS files from the Dispatcher Servlet.
您需要在 applicationContext.xml 文件中为您的应用程序添加 resourceHandler,如下所示。此配置将绕过 Dispatcher Servlet 对 CSS 和 JS 文件的请求。
<mvc:resources location="/assets/" mapping="/assets/**" />
Hope this helps you... Cheers.
希望这可以帮助你...干杯。
Please put following code in web.xml
请将以下代码放入 web.xml
<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>
回答by Marta
Auto-solved:
自动解决:
This mapping is blocking everything, I change this:
这个映射阻止了一切,我改变了这个:
<servlet-mapping>
<servlet-name>MyProject</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
For this (and I change my calls to "call.do"):
为此(我将我的电话更改为“call.do”):
<servlet-mapping>
<servlet-name>MyProject</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
And it works!
它有效!