簡體   English   中英

使用Spring Boot的第一個應用程序無法正常工作

[英]the first application with spring boot is not working

我想嘗試春季安全。 我從官方網站( https://spring.io/guides/gs/securing-web/ )打開了說明。 我已經按部就班地做了第一部分(准備春季安全性應用程序)。 根據說明,此應用程序必須有效:

1)我做了Maven的依賴:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.6.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
</dependencies>

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

2)創建第一頁:src \\ main \\ resources \\ templates \\ home.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
  <title>Spring Security Example</title>
</head>
<body>
  <h1>Welcome!</h1>

  <p>Click <a th:href="@{/hello}">here</a> to see a greeting.</p>
</body>
</html>

3)創建第二頁:src \\ main \\ resources \\ templates \\ hello.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
  xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
  <title>Hello World!</title>
</head>
<body>
   <h1>Hello world!</h1>
</body>
</html>

4)創建配置:

package hello;

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/home").setViewName("home");
    registry.addViewController("/").setViewName("home");
    registry.addViewController("/hello").setViewName("hello");
    registry.addViewController("/login").setViewName("login");
}

}

就這樣 我嘗試運行此應用程序,但所有路徑(/,/ home,/ hello)都顯示錯誤404

您能解釋一下我想念的內容以及為什么我的應用程序無法正常工作嗎?

檢查pom.xml依賴項,嘗試添加spring-context,spring-core,spring-mvc。 另外,您可以嘗試在不添加maven的情況下重新創建您的項目,而只是在項目中添加了spring和其他文件(有時對我有用)。

我現在確定,但是嘗試在application.properties中添加此配置

spring.mvc.view.prefix=templates/
spring.mvc.view.suffix=.html

我的帶有main方法的類Application (執行應用程序)位於另一個包中,然后位於我的MvcConfig類中,這就是Spring Boot無法找到配置的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM