What are the conventional locations for for JSPs, JavaScript, CSS, Images in Maven web projects?

删除回忆录丶 提交于 2019-12-18 03:29:25

问题


I need to convert a traditional J2EE web application into a new Maven web project. In traditional project JSPs are under WebApp/jsps folder, JavaScript & CSS files under WebApp/scripts folder, image under WebApp/images folder, .properties files under WebApp/resources folder.

In the Maven project where would each of those file types go? Should I create folders under src/main/webapp such as: src/main/webapp/jsps, src/main/webapp/images, src/main/webapp/resources … etc and copy the files from old project? Or is there any standard structure to follow?


回答1:


Take a look at this article on the usage of the maven war plugin. It has a simple project structure.

Quoting from above link,

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



回答2:


In a Maven project, firstly, you must add

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

or

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

to your servlet-config.xml file (it is spring-servlet.xml in my project).

After that, construct a folder "resources" if it doesnt't exist under src/main/webapp. Put your CSS folder that contains CSS files, images folder that contains image files under the folder resources.

Now you can access any file under resources folder from a JSP file as:

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

or

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

My spring-servlet.xml file:

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

Project skeleton:

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

...etc



来源:https://stackoverflow.com/questions/5542382/what-are-the-conventional-locations-for-for-jsps-javascript-css-images-in-mav

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!