2011-12-20 105 views
10

我已經創建了MVC應用程序。Spring MVC - 包含靜態文件/ javascript,css

我想將js或css文件包含到jsp中。

我的靜態文件下AR:

 
- webapp 
     -js/jquery.js 
     -WEB-INF| 
       | 
       - jsp/*.jsp 

我的代碼,包括jQuery是:

<script type="text/javascript" src="<c:url value="js/jquery.js" />"></script> 

,我不能加載JS文件進入視野。

我看到日誌與信息:

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/pool/js/jquery.js] in DispatcherServlet with name 'appServlet' 

是什麼意思,是MVC嘗試URL映射到js文件。

我認爲有一些與我的配置,但我不知道是什麼。

我的web.xml是:

<?xml version="1.0" encoding="UTF-8"?> 

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd「>

<!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring/root-context.xml</param-value> 
</context-param> 

<!-- Creates the Spring Container shared by all Servlets and Filters --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<!-- Processes application requests --> 
<servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

    <filter> 
    <filter-name>hibernateFilter</filter-name> 
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> 
    </filter> 
    <filter-mapping> 
    <filter-name>hibernateFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
    </filter-mapping> 

+0

參見:http://stackoverflow.com/questions/10495571/spring-not-finding-resource-files-css-jsp – ALOToverflow 2013-10-17 17:08:42

回答

6

將您的DispatcherServlet映射更改爲例如:

<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>*.jsp</url-pattern> 
</servlet-mapping> 

或其他一些沒有衝突的url-pattern,如*.htm/controllers/*。請記住,從現在開始,所有的控制器都只能通過這種模式使用。

現在,它在你的web應用攔截一切,包括.js文件,圖像等

hibernateFilter同樣的事情 - 你取.js文件時,並不真正需要一個開放的Hibernate會話,不您?

+0

但是,當我這樣做時,我的實際映射被破壞了。更改我的網址後:http:// localhost:8080/pool/main不起作用 – Ilkar 2011-12-20 22:21:28

+0

One chenge - 我添加了另一個servlet-mapping,其中包含* .js – Ilkar 2011-12-20 22:52:06

0

使用Spring JSTL標記,包括外部腳本文件或樣式表。 首先,您應該在JSP中包含taglib,如下所示。

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%> 

然後你就可以使用,

<script type="text/javascript" src="<spring:url value="/js/jquery.js"/>"></script> 
0

我對你的回答同意包括extenal腳本文件。但是在style.css文件中聲明瞭與圖像路徑相關的url。

- style。css--

.cwt-object0 
{ 
    display: block; 
    left: 2.62%; 
    margin-left: -1px; 
    position: absolute; 
    top: 43px; 
    width: 64px; 
    height: 64px; 
    background-image: url('/resources/images/object0.png'); 
    background-position: 0 0; 
    background-repeat: no-repeat; 
    z-index: 0; 
} 

如何使用標籤<spring:url></spring:url>中的style.css文件在瀏覽器中看到IE/Firefox的

--jsp文件---

<link href="<spring:url value="/resources/style.css"/>" rel="stylesheet" type="text/css" media="screen"> 
2

爲什麼不使用簡單jsp核心?

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
<link rel="stylesheet" type="text/css" href="<c:url value='/resources/css/bootstrap.css'/>" /> 
+0

簡單的解決方案。 +1 – 2013-10-01 15:26:12

0

添加MVC:資源的配置文件(* -servlet.xml後綴),你可以找到它的工作原理

0

我只是跟着Mkyong Tutorial放置CSS,JS,jQuery的&圖像文件。它爲我工作。

在servlet的context.xml中

<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
     up static resources in the ${webappRoot}/resources directory --> 
    <resources mapping="/resources/**" location="/resources/assets/" /> 

在JSP中,進口標籤庫

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 

,並添加像

<link rel="stylesheet" href="<c:url value='/resources/css/custom.css'/>">