spring boot整合thymeleaf接口返回接口返回html数据浏览器不显示
spring boot整合thymeleaf未返回页面。
·
spring boot项目整合thymeleaf:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
配置文件释放静态资源
#释放静态资源
spring.mvc.static-path-pattern=/static/**
接口返回由thymeleaf引擎构建的页面
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping(value = "/")
String loginMEthod(){
return "index.html";
}
@PostMapping(value = "/login")
String loginIdentify(@RequestParam("user") String user,@RequestParam("password") String password){
User user1 = new User();
user1.setUserName(user);
user1.setPassWord(password);
if (userService.selectUser(user1)){
return "home.html";
}else {
return "index.html";
}
}
如上图所示接口返回html的文件,但浏览器却不显示。造成这个结果的原因是:Thymeleaf模版默认会使用templates作为视图文件夹,默认以/templates作为前缀,以.html作为后缀。
而我在写接口时由写了一遍html,导致错误。从而返回了名为xxx.html的文件的内容。也可以通过下面的配置更改:
在这个配置后不会还是没有返回页面,但排除为找到路径和无后缀的问题,最后跳转到其他页面是控制器的功能项,需要在控制器更改,这是控制的请求与转发的使用。
spring mvc请求与转发https://blog.csdn.net/xwh3165037789/article/details/123254513
修改控制器,将请求后转发到相应连接,也可以直接通过下面的方法直接返回视图:
@GetMapping(value = "/login")
public ModelAndView method4(){
ModelAndView view =new ModelAndView();
view.setViewName("home");
return view;
}
Error resolving template [login], template might not exist or might not be accessible by any of the configured Template Resolvers
该错误的意思是view.setViewName("home")
视图解析器的名称没有找到。
更多推荐
已为社区贡献3条内容
所有评论(0)