Spring Boot:找不到用于的处理程序方法

时间:2018-08-13 15:02:27

标签: java spring spring-boot

我正在关注有关Spring Boot的教程(试图学习它),但目前遇到了问题。我希望你们中的一个可以帮助我。

在指南中,演示者在其浏览器中打开localhost:8080 / login,然后显示.jsp文件。当我这样做时,得到以下输出和错误页面:

2018-08-13 16:48:39.696 DEBUG 18944 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/login]
2018-08-13 16:48:39.696 DEBUG 18944 --- [nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /login
2018-08-13 16:48:39.697 DEBUG 18944 --- [nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public java.lang.String com.example.demo.LoginController.sayHello()]
2018-08-13 16:48:39.697 DEBUG 18944 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/login] is: -1
2018-08-13 16:48:39.697 DEBUG 18944 --- [nio-8080-exec-5] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*])
2018-08-13 16:48:39.697 DEBUG 18944 --- [nio-8080-exec-5] o.s.w.servlet.view.BeanNameViewResolver  : No matching bean found for view name 'login'
2018-08-13 16:48:39.698 DEBUG 18944 --- [nio-8080-exec-5] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.web.servlet.view.InternalResourceView: name 'login'; URL [/WEB-INF/jsp/login.jsp]] based on requested media type 'text/html'
2018-08-13 16:48:39.698 DEBUG 18944 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'login'; URL [/WEB-INF/jsp/login.jsp]] in DispatcherServlet with name 'dispatcherServlet'
2018-08-13 16:48:39.698 DEBUG 18944 --- [nio-8080-exec-5] o.s.w.servlet.view.InternalResourceView  : Forwarding to resource [/WEB-INF/jsp/login.jsp] in InternalResourceView 'login'
2018-08-13 16:48:39.699 DEBUG 18944 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/WEB-INF/jsp/login.jsp]
2018-08-13 16:48:39.699 DEBUG 18944 --- [nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /WEB-INF/jsp/login.jsp
2018-08-13 16:48:39.699 DEBUG 18944 --- [nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/WEB-INF/jsp/login.jsp]
2018-08-13 16:48:39.699 DEBUG 18944 --- [nio-8080-exec-5] o.s.w.s.handler.SimpleUrlHandlerMapping  : Matching patterns for request [/WEB-INF/jsp/login.jsp] are [/**]
2018-08-13 16:48:39.699 DEBUG 18944 --- [nio-8080-exec-5] o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for request [/WEB-INF/jsp/login.jsp] are {}
2018-08-13 16:48:39.699 DEBUG 18944 --- [nio-8080-exec-5] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapping [/WEB-INF/jsp/login.jsp] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@3a2fdcaf]]] and 1 interceptor
2018-08-13 16:48:39.699 DEBUG 18944 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/WEB-INF/jsp/login.jsp] is: -1
2018-08-13 16:48:39.699 DEBUG 18944 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-08-13 16:48:39.699 DEBUG 18944 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Successfully completed request
2018-08-13 16:48:39.699 DEBUG 18944 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Successfully completed request
2018-08-13 16:48:39.700 DEBUG 18944 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2018-08-13 16:48:39.700 DEBUG 18944 --- [nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2018-08-13 16:48:39.701 DEBUG 18944 --- [nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]
2018-08-13 16:48:39.701 DEBUG 18944 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/error] is: -1
2018-08-13 16:48:39.708 DEBUG 18944 --- [nio-8080-exec-5] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, text/html;q=0.8] based on Accept header types and producible media types [text/html])
2018-08-13 16:48:39.708 DEBUG 18944 --- [nio-8080-exec-5] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView@1d2f722d] based on requested media type 'text/html'
2018-08-13 16:48:39.708 DEBUG 18944 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Rendering view [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView@1d2f722d] in DispatcherServlet with name 'dispatcherServlet'
2018-08-13 16:48:39.709 DEBUG 18944 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Successfully completed request
2018-08-13 16:48:39.762 DEBUG 18944 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/favicon.ico]
2018-08-13 16:48:39.762 DEBUG 18944 --- [nio-8080-exec-7] o.s.w.s.handler.SimpleUrlHandlerMapping  : Matching patterns for request [/favicon.ico] are [/**/favicon.ico]
2018-08-13 16:48:39.762 DEBUG 18944 --- [nio-8080-exec-7] o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for request [/favicon.ico] are {}
2018-08-13 16:48:39.762 DEBUG 18944 --- [nio-8080-exec-7] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapping [/favicon.ico] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], class path resource []], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@2d22a7f0]]] and 1 interceptor
2018-08-13 16:48:39.762 DEBUG 18944 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/favicon.ico] is: -1
2018-08-13 16:48:39.767 DEBUG 18944 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-08-13 16:48:39.767 DEBUG 18944 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet        : Successfully completed request

我有两个java类,.jsp文件和application.properties文件:

src.main.java.com.example.demo.DemoApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.ViewResolver;

@SpringBootApplication
public class DemoApplication {

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

src.main.java.com.example.demo.controller.LoginController.java

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {

    @RequestMapping("/login")
    public String sayHello() {
        return "login";
    }

}

src.webapp.WEB-INF.jsp.login.jsp

<html>
<head>
<title>Yahoo!!</title>
</head>
<body>
My First JSP!!!
</body>
</html>

src.main.resources.application.properties

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
logging.level.org.springframework.web: DEBUG

一切都通过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.5.15.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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </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>

如果有人可以帮助我,我将非常感激。我已经在Google上搜索了很多,并尝试了很多建议,但我不知道这些到底有什么问题。我什至只是简单地复制了课程中提供的代码,这也无济于事。

最好的问候

3 个答案:

答案 0 :(得分:0)

@JBNizet在#1.1中发布:发布的链接具有答案,这是Spring Boot和JSP文件之间的不良兼容性。通过Maven构建它并运行Jar就像一个魅力。这样问题就解决了。由于我无法将评论标记为答案,因此我会将此评论投票为正确答案。

答案 1 :(得分:0)

经过大量的搜索并打破了自己的头脑,您必须将这两个依赖项添加到pom.xml中。

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

我对您的代码执行了相同的操作,并且按预期工作。

enter image description here

答案 2 :(得分:-1)

据我所知,spring boot不喜欢WEB-INF目录。 尝试将其重命名为views并相应地更新您的配置。