在Maven web项目中,jsp、JavaScript、CSS和图像的常规位置是什么?



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

在Maven项目中,这些文件类型应该放在哪里?我是否应该在src/main/webapp下创建文件夹,如:src/main/webapp/jsps, src/main/webapp/images, src/main/webapp/resources & help;等等,并从旧项目复制文件?或者有什么标准结构可以遵循?

看看这篇关于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

在Maven项目中,首先,您必须添加

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

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

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

之后,如果src/main/webapp下不存在"resources"文件夹,创建一个"resources"文件夹。把包含CSS文件的CSS文件夹,包含图像文件的images文件夹放在resources文件夹下。

现在您可以从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

最新更新