Java Spring-Eleaf未加载CSS(xml配置)
我正在用thymeleaf构建一个SpringWeb应用程序。我基于xml进行了配置,除了css资源引用之外,其他一切都可以正常工作。我使用的是spring版本5.0.1.RELEASE和thymeleaf版本3.0.1.RELEASE 登录页面正在工作,但css未加载。我认为问题出在配置上 以下是项目布局:Java Spring-Eleaf未加载CSS(xml配置),java,xml,spring,spring-mvc,thymeleaf,Java,Xml,Spring,Spring Mvc,Thymeleaf,我正在用thymeleaf构建一个SpringWeb应用程序。我基于xml进行了配置,除了css资源引用之外,其他一切都可以正常工作。我使用的是spring版本5.0.1.RELEASE和thymeleaf版本3.0.1.RELEASE 登录页面正在工作,但css未加载。我认为问题出在配置上 以下是项目布局: Spring |- src/main/java/ | |- com.web | | |- WebContent | |- META-INF | | | |- WEB-INF |
Spring
|- src/main/java/
| |- com.web
| |
|- WebContent
| |- META-INF
| |
| |- WEB-INF
| |- WEB-INF
| |- lib
| |- resources
| |- css
| |- style.css
| |- views
| |- login
| |- login.html
| |- web.xml
| |- applicationConfig-mvc.xml
| |- applicationContext.xml
我的web.xml是
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
</web-app>
org.springframework.web.context.ContextLoaderListener
上下文配置位置
/WEB-INF/applicationContext.xml
调度员
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
1.
调度员
/
违约
*.css
MyapplicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p"
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/aop
http://www.springframework.org/schema/aop/spring-aop-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<import resource="applicationConfig-mvc.xml" />
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="com.web" />
</beans>
<?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/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">
<mvc:annotation-driven />
<mvc:resources location="/resources/" mapping="/resources/**" />
<mvc:resources location="/resources/css/" mapping="/resources/css/**" />
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/resources/views/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="true" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="1" />
<property name="viewNames" value="*" />
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</bean>
</beans>
以及我的应用程序配置mvx.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p"
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/aop
http://www.springframework.org/schema/aop/spring-aop-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<import resource="applicationConfig-mvc.xml" />
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="com.web" />
</beans>
<?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/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">
<mvc:annotation-driven />
<mvc:resources location="/resources/" mapping="/resources/**" />
<mvc:resources location="/resources/css/" mapping="/resources/css/**" />
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/resources/views/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="true" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="1" />
<property name="viewNames" value="*" />
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</bean>
</beans>
Login.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Login page</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="../../css/style.css"
th:href="@{/resources/css/style.css}" />
</head>
<body>
<h1>Login page</h1>
<p th:if="${loginError}" class="error">Wrong user or password</p>
<form th:action="@{/login}" method="post">
<label for="username">Username</label>:
<input type="text" id="username" name="username" autofocus="autofocus" />
<br />
<label for="password">Password</label>:
<input type="password" id="password" name="password" />
<br />
<input type="submit" value="Log in" />
</form>
<p>
<a href="../index.html" th:href="@{/}">Back to home page</a>
</p>
</body>
</html>
登录页面
登录页面
错误的用户或密码
用户名:
密码:
如果您对结构、配置或命名有任何意见,请帮助我,因为我现在正在学习如何操作。谢谢大家! 一开始我也遇到了同样的问题:) 把你的css、js、图像。。。在资源目录中的“静态”文件夹中。否则百里香就找不到了。
它应该是这样的:resources->static->css->style.css
您现在可以通过href=“css/style.css”
js、图像等也是如此 编辑:添加示例项目的图片
查看下面的演示项目!:) 也许放一个字体会有帮助<代码>不,这不起作用。我认为问题在于我在applicationConfig-mvx.xml中声明资源映射的配置。您能否告诉我
的配置文件中必须包含哪些内容,以及html中的href是什么。类似于
?谢谢,您不需要使用我建议的配置指定位置。您可以使用th:href=。。。或href=。。。在我看来,两者都用是没有意义的。请看我原始答案的编辑。我添加了项目结构的图像和一个示例html文件。我测试了它,它在我的设置中工作。尝试仅使用href=“css/style.css”(删除th:href=“…”)来指定样式表,看看是否有效。如果没有,告诉我,我们会找到另一个解决方案。干杯,非常感谢你。你真的很完美!我找到了另一个解决方案,如果项目是动态Web项目,并且您使用的是WebContent和Web-INF,资源目录应该位于WebContent目录的根目录和html
中,因此项目结构是:WebContent META-INF WEB-INF模板/*resources css/*