【问题标题】:Spring Boot JSP春季启动 JSP
【发布时间】:2016-08-22 11:39:19
【问题描述】:

有没有什么简单的方法可以为 .jsp 视图生成 Spring Boot Mvc 项目?

我尝试以类似于 Spring Boot Rest 项目的方式创建项目,但这对我来说并不容易。我试过使用official Spring Boot Mvc jsp sample,但是当我尝试运行它时,我得到了这样的异常:

    org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [sample.jsp.SampleWebJspApplication]; nested exception is java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.boot.context.web.SpringBootServletInitializer
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:187) ~[spring-context-4.3.0.BUILD-20160427.201731-353.jar:4.3.0.BUILD-SNAPSHOT]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:321) ~[spring-context-4.3.0.BUILD-20160427.201731-353.jar:4.3.0.BUILD-SNAPSHOT]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243) ~[spring-context-4.3.0.BUILD-20160427.201731-353.jar:4.3.0.BUILD-SNAPSHOT]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273) ~[spring-context-4.3.0.BUILD-20160427.201731-353.jar:4.3.0.BUILD-SNAPSHOT]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:98) ~[spring-context-4.3.0.BUILD-20160427.201731-353.jar:4.3.0.BUILD-SNAPSHOT]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681) ~[spring-context-4.3.0.BUILD-20160427.201731-353.jar:4.3.0.BUILD-SNAPSHOT]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523) ~[spring-context-4.3.0.BUILD-20160427.201731-353.jar:4.3.0.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:765) [spring-boot-1.4.0.BUILD-20160427.203128-289.jar:1.4.0.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.4.0.BUILD-20160427.203128-289.jar:1.4.0.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.4.0.BUILD-20160427.203128-289.jar:1.4.0.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1182) [spring-boot-1.4.0.BUILD-20160427.203128-289.jar:1.4.0.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1171) [spring-boot-1.4.0.BUILD-20160427.203128-289.jar:1.4.0.BUILD-SNAPSHOT]
    at sample.jsp.SampleWebJspApplication.main(SampleWebJspApplication.java:33) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_65]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_65]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_65]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_65]
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) [idea_rt.jar:na]
Caused by: java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.boot.context.web.SpringBootServletInitializer
    at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:163) ~[spring-core-4.3.0.BUILD-20160427.201731-353.jar:4.3.0.BUILD-SNAPSHOT]
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:300) ~[spring-context-4.3.0.BUILD-20160427.201731-353.jar:4.3.0.BUILD-SNAPSHOT]
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:237) ~[spring-context-4.3.0.BUILD-20160427.201731-353.jar:4.3.0.BUILD-SNAPSHOT]
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:204) ~[spring-context-4.3.0.BUILD-20160427.201731-353.jar:4.3.0.BUILD-SNAPSHOT]
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:173) ~[spring-context-4.3.0.BUILD-20160427.201731-353.jar:4.3.0.BUILD-SNAPSHOT]
    ... 17 common frames omitted
Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContext
    at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_65]
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_65]
    at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_65]
    at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:152) ~[spring-core-4.3.0.BUILD-20160427.201731-353.jar:4.3.0.BUILD-SNAPSHOT]
    ... 21 common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContext
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_65]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_65]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_65]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_65]
    ... 25 common frames omitted

【问题讨论】:

  • 尝试查找ServletContext所在的jar并尝试导入并再次运行。
  • 可能是因为 jar 在下载过程中损坏了。
  • 去找到它然后尝试用zip或其他解压缩工具打开它并确保它完全下载。
  • 尝试检查您的本地存储库并确保它正常。但首先您需要找到它的位置。

标签: java spring jsp spring-mvc spring-boot


【解决方案1】:

此博客post 解释了您应该采取的步骤以及原因。它相当简单,我自己修好了。

TD:LR

添加 Maven 依赖项

为了启用 JSP 支持,我们需要在 pom.xml 中为我们的新项目添加一些依赖项。

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

将 JSP 模板放在 src/main/resources/META-INF/resources/WEB-INF/jsp/ 下

【讨论】:

    【解决方案2】:

    你有没有机会在 IntelliJ 中运行?我也注意到了这个问题,解决方案是从 spring-boot-starter-tomcat 依赖项中删除 &lt;scope&gt;provided&lt;/scope&gt;,如下所示:

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

    IntelliJ 中有一个错误,导致它在本地运行时没有将提供的依赖项添加到类路径中。

    编辑:当您要在 servlet 容器中运行提供的范围时,删除提供的范围可能会导致问题,因此请注意这一点,有关如何修复的说明可以在 Run Spring-boot's main using IDE 中找到。

    【讨论】:

      【解决方案3】:

      我也遇到过类似的问题,见github链接。 jsf-spring-boot-2.1.6 父级使用spring boot 1.4.0 一切正常,没有问题,信不信由你!

      https://github.com/joinfaces/joinfaces-example-war

      【讨论】:

        【解决方案4】:

        据我所知,spring boot默认页面技术是thymeleaf,官方推荐我们使用。 如果你想使用jsp,你可以做一些配置。

        以下步骤可能有用。

        1. Creating  src/main/webapp/WEB-INF/jsp folder.
        2. Modify application.properties file.
        
        spring.mvc.view.prefix: /WEB-INF/jsp/
        spring.mvc.view.suffix: .jsp
        

        3.添加对jsp的jar支持。您可以将以下 maven 存储库添加到您的项目 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>
        

        希望对您有所帮助。有任何问题,请随时告诉我。

        【讨论】:

        • 我不想再学习另一种模板语言!而且打字更多!如果他们有HAML,我完全愿意!
        【解决方案5】:

        将以下依赖项添加到您的 POM 文件中:

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

        创建下面的结构 /src/main/resources/META-INF/resources

        现在上面的文件夹相当于你的 WebContent 文件夹。在这个里面你可以放你的静态内容。在此处添加 WEB-INF 文件夹,现在您可以像放置任何其他 Web 项目一样放置 JSP 文件。

        在您的 application.properties 文件中添加以下内容:

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

        【讨论】:

          【解决方案6】:

          在下面的POM文件中添加依赖

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

          完整代码可在 github 链接点击here

          下载代码并使用退出的maven项目导入eclipse/STS

          下一步运行项目。 转到 com.chandrakumar.test.demo 上的包。

          它有“DemoApplication.java”并右击鼠标并选择run as 选择 java 应用程序。

          浏览器地址栏类型"http://localhost:8080/"

          【讨论】:

            【解决方案7】:

            在 johnny 的回复之后,我实际上并不知道在将应用程序部署为 JAR 时可以运行 JSP 文件。虽然显然有一些我没有听说过的扩展... 通常,JSP 应用程序被部署为 WAR,所有与 JSP 相关的文件都位于 webapp/WEB-INF/ 文件夹中。如果您愿意将应用程序打包为 WAR,则可能需要将 &lt;packaging&gt;war&lt;/packaging&gt; 添加到您的 POM。如果您的应用程序不是微服务或不使用基于云的技术,这听起来不错。但是大多数基于云的技术(不包括 cloud-foundry 和其他一些技术)只支持 JAR 打包。在这种情况下,如果您决定采用 JAR 路线,您可能希望使用替代模板,例如 Thymeleaf 或 FreeMarker,而不是 JSP。

            【讨论】:

              【解决方案8】:

              使用 maven 运行配置,将 spring-boot:run 作为“命令行”值。

              【讨论】:

                猜你喜欢
                • 2018-07-04
                • 1970-01-01
                • 2017-09-11
                • 2015-04-18
                • 2017-06-24
                • 2015-08-22
                • 2015-09-18
                • 2015-03-20
                • 2018-01-24
                相关资源
                最近更新 更多