2011-12-20 104 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'/>">