当Tomcat部署在war文件中时,为什么我的jsp文件没有显示?

时间:2019-02-08 18:08:27

标签: java spring spring-boot jsp tomcat

另一个编辑:我只是重新阅读了一个问题,所以这个问题很模棱两可:问题是Tomcat无法向我显示视图。它们位于Tomcat的webapps目录下的文件夹中。

编辑:提供赏金。代码是here

我有一个Spring Boot Web应用程序,当我导航到http://localhost:8080/swa-boot/时,它在eclipse tomcat服务器下运行良好。  当我尝试在外部将其部署为Tomcat 9.0.14下的战争时,我得到:

  

消息/swa-boot/WEB-INF/views/home.jsp

     

描述原始服务器找不到目标资源的当前表示,或者不愿意透露该资源的存在。

home.jspviews文件夹中。

我尝试添加资源到pom:

        <directory>${basedir}/src/main/webapp</directory>
        <includes>
            <include>**/**</include>
        </includes>

和依赖项

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

我还尝试过删除“提供的”范围。

我在application.properties中:

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

我已将jsp页面中的所有<a标签更改为以下形式: <a href=" <spring:url value="/mappedpath" /> ">TakeMeToTheController</a>

我已将views文件夹添加到Deployment Assembly。

tomcat Web应用程序下的文件夹如下所示: enter image description here

在tomcat服务器Web应用程序目录下: enter image description here

有什么想法吗?

编辑:部署程序集: enter image description here

3 个答案:

答案 0 :(得分:0)

看起来像生成的战争不会拾取src / main / webapp文件夹中的内容。

更改行号在您的pom.xml中如下所示72 <warSourceDirectory>src/main/webapp</warSourceDirectory>

这样,所产生的战争将具有src / main / webapp的内容。测试新的战争,看看吧。检查this问题,了解如何正确指定warSourceDirectory。

修改 此外,您需要从web.xml文件夹中删除src/main/webapp/WEB-INF文件。删除它或将其移到src文件夹之外。

接下来运行mvn clean install并将target/swa-boot.war部署到tomcat。

工作副本在这里-https://storage.googleapis.com/schoon/swa-boot.zip

答案 1 :(得分:0)

在我看来您的配置不正确:

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

引用的文件servlet-context.xml-在WEB-INF下不存在,但在src/main/resources下不存在,这意味着在运行时它将位于类路径的根目录下,因此应按以下方式进行引用:

<param-value>classpath:/servlet-context.xml</param-value>

您还可以从POM中删除此文件,并完全删除WebContent文件夹,因为作为Maven项目的Web应用程序的根目录现在是Webapp,而不是WebContent。

<warSourceDirectory>WebContent</warSourceDirectory> 

答案 2 :(得分:0)

您必须进行一些更改。

第一个更改是删除资源配置。这会将/ src / main / webapp下的所有内容打包到具有相同源结构的WEB-INF / classs中。

<resources>
   <resource>
    <directory>${basedir}/src/main/webapp</directory>
       <includes>
          <include>**/**</include>
       </includes>
   </resource>
   <resource>
    <directory>src/main/webapp</directory>
   </resource>
   <resource>
    <directory>${basedir}/src/main/resources</directory>
      <includes>
         <include>**/**</include>
      </includes>
   </resource>
</resources>

下一个更改是删除       <warSourceDirectory>WebContent</warSourceDirectory>来自战争插件。这将阻止处理Web资源。

<plugin>
    <artifactId>maven-war-plugin</artifactId>
</plugin>

下一步是运行maven clean install,您应该在战争中结束以下结构,spring才能正确找到jsps。

WEB-INF
   classes
      org
      META-INF
      static
      templates
   lib
   spring
   tags
   views
resources

更新-终于可以签出您的项目了。要将Spring Boot应用程序作为传统的战役在容器内运行,您需要进行一些更改。

更改Web.xml以正确定位应用程序servlet并删除SpringBoot ServletIntializer并删除导入资源注释。

这就是所有更改。

在github中可用的更改-https://github.com/saagar2000/springboot-traditional

enter image description here

enter image description here