【问题标题】:What are the conventional locations for for JSPs, JavaScript, CSS, Images in Maven web projects?Maven Web 项目中 JSP、JavaScript、CSS、图像的常规位置是什么?
【发布时间】:2011-07-29 09:09:16
【问题描述】:

我需要将一个传统的 J2EE Web 应用程序转换为一个新的 Maven Web 项目。在传统项目中,JSP 在WebApp/jsps 文件夹下,JavaScript & CSS 文件在WebApp/scripts 文件夹下,图像在WebApp/images 文件夹下,.properties 文件在WebApp/resources 文件夹下。

在 Maven 项目中,这些文件类型中的每一个会去哪里?我应该在src/main/webapp 下创建文件夹,例如:src/main/webapp/jspssrc/main/webapp/imagessrc/main/webapp/resources 等,然后从旧项目中复制文件吗?或者有什么标准的结构可以遵循?

【问题讨论】:

    标签: maven jakarta-ee conventions


    【解决方案1】:

    看看this article maven war 插件的使用。它有一个简单的项目结构。

    引用以上链接,

     |-- pom.xml  
     `-- src
         `-- main
             |-- java
             |   `-- com
             |       `-- example
             |           `-- projects
             |               `-- SampleAction.java
             |-- resources
             |   `-- images
             |       `-- sampleimage.jpg
             |   `-- js
             |       `-- scripts.js
             |   `-- css
             |       `-- styles.css
             `-- webapp
                 |-- WEB-INF
                 |   `-- web.xml
                 |-- index.jsp
                 `-- jsp
                     `-- websource.jsp
    

    【讨论】:

    • 这不包括任何 .js 或 .css 文件?
    • 如何在 JSP 中添加指向 src/main/resources 中的 JS、CSS、图像的链接?
    • 好像maven页面没有上面的结构了。
    【解决方案2】:

    在一个Maven项目中,首先,你必须添加

    <mvc:resources mapping="/resources/**" location="/resources/" />
    

    <resources mapping="/resources/**" location="/resources/" />
    

    到您的 servlet-config.xml 文件(在我的项目中是 spring-servlet.xml)。

    之后,如果 src/main/webapp 下不存在“resources”文件夹,则构建它。 将你的 CSS 文件夹包含 CSS 文件,images 文件夹包含图片文件放在资源文件夹下。

    现在您可以从 JSP 文件中访问资源文件夹下的任何文件:

    <img src="<%=request.getContextPath() %>/resources/images/image.jpg"/>
    

    <link rel="stylesheet" href="<%=request.getContextPath()%>/resources/css/style.css" />
    

    我的 spring-servlet.xml 文件:

    <?xml version="1.0" encoding="windows-1252"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
      <!-- Use @Component annotations for bean definitions -->
      <context:component-scan base-package="form"/>
    
      <!-- Use @Controller annotations for MVC controller definitions -->
      <mvc:annotation-driven />
    
      <mvc:resources mapping="/resources/**" location="/resources/" />
    
      <!-- Add JPA support -->
      <bean id="emf" class=
           "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
         <property name="loadTimeWeaver">
            <bean class=
     "org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
          </property>
      </bean>
    
      <!-- Add Transaction support -->
      <bean id="myTxManager"
         class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="emf"/>
      </bean>
    
      <!-- Use @Transaction annotations for managing transactions -->
      <tx:annotation-driven transaction-manager="myTxManager" />
    
      <!-- View resolver -->
     <bean class=
         "org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/WEB-INF/"/>
     </bean>
    
    </beans>
    

    项目骨架:

    src
    --main
      --webapp
        --resources
          --css+
          --images+
    --target
    

    ...等

    【讨论】:

      猜你喜欢
      • 2011-12-11
      • 2010-09-18
      • 2011-11-30
      • 2011-12-05
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      相关资源
      最近更新 更多