spring中找不到css、图片和js静态文件 [英] can't find css, images and js static files in spring
本文介绍了spring中找不到css、图片和js静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 servlet-context.xml 文件
my servlet-context.xml 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: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>
我使用 thymeleaf 访问图像
And i access the image using thymeleaf like
<img src="../../resources/img/cancel.png" th:attr="src=@{resources/img/cancel.png}, title=#{background}, alt=#{background}" />
但是什么都没有显示..!
But nothing is showing..!
推荐答案
如果你有java项目并且你的resources/img/**或resources/css/**文件夹在WebContent下,你可以像这样访问你的资源
If you have java project and your resources/img/** or resources/css/** folder comes under WebContent you can access the your resources like this
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/styleV.css"
type="text/css" />
以及类似的图像和js文件方式.
and similar way for images and js file.
或者第二种方法是您可以在 web.xml 中为您的 servlet 添加以下映射.
Or second approach is you can add following mapping in web.xml for your 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>
并以
<link rel="stylesheet" type="text/css" href="resources/css/jquery-ui-1.8.22.custom.css">
这篇关于spring中找不到css、图片和js静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文