解析模板“/index.html”时出错,模板可能不存在或可能无法被任何配置的模板解析器访问

Dre*_*208 0 spring-boot

我正在尝试使用 spring-boot 解析一个简单的 index.html 文件。如果我输入 localhost:8080/index.html 它会解决,否则我会得到白色错误页面。

我把它贴在 GIT 上;这是一个非常简单的应用程序:Git Hub Link

问题似乎是模型:

在此处输入图片说明

表格型号:

@Entity
@Table(name="applicant")
public class FormModel {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String name;
    @Email
    private String email;
    @Size(min=2, max=200)
    private String description;

    public String getName() {
        return name;
    }

    @Required
    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    @Required
    public void setEmail(String email) {
        this.email = email;
    }

    public String getDescription() {
        return description;
    }

    @Required
    public void setDescription(String description) {
        this.description = description;
    }
}
Run Code Online (Sandbox Code Playgroud)

家庭控制器:

@RestController
public class Home {

    @RequestMapping(value = "/", method = {RequestMethod.GET, RequestMethod.POST} )
    public ModelAndView index(){

        ModelAndView modelAndView = new ModelAndView("/index.html");

        return modelAndView;
    }

}
Run Code Online (Sandbox Code Playgroud)

var*_*ren 6

查看您的源代码后

你的标题问题的答案是:

  1. 您使用 Thymeleaf,它可以解析模板,index但不能解析/index.html. Thymeleaf 的 TemplateResolver 负责加载 HTML 标记。默认情况下,它将模板名称转换index为资源名称,例如classpath:/templates/index.htmlWEB-INF/templates/index.html

    这样的事情应该工作:

    ModelAndView modelAndView = new ModelAndView("index");

但这不会为您解决太多问题,因为您还有其他几件事要处理

  1. 该图像显示您@用于对象,但您可能应该使用$,查看文档

  2. Thymeleaf 实际上使用 xml 格式,所以你不能使用你的 html 并在其中添加一些 thymeleaf 形式。您需要为 each 添加结束标签<hr/><br/> 依此类推。

  3. 你使用 html 链接,../static/js/some.js但你应该使用/js/some.js更多信息可以谷歌 spring 如何解析静态文件

这是带有一些修复程序的 repo:https : //github.com/varren/Portfolio