简体   繁体   English

使用Spring Boot的第一个应用程序无法正常工作

[英]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/ ). 我从官方网站( 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: 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) created first page: src\\main\\resources\\templates\\home.html 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) created second page: src\\main\\resources\\templates\\hello.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) created configuration: 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");
}

}

That all. 就这样 I tried to run this application but I see error-404 for all paths (/, /home, /hello) 我尝试运行此应用程序,但所有路径(/,/ home,/ hello)都显示错误404

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. 检查pom.xml依赖项,尝试添加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). 另外,您可以尝试在不添加maven的情况下重新创建您的项目,而只是在项目中添加了spring和其他文件(有时对我有用)。

I'm now sure, but try to add this configuration in the application.properties 我现在确定,但是尝试在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