Каковы обычные места для JSP, JavaScript, CSS, изображений в веб-проектах Maven?
Мне нужно преобразовать традиционное веб-приложение J2EE в новый веб-проект Maven. В традиционных проектах 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
& hellip; etc и скопировать архивы от старого проекта? Или существует какая-либо стандартная структура?
Ответы
Ответ 1
Взгляните на эту статью об использовании плагина 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
Ответ 2
В проекте Maven, во-первых, вы должны добавить
<mvc:resources mapping="/resources/**" location="/resources/" />
или
<resources mapping="/resources/**" location="/resources/" />
в ваш файл servlet-config.xml(это spring -servlet.xml в моем проекте).
После этого создайте папку "resources", если она не существует в src/main/webapp.
Поместите свою папку CSS, содержащую файлы CSS, папку с изображениями, содержащую файлы изображений в ресурсах папок.
Теперь вы можете получить доступ к любому файлу из папки ресурсов из 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
... и т.д.