Java 将SpringMVC与html结合使用
以前,我只对jsp文件使用SpringMVC。一切都很顺利。但是今天我第一次尝试使用一个html文件,立即遇到了麻烦。由于某些原因,html文件的内容从未显示在我的浏览器中。下面是配置 配置:Java 将SpringMVC与html结合使用,java,html,spring-mvc,Java,Html,Spring Mvc,以前,我只对jsp文件使用SpringMVC。一切都很顺利。但是今天我第一次尝试使用一个html文件,立即遇到了麻烦。由于某些原因,html文件的内容从未显示在我的浏览器中。下面是配置 配置: @SpringBootApplication @ComponentScan("newTestPackage") public class Config extends WebMvcConfigurerAdapter { public static void main(String[] args)
@SpringBootApplication
@ComponentScan("newTestPackage")
public class Config extends WebMvcConfigurerAdapter {
public static void main(String[] args) throws Exception {
SpringApplication.run(Config.class, args);
}
@Bean
public ViewResolver jspViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
}
控制器:
@Controller
public class IndexController {
@RequestMapping(value = "/")
public String index() {
return "index";
}
}
html:
项目结构:
浏览器显示的内容:
JSP版本运行良好。
我不明白为什么jsp页面会被找到并显示,而html one不会处于“未找到”状态。默认情况下,spring boot提供src/main/resources/static文件夹中的静态资源,所以请将index.html放在该文件夹中
您可以参考Spring boot如何为web应用程序提供静态内容。您使用Spring boot吗?如果没有,请使用弹簧护套。使用Boot,您通常会将模板放在src/main/resources/templates中。当index.html被删除到src/main/resources/templates时,结果是sameWell,我发现html可以很好地与通常的SpringMVC配置配合使用,而不涉及SpringBoot。但是html和Spring Boot的结合仍然是一个谜。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
Hello World
</body>
</html>