问题描述
我正在尝试将 Spring 5 和 Thymeleaf 3 配置在一起.我正在 Eclipse 上工作,我使用全新安装"构建并使用springboot:run"运行应用程序.我已经设置了一个控制器和几个模板,但 Thymeleaf 似乎找不到它们,它在浏览器中显示模板名称(例如panda")而不是加载模板本身(panda.html").
I'm trying to configure Spring 5 and Thymeleaf 3 together. I'm working on Eclipse, I build with 'clean install' and run the app with 'springboot:run'.I've setup a controller and a couple of templates but it seems that Thymeleaf cannot find them, in the browser it shows the template name (es. 'panda') instead of loading the template itself ('panda.html').
这是文件夹结构:
在我的控制器中,我有以下映射
In my controller I have the following mapping
@RestController
@Controller
public class MyController {
@Autowired
UtentiRepository utentiRepository;
@GetMapping("/gab")
public String panda(Model model) {
return "panda";
}
}
这是模板解析器的代码
@Configuration
@EnableWebMvc
@ComponentScan("it.uniroma3.spring")
public class MvcWebConfig implements WebMvcConfigurer {
@Autowired
private ApplicationContext applicationContext;
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(applicationContext);
templateResolver.setPrefix("classpath:/templates/");
templateResolver.setSuffix(".html");
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
registry.viewResolver(resolver);
}
}
这是 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>it.uniroma3.progettosiw</groupId>
<artifactId>progetto-siw</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
推荐答案
@RestController
隐式地将 @ResponseBody
放在您的方法上,这在您的情况下会导致您返回 String
直接传递到 HttpResponse 对象中(而不是被解释为视图名称).
@RestController
implicitly puts @ResponseBody
on your methods which in your case causes your returned String
to be passed directly into the HttpResponse object (instead of being interpreted as a view name).
所以只需删除 MyController 类中的 @RestController
.
So just remove @RestController
in MyController class.
这篇关于Thymeleaf 3 Spring 5 映射加载字符串而不是 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!