2016-07-27 60 views
0

我嘗試了各種教程,但它完全不起作用。我不能在jsp頁面中包含css文件

我正在用boostrap構建spring應用程序。我想在我的jsp頁面中包含bootstrap.min.css。

這裏是我的項目樹:

這是我的Spring配置文件:

Spring Configuration File

<?xml version="1.0" encoding="UTF-8"?> 
<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-4.0.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 

    <mvc:annotation-driven /> 
    <context:component-scan base-package="com.packt.webstore" /> 
    <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" /> 
    <mvc:resources location="WEB-INF" mapping="/resource"/> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/views/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
     <property name="maxUploadSize" value="10240000"/> 
    </bean> 
</beans> 

這一行是很重要的:

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

這是我的看法頁:

View Page

我上傳的圖像故意顯示,我甚至無法導入它,因爲它不認識路徑。那麼我嘗試了其他配置,但我無法弄清楚如何以正確的方式做到這一點。

+1

你的問題到底是什麼?從你的問題或圖片中,我不清楚哪些不起作用。 – bpachev

+0

[spring mvc放置css/js/img文件的位置]的可能重複(http://stackoverflow.com/questions/14171862/spring-mvc-where-to-put-css-js-img-files) – ochi

回答

0

可以使用JSTL:使用JSP和servlet

<spring:url value="/resources/js/jquery.1.10.2.min.js" var="jqueryJs" /> 
<spring:url value="/resources/js/main.js" var="bootstrap" /> 

<link href="${bootstrap}" rel="stylesheet" /> 
<script src="${jqueryJs}"></script> 

<link href="${pageContext.request.contextPath}/resources/css/main.css" rel="stylesheet" > 
<script src="${pageContext.request.contextPath}/resources/css/jquery.js"></script> 
0

前段時間我創建簡單的Web應用程序:

<link href="<c:url value="/resources/css/main.css" />" rel="stylesheet"> 
<script src="<c:url value="/resources/js/jquery.1.10.2.min.js" />"></script> 

或Spring標籤和我有同樣的問題。我已通過在每個網址開頭處添加上下文名稱來解決此問題。以下是該網址的示例:

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

<script src="${pageContext.request.contextPath}/js/edit-contact-form-scripts.js"></script> 
<script src="${pageContext.request.contextPath}/js/datetimepicker.js"></script> 

試試吧。我希望這有幫助。