静态内容没有加载spring boot

时间:2017-02-28 08:54:06

标签: spring spring-mvc spring-boot

我正在解决这个问题。我有一个简单的控制器类,能够呈现一个简单的html页面以及Spring启动初始化器类。

我已将HTML页面放在src / main / resources目录下的静态文件夹中。 但我无法获得HTML页面。相反,我得到404错误。

以下是我的项目结构

Project structure

下面是我的Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

下面是控制器和Spring Initializer类

DemoApplication.java:

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

SimpleController.java:

package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class SimpleController {

@RequestMapping("/")
public String home(){
    System.out.println("Hello Home...");
    return "home";
}
}

home.html的:

<!DOCTYPE html>
<html>
<head> 
<meta charset="ISO-8859-1">
<title>Hello</title>
</head>
<body>
 Helloooo......
 </body>
</html>

控制台登录运行Spring Boot:

:: Spring Boot ::(v1.5.1.RELEASE)

2017-02-28 10:46:26.676 INFO 8240 --- [main] com.example.DemoApplication:在带有PID 8240的abc上启动DemoApplication(C:\ SpringBootEx \ demo \ target \ classes由C中的abc启动:\ SpringBootEx \演示) 2017-02-28 10:46:26.681 INFO 8240 --- [main] com.example.DemoApplication:没有活动的配置文件集,回退到默认配置文件:默认 2017-02-28 10:46:26.771 INFO 8240 --- [main] ationConfigEmbeddedWebApplicationContext:刷新org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@70b0b186:启动日期[Tue Feb 28 10:46:26 MST 2017];上下文层次结构的根 2017-02-28 10:46:28.281 INFO 8240 --- [main] trationDelegate $ BeanPostProcessorChecker:Bean&#39; org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration&#39;类型[class org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration]类型不适合所有BeanPostProcessors处理(例如:不符合自动代理条件) 2017-02-28 10:46:28.514 INFO 8240 --- [main] trationDelegate $ BeanPostProcessorChecker:Bean&#39;验证器&#39;类型[class org.springframework.validation.beanvalidation.LocalValidatorFactoryBean]类型不适合所有BeanPostProcessors处理(例如:不符合自动代理条件) 2017-02-28 10:46:28.949 INFO 8240 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer:使用端口初始化的Tomcat:8080(http) 2017-02-28 10:46:28.973 INFO 8240 --- [main] o.apache.catalina.core.StandardService:启动服务Tomcat 2017-02-28 10:46:28.974 INFO 8240 --- [main] org.apache.catalina.core.StandardEngine:启动Servlet引擎:Apache Tomcat / 8.5.11 2017-02-28 10:46:29.194 INFO 8240 --- [ost-startStop-1] o.a.c.c.C. [Tomcat]。[localhost]。[/]:初始化Spring嵌入式WebApplicationContext 2017-02-28 10:46:29.194 INFO 8240 --- [ost-startStop-1] o.s.web.context.ContextLoader:Root WebApplicationContext:初始化在2428 ms完成 2017-02-28 10:46:29.448 INFO 8240 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean:映射servlet:&#39; dispatcherServlet&#39;至 [/] 2017-02-28 10:46:29.455 INFO 8240 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean:映射过滤器:&#39; characterEncodingFilter&#39;至:[/ ] 2017-02-28 10:46:29.457 INFO 8240 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean:映射过滤器:&#39; hiddenHttpMethodFilter&#39;致:[/ ] 2017-02-28 10:46:29.458 INFO 8240 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean:映射过滤器:&#39; httpPutFormContentFilter&#39;至:[/ ] 2017-02-28 10:46:29.458 INFO 8240 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean:映射过滤器:&#39; requestContextFilter&#39;致:[/ ] 2017-02-28 10:46:29.967 INFO 8240 --- [main] swsmmaRequestMappingHandlerAdapter:寻找@ControllerAdvice:org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@70b0b186:启动日期[2月28日星期二10:46: 26 MST 2017];上下文层次结构的根 2017-02-28 10:46:30.071 INFO 8240 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping:Mapped&#34; {[/]}&#34;到public java.lang.String com.example.controller.SimpleController.home() 2017-02-28 10:46:30.080 INFO 8240 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping:Mapped&#34; {[/ error]}&#34; on public org.springframework.http.ResponseEntity&gt; org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2017-02-28 10:46:30.081 INFO 8240 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping:Mapped&#34; {[/ error],produce = [text / html]}&#34; on public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2017-02-28 10:46:30.145 INFO 8240 --- [main] oswshandler.SimpleUrlHandlerMapping:映射的URL路径[/ webjars / ]到[class org.springframework.web.servlet.resource类型的处理程序。 ResourceHttpRequestHandler] 2017-02-28 10:46:30.145 INFO 8240 --- [main] oswshandler.SimpleUrlHandlerMapping:映射的URL路径[/ ]到类型为[class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]的处理程序] 2017-02-28 10:46:30.194 INFO 8240 --- [main] oswshandler.SimpleUrlHandlerMapping:将URL路径[/**/favicon.ico]映射到[class org.springframework.web.servlet.resource]类型的处理程序.ResourceHttpRequestHandler] 2017-02-28 10:46:30.478 INFO 8240 --- [main] o.s.j.e.a.AnnotationMBeanExporter:在启​​动时为JMX曝光注册bean 2017-02-28 10:46:30.582 INFO 8240 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer:Tomcat在端口上启动:8080(http) 2017-02-28 10:46:30.590 INFO 8240 --- [main] com.example.DemoApplication:在4.391秒内启动DemoApplication(运行5.147的JVM) 2017-02-28 10:49:04.176 INFO 8240 --- [nio-8080-exec-1] o.a.c.c.C. [Tomcat]。[localhost]。[/]:初始化Spring FrameworkServlet&#39; dispatcherServlet&#39; 2017-02-28 10:49:04.176 INFO 8240 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet:FrameworkServlet&#39; dispatcherServlet&#39 ;:初始化开始 2017-02-28 10:49:04.205 INFO 8240 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet:FrameworkServlet&#39; dispatcherServlet&#39;:初始化完成时间为29毫秒 你好家...

有人可以帮助我渲染视图吗?感谢..

2 个答案:

答案 0 :(得分:1)

您可以将home.html放入以下位置之一:

  

的src /主/资源/ META-INF /资源/ home.html的

     

的src /主/资源/资源/ home.html的

     

的src /主/资源/静态/ home.html的

     

的src /主/资源/公共/ home.html的

并制作

@RequestMapping("/")
public String home() {
    System.out.println("Hello Home...");
    return "home.html";
}

答案 1 :(得分:0)

Add the below tags inside <build></build>

 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
 <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
        </resources>`
    </build>