简体   繁体   中英

the first application with spring boot is not working

I would like to try spring security. I opend the instruction from official web site ( https://spring.io/guides/gs/securing-web/ ). I did step by step first part (prepare application for spring security). Accoring to instruction this application has to work:

1) I made maven dependency:

<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) created first page: 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) created second page: 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) created configuration:

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

}

That all. I tried to run this application but I see error-404 for all paths (/, /home, /hello)

Could you explain me what I miss and why my application is not working?

Check dependencies in your pom.xml , try to add spring-context, spring-core, spring-mvc. ALso you can try recreate your project without maven just added spring and other files in your project (sometimes it's work for me).

I'm now sure, but try to add this configuration in the application.properties

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

我的带有main方法的类Application (执行应用程序)位于另一个包中,然后位于我的MvcConfig类中,这就是Spring Boot无法找到配置的原因。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM