source

스프링 부트 MVC 템플릿이 로드되지 않음(404를 찾을 수 없음)

lovecheck 2023. 3. 13. 20:34
반응형

스프링 부트 MVC 템플릿이 로드되지 않음(404를 찾을 수 없음)

매우 간단한 Spring-Boot MVC 어플리케이션이 동작하지 않습니다.404가 로드되지 않고 수신되지 않는 한 페이지의 컨트롤러를 찾을 수 없습니다.

컨트롤러 메서드에 System.out.println("Home Page") 문을 삽입하여 매핑되어 있고 올바르게 기동하고 있는 것을 확인했지만 템플릿이 로드되지 않았습니다.

오류:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Nov 06 23:43:51 EST 2014
There was an unexpected error (type=Not Found, status=404).

폴더 구조:

src/main/java
 +-Application.java
 +-WebController.java

src/main/resources
 +-templates
   +-index.html

콘솔 출력 생략:

Server initialized with port: 8080
Starting service Tomcat
Starting Servlet Engine: Apache Tomcat/7.0.55
Initializing Spring embedded WebApplicationContext
Root WebApplicationContext: initialization completed in 1947 ms
Mapping servlet: 'dispatcherServlet' to [/]
Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
Mapped "{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String app.WebController.index()
Tomcat started on port(s): 8080/http
Started Application in 5.552 seconds (JVM running for 6.366)
Home Page
Home Page
Home Page

설정:

@EnableAutoConfiguration
@Configuration
@ComponentScan
public class Application extends WebMvcConfigurerAdapter {
    public static void main(String[] args) {
         ConfigurableApplicationContext context = SpringApplication.run(Application.class);
    }
}

컨트롤러:

@Controller
public class WebController {
    @RequestMapping(value="/")
    public String index(){
        System.out.println("Home Page");
        return "index";
    }
}

index.syslog:

<!DOCTYPE html>
<html lang="en">
<body>
 <h1>Hello</h1>    
</body>
</html>

매우 간단한 Spring-Boot MVC 어플리케이션이 동작하지 않습니다.404가 로드되지 않고 수신되지 않는 한 페이지의 컨트롤러를 찾을 수 없습니다.

컨트롤러 메서드에 System.out.println("Home Page") 문을 삽입하여 매핑되어 있고 올바르게 기동하고 있는 것을 확인했지만 템플릿이 로드되지 않았습니다.

오류:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Nov 06 23:43:51 EST 2014
There was an unexpected error (type=Not Found, status=404).

폴더 구조:

src/main/java
 +-Application.java
 +-WebController.java

src/main/resources
 +-templates
   +-index.html

콘솔 출력 생략:

Server initialized with port: 8080
Starting service Tomcat
Starting Servlet Engine: Apache Tomcat/7.0.55
Initializing Spring embedded WebApplicationContext
Root WebApplicationContext: initialization completed in 1947 ms
Mapping servlet: 'dispatcherServlet' to [/]
Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
Mapped "{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String app.WebController.index()
Tomcat started on port(s): 8080/http
Started Application in 5.552 seconds (JVM running for 6.366)
Home Page
Home Page
Home Page

설정:

@EnableAutoConfiguration
@Configuration
@ComponentScan
public class Application extends WebMvcConfigurerAdapter {
    public static void main(String[] args) {
         ConfigurableApplicationContext context = SpringApplication.run(Application.class);
    }
}

컨트롤러:

@Controller
public class WebController {
    @RequestMapping(value="/")
    public String index(){
        System.out.println("Home Page");
        return "index";
    }
}

index.syslog:

<!DOCTYPE html>
<html lang="en">
<body>
 <h1>Hello</h1>    
</body>
</html>

POM:

<?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.App</groupId>
    <artifactId>App</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>App</name>
    <description>App</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.181</version>
        </dependency>
        <!--dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
        </dependency--> 
        <dependency>
            <groupId>com.firebase</groupId>
            <artifactId>firebase-client</artifactId>
            <version>1.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.10.4</version>
        </dependency>
    </dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>App.Application</start-class>
        <java.version>1.7</java.version>
    </properties>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>http://repo.spring.io/libs-release</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>http://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>   

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

</project>

수업시간에 티멜리프가 없는 것 같네요.

다음 종속성 추가:

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

그러면 모든 게 잘 될 거야

아직 문제가 해결되지 않은 경우 http://start.spring.io/에서 새로운 프로젝트를 작성할 것을 권장합니다.

이 링크를 클릭해 주세요.좋은 예시로 http://spring.io/guides/gs/handling-form-submission/를 들 수 있습니다.

src/main/java/hello/Greeting Controller.java

package hello;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class GreetingController {
    @GetMapping("/greeting")
    public String greetingForm(Model model) {
        model.addAttribute("greeting", new Greeting());
        return "greeting";
    }
    @PostMapping("/greeting")
    public String greetingSubmit(@ModelAttribute Greeting greeting) {
        return "result";
    }
}

src/main/filename/filename.filename

<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Form</h1>
    <form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
        <p>Id: <input type="text" th:field="*{id}" /></p>
        <p>Message: <input type="text" th:field="*{content}" /></p>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>
</body>
</html>

src/main/filename/filename/result.filename

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Result</h1>
    <p th:text="'id: ' + ${greeting.id}" />
    <p th:text="'content: ' + ${greeting.content}" />
    <a href="/greeting">Submit another message</a>
</body>
</html>

src/main/java/hello/Application.자바

package hello;

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

@SpringBootApplication
public class Application {

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

}

이 웹 사이트 http://start.spring.io/,에서 스프링 부트 프로젝트를 생성해 보십시오.이것은 스프링 부트로 메이븐 프로젝트를 작성하기 위해 사용되는 스프링 이니셜라이저입니다.웹 및 백태엽 종속성을 추가하고 프로젝트를 생성합니다.IDE의 프로젝트를 maven 프로젝트로 가져오거나 스프링 도구 집합을 사용합니다.

데모 어플리케이션자바:

@Controller
@SpringBootApplication
public class DemoApplication {
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
  @RequestMapping(value = "/homepage" , method = RequestMethod.GET  )
  public String sample()
  {
    return "home";
  }
}

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.4.2.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.7</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </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>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

home.module:

<!DOCTYPE html>

<html>
<head>
    <meta charset="UTF-8"></meta>
    <title>HomePage</title>
</head>
<body>
    <h1>MY Spring Boot Home Page</h1>
</body>
</html>

이건 엣지 케이스고 우발적인 사건이야하지만 그럴 수도 있어요.

에서 정적 리소스 경로를 변경하지 않았는지 확인합니다.application.properties예를 들어 다음과 같습니다.

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/META-INF/resources/webjars/

스웨거를 위해 이 부동산을 사용했는데다른 작업이 있는 UI.이 속성을 제거하면 정상입니다.

언급URL : https://stackoverflow.com/questions/26794334/spring-boot-mvc-template-not-loading-404-not-found

반응형