RequestMappingHandlerMapping:找不到[/WEB-INF/jsp/home.jsp]的处理程序方法

时间:2018-04-07 07:45:10

标签: java spring spring-mvc spring-boot

可能一切都还可以,但我收到了错误。基本上我在应用程序属性文件中添加视图解析器属性,然后运行此项目并打开浏览器,浏览器显示此错误找不到文件。我不明白帮助我,并建议如何解决这个问题,我是Spring Boot中的新手。这就是我的项目细节。

错误

 2018-04-07 12:11:51.661 DEBUG 3740 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.web.servlet.view.InternalResourceView: name 'home'; URL [/WEB-INF/jsp/home.jsp]] based on requested media type 'text/html'
2018-04-07 12:11:51.661 DEBUG 3740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'home'; URL [/WEB-INF/jsp/home.jsp]] in DispatcherServlet with name 'dispatcherServlet'
2018-04-07 12:11:51.661 TRACE 3740 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView  : Rendering view with name 'home' with model {} and static attributes {}
2018-04-07 12:11:51.684 DEBUG 3740 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView  : Forwarding to resource [/WEB-INF/jsp/home.jsp] in InternalResourceView 'home'
2018-04-07 12:11:51.687 TRACE 3740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Bound request context to thread: org.apache.catalina.core.ApplicationHttpRequest@3087f725
2018-04-07 12:11:51.687 DEBUG 3740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/WEB-INF/jsp/home.jsp]
2018-04-07 12:11:51.687 TRACE 3740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@7ba6a25a] in DispatcherServlet with name 'dispatcherServlet'
2018-04-07 12:11:51.687 TRACE 3740 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : No handler mapping found for [/WEB-INF/jsp/home.jsp]
2018-04-07 12:11:51.687 TRACE 3740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@6d6680cb] in DispatcherServlet with name 'dispatcherServlet'
2018-04-07 12:11:51.687 DEBUG 3740 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /WEB-INF/jsp/home.jsp
2018-04-07 12:11:51.690 DEBUG 3740 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/WEB-INF/jsp/home.jsp]
2018-04-07 12:11:51.690 TRACE 3740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@1a14f49d] in DispatcherServlet with name 'dispatcherServlet'
2018-04-07 12:11:51.690 TRACE 3740 --- [nio-8080-exec-1] o.s.w.s.h.BeanNameUrlHandlerMapping      : No handler mapping found for [/WEB-INF/jsp/home.jsp]
2018-04-07 12:11:51.690 TRACE 3740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@7b19df79] in DispatcherServlet with name 'dispatcherServlet'
2018-04-07 12:11:51.690 DEBUG 3740 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Matching patterns for request [/WEB-INF/jsp/home.jsp] are [/**]
2018-04-07 12:11:51.691 DEBUG 3740 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for request [/WEB-INF/jsp/home.jsp] are {}
2018-04-07 12:11:51.692 DEBUG 3740 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapping [/WEB-INF/jsp/home.jsp] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@73797478]]] and 1 interceptor
2018-04-07 12:11:51.692 TRACE 3740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Testing handler adapter [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter@44abba94]
2018-04-07 12:11:51.692 TRACE 3740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter@f0e0b58]
2018-04-07 12:11:51.693 DEBUG 3740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/WEB-INF/jsp/home.jsp] is: -1
2018-04-07 12:11:51.693 TRACE 3740 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler     : Path contains "WEB-INF" or "META-INF".
2018-04-07 12:11:51.693 TRACE 3740 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler     : Ignoring invalid resource path [WEB-INF/jsp/home.jsp]
2018-04-07 12:11:51.693 TRACE 3740 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler     : No matching resource found - returning 404
2018-04-07 12:11:51.693 DEBUG 3740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-04-07 12:11:51.693 TRACE 3740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Cleared thread-bound request context: org.apache.catalina.core.ApplicationHttpRequest@3087f725

的build.gradle

    buildscript {
    ext {
        springBootVersion = '2.0.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'

group = 'com.ethxpay.book.ticket.app'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

configurations {
    providedRuntime
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.apache.tomcat.emded:tomcat-embed-japer')

    runtime('org.springframework.boot:spring-boot-devtools')
    runtime('mysql:mysql-connector-java')

    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')

    testCompile('org.springframework.boot:spring-boot-starter-test')

    compile group: 'javax.servlet', name: 'servlet-api', version: '3.0' 
    compile group: 'javax.servlet.jsp', name: 'jsp-api', version: '2.1' 
}

application.properties

    #Database Properties
spring.datasource.url=jdbc:mysql://localhost:3306/customerdb?useSSL=false
spring.datasource.username=root
spring.datasource.password=root

#View Resolver
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

#JPA/Hibernate properties
spring.jpa.show-sql=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

#Log
logging.level.org.hibernate.SQL:debug
logging.level.org.springframework: TRACE
logging.level.com: TRACE

控制器类

    @RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/")
    public ModelAndView index() {
        return new ModelAndView("home");
    }

    @PostMapping("/createEmp")
    public void createEmp(HttpServletRequest request, HttpServletResponse response) {
        Employee emp = new Employee(request.getParameter("name"), request.getParameter("tel"),
                request.getParameter("address"), Double.parseDouble(request.getParameter("sal")));

        employeeService.create(emp);
    }
}

项目-种子

enter image description here

3 个答案:

答案 0 :(得分:1)

使用Maven时,我缺少这些依赖项-

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <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>

我对Gradle几乎一无所知,您可以尝试删除javax-servlet和jsp依赖项。由于您已经添加了tomcat和Web依赖项。 另外,如果您使用的是jstl,则可以使用此依赖项-

compile('javax.servlet:jstl')

答案 1 :(得分:1)

  • 添加碧玉依赖对我有用。
  • 嵌入式tomcat需要jasper依赖关系才能在Spring Boot中呈现jsp视图 应用。

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    
  • 如果需要编写迭代,请在jsp页面中使用控制语句 在pom.xml中包含JSTL,即JSP标准标记库,如下所示:

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    

答案 2 :(得分:0)

编译组:“ org.apache.tomcat.embed”,名称:“ tomcat-embed-jasper”,版本:“ 9.0.7”

上述依赖对我有用。