我的 Spring Boot 项目没有从 src/main/resources/templates/login.html 加载 View 。但是它确实从 src/main/resources/static/index.html 加载 View 。
这是我的项目结构:
我添加了简单的 <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/