gpt4 book ai didi

java - Spring 和 thymeleaf 布局方言不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 06:15:55 25 4
gpt4 key购买 nike

我是 Spring 和 thymeleaf 的新手,我正在使用 JSF + Facelets,因此我选择了 thymeleaf 布局方言,因为它与 Facelets 非常相似,但是,由于某种原因,它在我的简单项目中不起作用。

我的配置文件中有这个

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

public class AppInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext sc) throws ServletException {

AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(WebConfig.class);

}

}

WebConfig.java

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


@Configuration
@EnableWebMvc
@Import(ThymeLeafConfig.class)
public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}

}

我的 TyhmeLeafConfig

import nz.net.ultraq.thymeleaf.LayoutDialect;
import org.springframework.context.annotation.Bean;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;


public class ThymeLeafConfig {

@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/WEB-INF/");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
resolver.setOrder(1);
resolver.setCacheable(false);
return resolver;
}

@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setDialect(new LayoutDialect());
return templateEngine;
}

@Bean
public ThymeleafViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("UTF-8");
return viewResolver;
}
}

layout.html 文件

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<title>Layout page</title>
<script src="js/jquery.js"></script>
</head>
<body>
<header>
<h1>MASTER!!!!</h1>
</header>
<section layout:fragment="content">
<p>MASTER CODE</p>
</section>
<footer>
<p>My footer</p>
<p layout:fragment="custom-footer">Custom footer here</p>
</footer>
</body>
</html>

index.html 文件

<p layout:decorator="layout" layout:fragment="content">
asada
</p>

问题是,当我打开index.html时,它不包含layout.html文件中的任何内容,文件位于根目录中的其他文件旁边,因此其中没有文件夹,我是否遗漏了配置中的某些内容?谢谢

最佳答案

即使使用 Spring Boot,您也必须在依赖项中包含布局方言,如下所示:

<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>

如果您使用 Spring Security,还可以使用以下依赖项:

<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>

关于java - Spring 和 thymeleaf 布局方言不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28100986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com