问题在于,浏览器没有输出 thymeleaf 模板,而是显示带有“login”一词的页面。我认为这与配置有关,但我找不到错误。可能有人知道如何解决这个问题
application.yml
spring:
autoconfigure:
exclude: org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
datasource:
url: jdbc:mysql://127.0.0.1:3306/getyourhotel
password: *password*
username: root
jpa:
database-platform: org.hibernate.dialect.MySQL8InnoDBDialect
generate-ddl: true
show-sql: true
hibernate:
ddl-auto: update
properties:
hibernate:
globally_quoted_identifiers: true
dialect: org.hibernate.dialect.MySQL8Dialect
format_sql: true
sql:
init:
mode: never
continue-on-error: false
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: HTML
cache: false
application:
title: getyourhotel
version: 1.0
来自 pom.xml 的依赖项
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<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>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
控制器
@PostMapping("/login")
public String login(@RequestBody @Valid User user, Model model) {
UserDTO userDTO = userService.findUser(user);
model.addAttribute("user", userDTO);
return "login";
}
控制器不是设置为@RestController吗?
如果是@RestController,则返回的登录字符串将作为正文返回。
在这种情况下,请将其更改为@Controller。