Spring Boot 1.5.9版应用程序无法打开视图文件,未找到错误404 [英] Spring Boot version 1.5.9 app does not open view file, not found error 404

查看:48
本文介绍了Spring Boot 1.5.9版应用程序无法打开视图文件,未找到错误404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用1.5.9版本(最新版本)的Spring Boot中显示视图文件时遇到问题.我是从Spring Initializr创建的项目.我到处搜索有关Internet的内容.我尝试了多种方法,但是无法显示视图文件,因此我决定在这里作为最后一个选择.

I have a problem at displaying view file in Spring Boot with version 1.5.9 (the latest version). I created the project from Spring Initializr. I searched everywhere about the probelm in the internet. I tried many different ways but I could not make the view file displayed so I decided to ask here as the last option.

这是我的设置和代码:

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.eric</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-boot-starter</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.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.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

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

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

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

SpringBootStarterApplication.java

package com.eric.springbootstarter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration;
import org.springframework.context.annotation.Configuration;



@SpringBootApplication(exclude = ErrorMvcAutoConfiguration.class)
public class SpringBootStarterApplication{

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

LoginController.java

package com.eric.springbootstarter;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {

    @RequestMapping(value="/giris", method = RequestMethod.GET)
    public ModelAndView giris(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        return modelAndView;
    }

}

在控制台选项卡中,我得到以下日志:

In the console tab, I get this log:

2017-12-25 18:49:44.874  INFO 8740 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-12-25 18:49:44.892  INFO 8740 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2017-12-25 18:49:44.995  INFO 8740 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.12.Final}
2017-12-25 18:49:44.997  INFO 8740 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2017-12-25 18:49:44.998  INFO 8740 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2017-12-25 18:49:45.052  INFO 8740 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-12-25 18:49:45.223  INFO 8740 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2017-12-25 18:49:45.625  INFO 8740 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000228: Running hbm2ddl schema update
2017-12-25 18:49:45.805  INFO 8740 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-12-25 18:49:46.631  INFO 8740 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.web.context.support.GenericWebApplicationContext@32b260fa: startup date [Mon Dec 25 18:49:41 EET 2017]; root of context hierarchy
2017-12-25 18:49:46.779  INFO 8740 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/giris],methods=[GET]}" onto public org.springframework.web.servlet.ModelAndView com.eric.springbootstarter.LoginController.giris()
2017-12-25 18:49:46.834  INFO 8740 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-25 18:49:46.835  INFO 8740 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-25 18:49:46.906  INFO 8740 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-25 18:49:46.954  INFO 8740 --- [           main] oConfiguration$WelcomePageHandlerMapping : Adding welcome page: ServletContext resource [/index.html]
2017-12-25 18:49:47.514  INFO 8740 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/**']]], []
2017-12-25 18:49:47.614  INFO 8740 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: org.springframework.boot.autoconfigure.security.SpringBootWebSecurityConfiguration$ApplicationNoWebSecurityConfigurerAdapter$1@41bfa9e9, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@68b7d0ef, org.springframework.security.web.context.SecurityContextPersistenceFilter@59c500f7, org.springframework.security.web.header.HeaderWriterFilter@40ed1802, org.springframework.security.web.csrf.CsrfFilter@2b736fee, org.springframework.security.web.authentication.logout.LogoutFilter@721d5b74, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@50110971, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@73c48264, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@7069f076, org.springframework.security.web.session.SessionManagementFilter@7f42e06e, org.springframework.security.web.access.ExceptionTranslationFilter@1b7f06c7]
2017-12-25 18:49:48.040  INFO 8740 --- [           main] .y.s.s.SpringBootStarterApplicationTests : Started SpringBootStarterApplicationTests in 6.593 seconds (JVM running for 7.71)
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 6.901 sec - in com.eric.springbootstarter.SpringBootStarterApplicationTests
2017-12-25 18:49:48.223  INFO 8740 --- [       Thread-3] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@32b260fa: startup date [Mon Dec 25 18:49:41 EET 2017]; root of context hierarchy
2017-12-25 18:49:48.230  INFO 8740 --- [       Thread-3] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'

index.html 文件位于src/main/resources/templates文件夹中.我没有将其放在"webapp/WEB-INF/views"之类的文件夹中,因为它既旧又已弃用.Spring Boot可以处理我所假定的所有内容.您有什么建议我还能做什么?预先感谢.

index.html file is located in src/main/resources/templates folder. I did not put it in a folder like "webapp/WEB-INF/views" because it is old and deprecated. Spring Boot handles all I presume. You have any suggestion what else can I do? Thanks in advance.

推荐答案

我找到了无法解析视图的原因.我正在为那些可能遇到与我一样的问题的人撰写这篇文章,以便他们可以在此页面中使用.我看到互联网上没有有关该问题的信息.

I found the reason of not resolving view. I am writing this post for those who might have the same problem like me so they can utilize from this page. I see that there is no information regarding the problem in the internet.

我在pom.xml中包含了这两个模块

I included these two modules in pom.xml

<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
</dependency>

您可能更喜欢用Thymeleaf来做,因为这不是唯一的选择.

You can prefer doing it with Thymeleaf as this is not the only option.

这篇关于Spring Boot 1.5.9版应用程序无法打开视图文件,未找到错误404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆