Intellij idea下的maven web工程

时间:2021-12-16 20:10:45

新建

新建maven项目,create from archetype,选择maven-archetype-webapp

Intellij idea下的maven web工程

Next,填写GroupId,ArtifactId和Version

Intellij idea下的maven web工程

Next,这里在Properties中添加一个参数archetypeCatalog=internal,不加这个参数,在maven生成骨架的时候将会非常慢,有时候会直接卡住。

来自网上的解释:

archetypeCatalog表示插件使用的archetype元数据,不加这个参数时默认为remote,local,即*仓库archetype元数据,由于*仓库的archetype太多了,所以导致很慢,指定internal来表示仅使用内部元数据。

Intellij idea下的maven web工程

Next,填写项目名称和module名称。

Intellij idea下的maven web工程

点击Finish。

项目的目录结构如下:

Intellij idea下的maven web工程

配置Jetty(在pom.xml中)

<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
<configuration>
<systemProperties>
<force>true</force>
<systemProperty>
<name>environment</name>
<value></value>
</systemProperty>
<systemProperty>
<name>app.key</name>
<value>d</value>
</systemProperty>
<systemProperty>
<name>app.port</name>
<value>8080</value>
</systemProperty>
</systemProperties>
<stopKey>xx</stopKey>
<stopPort>5666</stopPort>
<scanIntervalSeconds>0</scanIntervalSeconds>
<reload>manual</reload>
<!-- webAppConfig is is an alias for webApp-->
<webApp>
<contextPath>/</contextPath>
<parentLoaderPriority>true</parentLoaderPriority>
<allowDuplicateFragmentNames>true</allowDuplicateFragmentNames>
</webApp>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8080</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>

配置web时需要注意的几点

配置web.xml(servlet容器)
 <servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
  <servlet-mapping>    <servlet-name>DispatcherServlet</servlet-name>    <url-pattern>/</url-pattern>  </servlet-mapping>
系统会自动的根据servlet-name寻找该目录下的默认文件DispatcherServlet-servlet.xml,但是为了规范,还是希望在web.xml中显示指出。

配置applicationContext.xml
<context:component-scan base-package="com.d.controller"></context:component-scan>

<mvc:annotation-driven />
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>

在配置过程中遇到的问题 在Intellij Idea中进行web工程配置时,由于开始并没有建立maven工程,后来通过Intellij idea 的Add Framework Support来构建的maven,然后通过maven添加jetty插件,这样添加的插件,是不会将工程自动部署到jetty容器上的(如果是在intellij中创建maven项目 intellij会给你配置好所有的配置 )。 通过后期构建maven,对应的Project Settings的Artifacts是空的,没有任何配置的,是需要自己手动去添加的。
通过构建maven项目获取的配置是这样的(图参考http://www.cnblogs.com/jifeng/p/4658765.html)
Intellij idea下的maven web工程 总的来说其实就是没有将工程自动部署到jetty容器上 如果没有部署的话,会出现404的错误。