【系统学习SpringBoot】初见Thymeleaf之SpringBoot访问项目中的 jsphtml

xiaoxiao2021-02-28  145

网上提供了许多方法

方法1

1 启动的类继承 extends SpringBootServletInitializer 2 重写configure方法 @Override protected SpringApplicationBuilder configure( SpringApplicationBuilder application) { // TODO Auto-generated method stub return application.sources(SampleSimpleApplication.class); } 3 在resources下加入 spring.view.prefix: /WEB-INF/jsp/ spring.view.suffix: .jsp


这种方法,,我试了不行啊,,少了视图解析器的(ViewResolver),没有试图解析器。

浪费了一天发现下面的东西: (1)pom.xml

<?xml version="1.0" encoding="UTF-8"?> <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>xatu.zsl</groupId> <artifactId>spring_boot_ajax</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring_boot_ajax</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <organization> <name>Pivotal Software, Inc.</name> <url>http://www.spring.io</url> </organization> <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> <!-- Compile --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <useSystemClassLoader>false</useSystemClassLoader> </configuration> </plugin> </plugins> </build> </project>

其中这个依赖比较重要

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

(2).controller类

必须使用 @Controller,,测试过,使用@RestController不好使,,原因是@RestController组合注解中包含【@Responsebody:返回内容】 (3).目录结构,,spring Boot新建时会有一个 templates

因为SpringBoot集成了Thymeleaf,所以它会默认查找resources下面的templates这个目录下的文件。


大概就是这样,,

鼠小 认证博客专家 一个萌汉子 未来的路是黑的,我不知道怎么走,我需要做的就是先走着。https://smallzheng.blog.csdn.net/
转载请注明原文地址: https://www.6miu.com/read-36925.html

最新回复(0)