2

我的 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>

我使用 thymeleaf 访问图像

<img src="../../resources/img/cancel.png" th:attr="src=@{resources/img/cancel.png}, title=#{background}, alt=#{background}" />

但是什么都没有显示..!

4

3 回答 3

5

如果您有 java 项目并且您的 resources/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>

并以

<link rel="stylesheet" type="text/css" href="resources/css/jquery-ui-1.8.22.custom.css">
于 2013-06-26T09:29:33.700 回答
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/

于 2013-06-26T08:46:17.240 回答
0

我刚刚在 Linux 上发现我必须删除资源映射中的起始“/”!
看这里

<!--    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">
于 2016-03-25T16:14:52.350 回答