在春季启动中找不到网址为http:// localhost:8080的网页

时间:2018-12-19 11:41:34

标签: spring spring-boot

我已经启动了Spring Boot应用程序,当我看到控制台时该应用程序已经启动,但是当我尝试打开浏览器运行时,它显示未找到网址的网页:http://localhost:8080/Demo/login.html 。这里有什么问题?这是我的代码

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);
    }

}

我的login.html在

下可用
src/main/resources/templates/login.html 

这是pom.xml

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <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>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

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

当Tomcat服务器从8080端口号启动时,日志清楚显示

 2018-12-19 17:02:24.213  INFO 709136 --- [           main] 
com.test.demo.DemoApplication          : Starting DemoApplication on Test with PID 709136 (C:\Users\Syed\Documents\DEMO\DEMO\target\classes started by Syed in C:\Users\Syed\Documents\DEMO\DEMO)
 2018-12-19 17:02:24.254  INFO 709136 --- [           main] 
 com.test.demo.DemoApplication          : The following profiles are active: @spring.profiles.active@
2018-12-19 17:02:25.674  INFO 709136 --- [           main] 
o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-12-19 17:02:25.705  INFO 709136 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-12-19 17:02:25.706  INFO 709136 --- [           main] 
org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.13
2018-12-19 17:02:25.715  INFO 709136 --- [           main] 
o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_45\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/bin/server;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/bin;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/lib/amd64;C:\Program Files (x86)\RSA SecurID Token Common;C:\Program Files\RSA SecurID Token Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Citrix\System32\;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\McAfee\Solidcore\Tools\GatherInfo;C:\Program Files\McAfee\Solidcore\Tools\Scanalyzer;C:\Program Files\McAfee\Solidcore\;C:\Program Files\McAfee\Solidcore\Tools\ScGetCerts;C:\Program Files\Citrix\System32\;C:\Program Files\Citrix\ICAService\;C:\Program Files\Common Files\Hitachi ID\;C:\Program Files\nodejs\;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.8.0_45\bin;C:\Program Files\Java\jdk1.8.0_45\jre\bin;C:\Users\AF68935\AppData\Roaming\npm;C:\Users\AF68935\AppData\Roaming\npm\node_modules\@angular\cli\bin;;C:\Users\AF68935\Downloads\spring-tool-suite-4-4.0.2.RELEASE-e4.9.0-win32.win32.x86_64\sts-4.0.2.RELEASE;;.]
2018-12-19 17:02:25.837  INFO 709136 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
 2018-12-19 17:02:25.837  INFO 709136 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1510 ms
2018-12-19 17:02:26.214  INFO 709136 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2018-12-19 17:02:26.523  INFO 709136 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-12-19 17:02:26.542  INFO 709136 --- [           main] com.test.demo.DemoApplication          : Started DemoApplication in 2.95 seconds (JVM running for 3.852)
2018-12-19 17:04:35.175  INFO 709136 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'

2018-12-19 17:04:35.175信息709136-[nio-8080-exec-1] o.s.web.servlet.DispatcherServlet:初始化Servlet'dispatcherServlet'     2018-12-19 17:04:35.198信息709136-[[nio-8080-exec-1] o.s.web.servlet.DispatcherServlet:在23毫秒内完成初始化

这里可能是什么问题?任何想法将不胜感激。

3 个答案:

答案 0 :(得分:1)

如果您正在学习spring,则应该阅读有关控制器的信息。尝试创建一个示例控制器,当您运行它时,请转到localhost:8080 /,您应该会看到home。

@Controller
public class HomeController
{
    @GetMapping("/")
    public String homeInit() {
        return "home";
    }
}

答案 1 :(得分:1)

如果您的静态内容位于/static/public/resources/META-INF/resources以外的目录中。

然后将属性spring.resources.static-locations添加到您的application.properties中以指向静态内容。

spring.resources.static-locations=classpath:/templates/

您将在控制台日志中看到以下消息:

Adding welcome page: class path resource [templates/index.html]

参考文档

  

默认情况下,Spring Boot从名为的目录中提供静态内容   / static(或/ public或/ resources或/ META-INF / resources)中的   类路径

     

您还可以使用以下选项来自定义静态资源位置:   spring.resources.static-locations属性(替换默认属性)   值以及目录位置列表。

答案 2 :(得分:0)

使用您提供的代码,对您的应用程序启动GET将返回404 Spring引导页面,因为您不处理相对路径'/'

您必须添加一个指示请求映射和请求方法的Controller类(带有@Controller注释)并呈现所需的任何页面