Spring boot 无法解析视图页面 [英] Spring boot cannot resolve view page

查看:78
本文介绍了Spring boot 无法解析视图页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Spring Boot 运行一个简单的 Web 应用程序,但是解析视图时出现问题.当我去

@SpringBootApplication公共类演示应用{公共静态无效主(字符串 [] args){SpringApplication.run(DemoApplication.class, args);}}@控制器公共类信息控制器{@RequestMapping("/")public String getServerInfo(Map model){//这个方法没有任何问题.model.put("message", "服务器信息");返回信息";}}

解决方案

如果您将 spring boot 与某些模板引擎一起使用,例如 thymeleaf.默认情况下,您不需要设置 spring.mvc.view.prefixspring.mvc.view.suffix.

你只需要把你的模板文件放在目录src/main/resources/templates/下:

您的控制器将类似于:

@GetMapping("/temp")公共字符串临时(模型模型){model.addAttribute("attr", "attr");返回索引";}

如果您使用任何模板引擎并且只想将 HTML 页面作为静态内容提供,请将您的 HTML 文件放在目录 src/main/resources/static/:

你的控制器将变成:

@GetMapping("/test")公共字符串测试(){返回test.html";}

I am trying to run a simple web application with using Spring Boot but there is a problem with resolving view. When I go to http://localhost:8080/ it shows;

There was an unexpected error (type=Not Found, status=404).

Is there any missing property or library? Why it cannot resolve my html page?

My application.properties;

spring.mvc.view.prefix: WEB-INF/html/
spring.mvc.view.suffix: .html

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}



@Controller
public class InfoController {

    @RequestMapping("/")
    public String getServerInfo(Map<String, Object> model){

        //This method works without any problem.

        model.put("message", "server info");
        return "info";  

    }

}

解决方案

If you use spring boot with some template engine, for example, thymeleaf. You don't need to set spring.mvc.view.prefix or spring.mvc.view.suffix by default.

You just need to put your template files under the directory src/main/resources/templates/:

And your controller will be something like:

@GetMapping("/temp")
public String temp(Model model) {
    model.addAttribute("attr", "attr");
    return "index";
}

If you are NOT using any template engine and just want to serve HTML page as static content, put your HTML files under the directory src/main/resources/static/:

And your controller will become:

@GetMapping("/test")
public String test() {
    return "test.html";
}

这篇关于Spring boot 无法解析视图页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆