spring-mvc - Spring 3 使用 Thymeleaf 进行静态资源映射

标签 spring-mvc spring-3 thymeleaf

我在使用 Spring 映射静态资源时遇到问题。我收到 404 错误。我的servlet代码如下

<?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/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

<context:component-scan base-package="web.controller" />

<!-- Enabling Spring MVC configuration through annotations -->
<mvc:annotation-driven />

<!-- Mapping Static Resources -->
<mvc:resources mapping="/css/**" location="/resources/css/" />
<mvc:resources mapping="/js/**" location="/resources/js/" />
<mvc:resources mapping="/images/**" location="/resources/images/" />
<mvc:resources mapping="/oldcss/**" location="/resources/oldResources/css/" />
<mvc:resources mapping="/oldjs/**" location="/resources/oldResources/js/" />
<mvc:resources mapping="/oldimages/**" location="/resources/oldResources/images/" />


<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" />
</bean>

<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine" />
    <property name="order" value="1" />
</bean>

我将静态对象称为 -

<link rel="stylesheet" type="text/css" href="../oldcss/reset.css" th:href="@{/oldcss/reset.css}"/>
    <link rel="stylesheet" type="text/css" href="../oldcss/skeleton.css" th:href="@{/oldcss/skeleton.css}"/>
    <link rel="stylesheet" type="text/css" href="../oldcss/style.css" th:href="@{/oldcss/style.css}"/>
    <!-- Import JS -->
    <script src="../oldjs/jquery.min.js" th:src="@{/oldjs/jquery.min.js}"></script>
    <script src="../oldjs/responsiveslides.min.js" th:src="@{/oldjs/responsiveslides.min.js}"></script>

谁能帮我找出可能的问题吗?预先感谢:)

最佳答案

解决了这个问题...必须将 servlet 中的映射更改为

<mvc:resources mapping="/css/**" location="/WEB-INF/resources/css/" />

关于spring-mvc - Spring 3 使用 Thymeleaf 进行静态资源映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15691158/

相关文章:

Spring 3 构造函数 Autowiring - 为什么这段代码有效?

java - Spring +Ehcache : Not able to get the cache back from cacheManager without key

spring - 如何在 thymeleaf 中为消息属性文件使用自定义位置?

java - 如何在 Spring MVC Web 应用程序中确认电子邮件地址

java - Spring添加web.xml配置时Session总是过期

java - 删除请求的 Controller 不起作用

Java Spring,使用域对象

spring - @Async 会发生什么。是否超时?

java - 当前登录的用户返回 Null,即使它显示当前用户信息

thymeleaf - 在Thymeleaf中动态创建的消息引用?