我的servlet-context.xml文件
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<context:component-scan base-package="com.aitrich.learnware.web" />
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<!-- Maps '/' requests to the 'home' view -->
<mvc:view-controller path="/" view-name="home" />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/css/**" location="/resources/css/" />
<mvc:resources mapping="/resources/js/libs/**" location="/resources/js/libs/" />
<mvc:resources mapping="/resources/img/**" location="/resources/img/" />
<!-- Internationalization support -->
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en_US" />
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="language" />
</bean>
<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="locale/messages" />
</bean>
<!-- **************************************************************** -->
<!-- THYMELEAF-SPECIFIC ARTIFACTS -->
<!-- TemplateResolver <- TemplateEngine <- ViewResolver -->
<!-- **************************************************************** -->
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
<!-- These lines configure the dialects to use with Thymeleaf -->
<property name="dialects">
<set>
<bean class="org.thymeleaf.spring3.dialect.SpringStandardDialect" />
<bean class="nz.net.ultraq.web.thymeleaf.LayoutDialect" />
</set>
</property>
</bean>
<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
</bean>
</beans>
我使用百日咳来访问图像
<img src="../../resources/img/cancel.png" th:attr="src=@{resources/img/cancel.png}, title=#{background}, alt=#{background}" />
但没有任何表现......!
答案 0 :(得分:5)
如果您有java项目,并且您的资源/ img / **或resources / css / **文件夹位于WebContent下,您可以访问您的资源
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/styleV.css"
type="text/css" />
和图像和js文件的类似方式。
或者第二种方法是,您可以在web.xml中为servlet添加以下映射。
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
并以
的形式访问css<link rel="stylesheet" type="text/css" href="resources/css/jquery-ui-1.8.22.custom.css">
答案 1 :(得分:1)
尝试添加到web.xml
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.ico</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
和我的css / js / img位于 - webapp / js /,webapp / img /,webapp / css /
答案 2 :(得分:0)
我刚刚在Linux上发现我必须删除起始&#34; /&#34;在我的资源映射!
看到这里
<!-- NOTE the location does not have a starting /!!!! -->
<resources mapping="/resources/**" location="resources/" />
男孩这样做会让我很沮丧。所有示例都有位置的/前缀。现在通过调用下面的c:url模式示例来运行它很好:
<link rel="stylesheet"
href="<c:url value="/resources/css/coreStyle.css" />" type="text/css">