从Spring Boot提供静态HTML的解决方案有很多。但是,这里有一个简单的html试图打包为WAR并使用嵌入式tomcat对其进行测试。但是无法提供静态html。我收到404错误。
这是正在查看的日志:
2018-11-28 07:30:34.291 DEBUG 6972 --- [nio-8085-exec-4] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*])
2018-11-28 07:30:34.292 DEBUG 6972 --- [nio-8085-exec-4] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.web.servlet.view.InternalResourceView: name 'testdata1'; URL [testdata1]] based on requested media type 'text/html'
2018-11-28 07:30:34.297 DEBUG 6972 --- [nio-8085-exec-4] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'testdata1'; URL [testdata1]] in DispatcherServlet with name 'dispatcherServlet'
2018-11-28 07:30:34.297 TRACE 6972 --- [nio-8085-exec-4] o.s.w.servlet.view.InternalResourceView : Rendering view with name 'testdata1' with model {} and static attributes {}
2018-11-28 07:30:34.298 DEBUG 6972 --- [nio-8085-exec-4] o.s.w.servlet.view.InternalResourceView : Forwarding to resource [testdata1] in InternalResourceView 'testdata1'
2018-11-28 07:30:34.298 TRACE 6972 --- [nio-8085-exec-4] o.s.web.servlet.DispatcherServlet : Bound request context to thread: org.apache.catalina.core.ApplicationHttpRequest@1fd05c8
2018-11-28 07:30:34.298 DEBUG 6972 --- [nio-8085-exec-4] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/main/testdata1]
2018-11-28 07:30:34.298 TRACE 6972 --- [nio-8085-exec-4] o.s.web.servlet.DispatcherServlet : Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@1c0cccc] in DispatcherServlet with name 'dispatcherServlet'
2018-11-28 07:30:34.298 TRACE 6972 --- [nio-8085-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : No handler mapping found for [/main/testdata1]
2018-11-28 07:30:34.298 TRACE 6972 --- [nio-8085-exec-4] o.s.web.servlet.DispatcherServlet : Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@154b7ce] in DispatcherServlet with name 'dispatcherServlet'
2018-11-28 07:30:34.298 DEBUG 6972 --- [nio-8085-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /main/testdata1
2018-11-28 07:30:34.298 DEBUG 6972 --- [nio-8085-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/main/testdata1]
2018-11-28 07:30:34.298 TRACE 6972 --- [nio-8085-exec-4] o.s.web.servlet.DispatcherServlet : Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@cb639d] in DispatcherServlet with name 'dispatcherServlet'
2018-11-28 07:30:34.298 TRACE 6972 --- [nio-8085-exec-4] o.s.w.s.h.BeanNameUrlHandlerMapping : No handler mapping found for [/main/testdata1]
2018-11-28 07:30:34.298 TRACE 6972 --- [nio-8085-exec-4] o.s.web.servlet.DispatcherServlet : Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@121a2f5] in DispatcherServlet with name 'dispatcherServlet'
2018-11-28 07:30:34.298 DEBUG 6972 --- [nio-8085-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/main/testdata1] are [/**]
2018-11-28 07:30:34.298 DEBUG 6972 --- [nio-8085-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/main/testdata1] are {}
2018-11-28 07:30:34.298 DEBUG 6972 --- [nio-8085-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/main/testdata1] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@cfb656]]] and 1 interceptor
2018-11-28 07:30:34.298 TRACE 6972 --- [nio-8085-exec-4] o.s.web.servlet.DispatcherServlet : Testing handler adapter [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter@ec05ba]
2018-11-28 07:30:34.299 TRACE 6972 --- [nio-8085-exec-4] o.s.web.servlet.DispatcherServlet : Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter@b2e8e5]
2018-11-28 07:30:34.299 DEBUG 6972 --- [nio-8085-exec-4] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/main/testdata1] is: -1
2018-11-28 07:30:34.299 TRACE 6972 --- [nio-8085-exec-4] o.s.w.s.resource.PathResourceResolver : Resolving resource for request path "main/testdata1"
2018-11-28 07:30:34.299 TRACE 6972 --- [nio-8085-exec-4] o.s.w.s.resource.PathResourceResolver : Checking location: class path resource [META-INF/resources/]
2018-11-28 07:30:34.299 TRACE 6972 --- [nio-8085-exec-4] o.s.w.s.resource.PathResourceResolver : No match for location: class path resource [META-INF/resources/]
2018-11-28 07:30:34.299 TRACE 6972 --- [nio-8085-exec-4] o.s.w.s.resource.PathResourceResolver : Checking location: class path resource [resources/]
2018-11-28 07:30:34.299 TRACE 6972 --- [nio-8085-exec-4] o.s.w.s.resource.PathResourceResolver : No match for location: class path resource [resources/]
2018-11-28 07:30:34.299 TRACE 6972 --- [nio-8085-exec-4] o.s.w.s.resource.PathResourceResolver : Checking location: class path resource [static/]
2018-11-28 07:30:34.299 TRACE 6972 --- [nio-8085-exec-4] o.s.w.s.resource.PathResourceResolver : No match for location: class path resource [static/]
2018-11-28 07:30:34.299 TRACE 6972 --- [nio-8085-exec-4] o.s.w.s.resource.PathResourceResolver : Checking location: class path resource [public/]
2018-11-28 07:30:34.299 TRACE 6972 --- [nio-8085-exec-4] o.s.w.s.resource.PathResourceResolver : No match for location: class path resource [public/]
2018-11-28 07:30:34.299 TRACE 6972 --- [nio-8085-exec-4] o.s.w.s.resource.PathResourceResolver : Checking location: ServletContext resource [/]
2018-11-28 07:30:34.299 TRACE 6972 --- [nio-8085-exec-4] o.s.w.s.resource.PathResourceResolver : No match for location: ServletContext resource [/]
2018-11-28 07:30:34.299 TRACE 6972 --- [nio-8085-exec-4] o.s.w.s.r.ResourceHttpRequestHandler : No matching resource found - returning 404
2018-11-28 07:30:34.299 DEBUG 6972 --- [nio-8085-exec-4] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-11-28 07:30:34.299 TRACE 6972 --- [nio-8085-exec-4] o.s.web.servlet.DispatcherServlet : Cleared thread-bound request context: org.apache.catalina.core.ApplicationHttpRequest@1fd05c8
2018-11-28 07:30:34.299 DEBUG 6972 --- [nio-8085-exec-4] o.s.web.servlet.DispatcherServlet : Successfully completed request
当前,主类的配置如下。
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class, JdbcTemplateAutoConfiguration.class})
public class MainApp extends SpringBootServletInitializer {
public static void main(String[] args) {
final SpringApplication springApplication = new SpringApplication(
SampleWebAppWebConfigurer.class,MainApp.class);
springApplication.run(args);
}
}
webMVC配置是这样的:
@ComponentScan({"sampleprograms.controller"})
public class SampleWebAppWebConfigurer implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("classpath:\\webapp\\resources\\static\\")
.setCacheControl(CacheControl.maxAge(2, TimeUnit.HOURS).cachePublic());
}
}
用于加载html文件的控制器是
@Controller
@RequestMapping(value = {"/main"})
public class DataController {
static final Log LOG = LogFactory.getLog(DataController.class);
@GetMapping(value = {"/allList"})
public String getGroups(HttpSession session) {
LOG.error("The servlet context " + session.getServletContext().getRealPath("/testdata1"));
return "testdata1";
}
@GetMapping(value = {"", "/"})
public String getTestPath(HttpSession session) {
LOG.error("The servlet context " + session.getServletContext().getRealPath("/testdata"));
return "testdata";
}
}
正在调用该html的URL是
http://localhost:8085/main http://localhost:8085/main/allList
应用程序属性文件是
server.port=8085
#server.servlet.context-path=/api
spring.main.banner-mode=off
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
logging.level.org.springframework: [![TRACE][1]][1]
logging.level.org.hibernate: ERROR
#logging.level.root=TRACE
我认为可能还需要POM。
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>sampleprograms</groupId>
<artifactId>sample-programs</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>sampleprograms</groupId>
<artifactId>sample-webapp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>sample-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>sampleprograms</groupId>
<artifactId>sample-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>sample-webapp</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
最后,项目结构是这样的