【问题标题】:JSP file not rendering in Spring Boot running in AppEngineJSP 文件未在 AppEngine 中运行的 Spring Boot 中呈现
【发布时间】:2018-04-11 15:18:00
【问题描述】:

我有一个使用 Google App Engine 标准运行时运行的 Spring Boot 应用程序。当我尝试从控制器渲染 JSP 视图时,它没有渲染。

这是我的 Maven 依赖项,

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

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
    <version>9.0.1</version>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <scope>provided</scope>
    <version>1.2</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <version>1.5.8.RELEASE</version>
</dependency>

我的控制器类试图渲染 calendar.jsp,

@RestController
public class HelloController {

@RequestMapping("/hello")
public String hello()
{
    return "calendar";
}
}

application.properties

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

在执行命令时

mvn spring-boot:run

我遇到了以下异常

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.NoSuchMethodError: org.apache.catalina.Context.addServletMapping(Ljava/lang/String;Ljava/lang/String;)V
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at com.spring.config.Application.main(Application.java:19) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:527) [spring-boot-maven-plugin-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]

Libraries

Directory Structure

感谢您提前提供的帮助。

【问题讨论】:

    标签: java maven jsp google-app-engine spring-boot


    【解决方案1】:

    在我看来,spring boot 依赖于 catalina 作为 servlet 引擎(例如 tomcat)

    会不会是google的应用引擎不支持?

    尝试阅读这个专门与 Spring Boot 和应用引擎相关的谷歌指南https://codelabs.developers.google.com/codelabs/cloud-app-engine-springboot/index.html

    你声称这个问题是你返回一个字符串“hello”,这很奇怪,试试看这个https://hellokoding.com/spring-boot-hello-world-example-with-jsp/

    【讨论】:

    • 我根据您提到的链接创建了我的 Spring Boot 应用程序。但是,如果我删除这个“tomcat-embed-jasper”依赖项,则不会引发异常并且 JSP 不会呈现。
    • 好吧,当你说不渲染时,发生了什么?日志中有错误吗?
    • 找不到 [/hello] 的处理程序映射
    • 我解决了找不到处理程序映射,但我的jsp仍然没有呈现。日志中没有异常。在浏览器中,它只是将“日历”打印为字符串。
    • 您的文件在哪里?可以发一份你的目录结构截图吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 2019-04-04
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 2019-06-04
    相关资源
    最近更新 更多