代码之家  ›  专栏  ›  技术社区  ›  schoon

为什么我的jsp文件在Tomcat部署到war文件时不显示?

  •  4
  • schoon  · 技术社区  · 7 年前

    编辑:赏金可用。代码是 here

    http://localhost:8080/swa-boot/ .

    说明源服务器找不到目标资源的当前表示形式,或者不愿意透露存在该表示形式。

    home.jsp 在 views 文件夹

    我已尝试将以下资源添加到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>
    

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

    我已经改变了一切 <a jsp页面中的标记的格式如下: <a href=" <spring:url value="/mappedpath" /> ">TakeMeToTheController</a> .

    我已将视图文件夹添加到部署程序集中。

    tomcat web apps下的文件夹如下所示: enter image description here

    enter image description here

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

    3 回复  |  直到 7 年前
        1
  •  0
  •   Cyac    7 年前

    看起来生成的war不会拾取您的src/main/webapp文件夹的内容。

    将pom.xml中的第72行更改如下 <warSourceDirectory>src/main/webapp</warSourceDirectory>

    this 有关如何正确指定warSourceDirectory的问题。

    另外,您需要删除 web.xml 归档自 src/main/webapp/WEB-INF

    mvn clean install 部署 target/swa-boot.war 给雄猫。

    https://storage.googleapis.com/schoon/swa-boot.zip

        2
  •  0
  •   Alan Hay    7 年前

    <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> 
    
        3
  •  0
  •   s7vr    7 年前

    你必须做一些改变。

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

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

    下一步是运行maven clean install,您应该在war中完成以下结构,spring将能够正确定位JSP。

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

    使现代化 -终于可以签出你的项目了。要将SpringBoot应用程序作为容器中的传统war运行,您需要再做一些更改。

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

    github中提供的更改- https://github.com/saagar2000/springboot-traditional

    enter image description here

    enter image description here