你好映射css不起作用。
这是我的配置:
弹簧servlet.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<context:component-scan base-package="spring.controllers" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/content/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="it.jsoftware.jacciseweb.controllers"></context:component-scan>
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/js/**" location="/js/" />
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<display-name>JewishProjectT</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
和我的文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<c:choose>
<c:when test="${sessionScope['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'] eq 'i18en'}">
<c:set var="approach" value="left" scope="page"></c:set>
</c:when>
<c:when test="${sessionScope['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'] eq 'i18ru'}">
<c:set var="approach" value="left" scope="page"></c:set>
</c:when>
<c:when test="${sessionScope['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'] eq 'i18he'}">
<c:set var="approach" value="right" scope="page"></c:set>
</c:when>
<c:otherwise>
<c:set var="approach" value="left"></c:set>
</c:otherwise>
</c:choose>
<link rel="stylesheet" media="all" type="text/css" href="/css/common.css">
<link rel="stylesheet" media="all" type="text/css" href="/css/lavamenu.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="/js/<c:out value="${approach}"/>/lavalamp.js"></script>
<title>${title}</title>
</head>
<body>
<div id="main">
在chrome中显示:
/js/left/lavalamp.js 404(未找到)0:19 GET
/css/common.css 404(未找到)0:17 GET
/css/lavamenu.css 404(未找到)
如何正确配置css / images / js的路径?
P.S。我排除了以http:\\开头的所有链接,因为stackoverflow的规则不允许我发布它们,因为我是初学者。
答案 0 :(得分:7)
您的<mvc:resources mapping="/css/**" location="/css/" />
对我来说似乎不错,但您确定您的应用程序在根目录中运行吗?您的网址不应该以其他上下文开头,例如:
href="/myapp/css/common.css"
或者,为了避免硬编码的引用:
href="<c:url value="/css/common.css" />"
如果您的应用程序在根目录中运行,那么您可能需要正确的名称空间声明?类似的东西:
<?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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">