Spring Boot 不从模板加载 View

标签 spring spring-boot

我的 Spring Boot 项目没有从 src/main/resources/templates/login.html 加载 View 。但是它确实从 src/main/resources/static/index.html 加载 View 。

这是我的项目结构:

enter image description here

我添加了简单的 <a>用于在我的 index.html

中打开我的登录页面的标记
    <div>
        <a class="btn" href="login">Login</a>
    </div>

登录 Controller :

@RequestMapping("/login")
public String showLogin() {
    System.out.println("Login method called");
    return "login";
}

此方法不会被调用。此外,我看到很多教程表明你不需要在 static 文件夹中添加你的 View ,只需在 templates 文件夹中添加你的 View ,Spring Boot 会自动选择它。

但似乎 Spring Boot 没有从 templates 文件夹中选取 View ,并显示白色标签错误页面。

我没有使用 jsp 页面,而是使用 Thymelead 模板。

application.properties

server.servlet.context-path=/hisservices

server.port=8081

welcome.message: Welcome to the HIS Services Dashboard

WelcomeController: <- 这显示了 index.html 页面

// inject via application.properties
@Value("${welcome.message:test}")
private String message = "Hello World";

@RequestMapping("/")
public String welcome(Map<String, Object> model) {
    model.put("message", this.message);
    return "welcome";
}

我是否需要为 templates 文件夹添加显式映射?

最佳答案

IMO,您需要进行包重组。

将主应用程序文件 (HisserviceJwtApplication.java) 移动到 com.skm.hisservice.securejwt 包中,并在 com.skm.hisservice 下创建新包config。 securejwt 并移动 SecurityConfig。无需更改其他包。

通过这样做,您的 LoginController 将由 spring-boot 自动扫描,方法 showLogin 也会被调用。

如果您想继续使用现有的包结构(不建议),则将 @ComponentScan('com.skm.hisservice.securejwt.controller') 添加到主应用程序文件中。

关于Spring Boot 不从模板加载 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53955276/

相关文章:

java - Spring Boot 自动配置和属性

java - kafka分区可以分布在多个kafka集群节点上吗?

java - 不使用方面跟踪 Spring 方法调用

java - Spring Security 自定义登录错误

java - 限制 Spring Webservice 并发调用

spring-boot - 我可以从 Spring Boot 2 执行器集成中删除执行器字以进行健康检查吗

java - 无法构造 `java.time.LocalDate` 的实例 - Spring boot、elasticsearch、jackson

java - Spring 启动 : Is there a similar way to hold an XML resource of strings and reference them like in Androids framework?

java - 为什么 Thymeleaf International 只能与 ResourceBundleMessageSource 配合使用

java - 将 YAML 列表映射到 Spring Boot 中的对象列表