[英]I can not include javaScript and css to my JSP page
我不能在我的JSP页面中包含javaScript和CSS字段。 我想这个和这个 ,但没有帮助。
我的JSP:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="../resources/js/my.js"></script>
<link rel="stylesheet" href="../resources/css/menu.css" />
...
和映射:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.springapp.mvc"/>
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="resources" cache-period="31556926"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
和结构:
WEB-INF
-pages
-my.jsp
-resources
-js
-my.js
-css
-menu.css
-mvc-dispatcher-servlet.xml
-web.xml
新结构无济于事
webapp-资源-js -my.js -css -menu.css -WEB-INF-页面-my.jsp -mvc-dispatcher-servlet.xml -web.xml
我做错了什么?
将您的资源文件夹移到WEB-INF之外,然后如下所示。
<script src="/resources/js/my.js"></script>
<link rel="stylesheet" href="/resources/css/menu.css" />
您已经提到过,您不能通过相对路径引用WEB-INF文件夹中的资源。
尝试:
<script src="${pageContext.request.contextPath}/resources/js/my.js"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/menu.css" />
您的结构应为:
webapp
-WEB-INF
-mvc-dispatcher-servlet.xml
-web.xml
-pages
-my.jsp
-resources
-js
-my.js
-css
-menu.css
因此静态资源不在WEB-INF下。 <mvc:resources>
标签告诉spring根本不提供这些URL,将它们留给您的servlet容器(Tomcat等)提供。 但是Tomcat等不会直接从WEB-INF目录提供内容。
编辑
<mvc:resources mapping="/resources/**"
location="/"
cache-period="31556926" />
假设您的项目使用标准目录布局,请将Javascript放在src/main/webapp/js/my.js
,然后使用相对URL /resources/js/my.js
进行访问。 希望能有所帮助。
您的JSP中是否有可用的taglib?
如果是这样,请考虑以下内容: <script src="<c:url value="/js/my.js"/>"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.