使用Eclipse构建Maven的SpringMVC项目.docx
《使用Eclipse构建Maven的SpringMVC项目.docx》由会员分享,可在线阅读,更多相关《使用Eclipse构建Maven的SpringMVC项目.docx(27页珍藏版)》请在冰豆网上搜索。
使用Eclipse构建Maven的SpringMVC项目
使用Eclipse构建Maven的SpringMVC项目
首先Eclipse需要安装Maven的插件,地址:
http:
//m2eclipse.sonatype.org/sites/m2e。
用MyEclipse安装Maven插件,建出的Maven项目有些问题。
一是,发布tomcat的时候resources总是不会被发布到tomcat下;二是,把WEB-INF下的classes改到target下的classes,但是不知道为什么MyEclipse要么仍然在WEB-INF下生成class。
要么真不在WEB-INF生成classes了但是发布tomcat的时候,class文件一个都不会给你发布过去,超级郁闷。
但是使用Eclipse构建Maven项目后,使用MyEclipse打开就没问题了。
用maven的好处:
我感觉最主要就是自动下载jar包和它所依赖的包,这样可以保证了多人开发时jar版本不同的问题。
再就是文件结构清晰,java文件,资源文件,测试文件都分的很清楚。
将介绍两种方法:
一,直接建立Maven项目方法;二、建立DynamicWebproject转成Maven项目方法。
一、直接建立Maven项目方法
1、建立Maven项目
接下来使用Eclipse的maven构建一个web项目,以构建SpringMVC项目为例:
1.1选择建立MavenProject
选择File->New->Other,在New窗口中选择Maven->MavenProject。
点击newxt。
1.2选择项目路径
UsedefaultWorkspacelocation默认工作空间。
1.3选择项目类型
在ArtifactId中选择maven-archetype-webapp
1.4输入GroupID和ArtifactID,以及Package
GroupID一般写大项目名称。
ArtifactID是子项目名称。
例如Spring的web包,GroupID:
org.springframework,artifactId:
spring-web。
Package是默认给你建一个包,不写也可以。
1.5刚建立好后的文件结构如下图
如果这里显示的内容多,一般是Filters设置的问题。
或perspective为JavaEE模式,改成Java模式就可以了。
2、配置Maven项目
接下来就需要更改好多配置了。
2.1添加Source文件夹
接下来需要添加src/main/java,src/test/java,src/test/resources三个文件夹。
右键项目根目录点击New->SourceFolder,
建出这三个文件夹。
注意不是建普通的Folder,而是SourceFolder。
2.2更改class路径
右键项目,JavaBuildPath->Source
下面应该有4个文件夹。
src/main/java,src/main/resources,src/test/java,src/test/resources。
双击每个文件夹的Outputfolder,选择路径。
src/main/java,src/main/resources,选择target/classes;
src/test/java,src/test/resources,选择target/test-classes;
选上Allowoutputfoldersforsourcefolders.
在此处还要更改:
更改文件夹显示的顺序:
点击OrderandExport。
更改JDK版本:
在Libraries双击JRESystemLibrary,要1.6版本。
2.3把项目变成DynamicWeb项目
2.3.1右键项目,选择ProjectFacets,点击Converttofacetedfrom
2.3.2配置ProjectFacets
更改DynamicWebModule的Version为2.5。
(3.0为Java7的)。
如果提示错误,可能需要在JavaCompiler设置Compilercompliancelevel为1.6。
或者需要在此窗口的Java的Version改成1.6。
2.3.3配置ModifyFacetedProject
点击Furtherconfigurationavailable…,弹出ModifyFacetedProject窗口
此处是设置web.xml文件的路径,我们输入src/main/webapp。
Generateweb.xmldeploymentdescriptor自动生成web.xml文件,可选可不选。
2.4设置部署程序集(WebDeploymentAssembly)
上面步骤设置完成后,点击OK,Properties窗口会关闭,在右键项目打开此窗口。
在左侧列表中会出现一个Deployment Assembly,点击进去后,如下图:
此处列表是,部署项目时,文件发布的路径。
1,我们删除test的两项,因为test是测试使用,并不需要部署。
2,设置将Maven的jar包发布到lib下。
Add->JavaBuildPathEntries->MavenDependencies->Finish
设置完成效果图
3、向maven项目中添加jar包
maven可以管理项目依赖的jar包,通过groupID、artifactId以及版本号可以唯一确定一个jar包。
这样可以防止老式Web项目中WEB-INF/lib下jar包不一致的问题。
并且maven还会自动下载添加进的jar包所依赖的jar包。
3.1在pom.xml中添加所需要的jar包
使用MavenPOMeditor打开项目中的pom.xml文件,选择Dependencies,在Dependencies栏目点击Add进行,首先弹出一个搜索按钮,例如输入spring-web,就会自动搜索关于spring-web相关的jar包,我们选择3.0.5版本的spring。
将spring包全部添加进来。
需要添加的其他jar包有:
junit、jstl。
或者点击pom.xml直接编辑pom.xml文件。
这样可以直接copy过来dependencies内容。
3.2设置jar包的scope
当添加进入一个jar包后,有一些属性需要设置,最重要的就是scope,它有以下几种取值:
1. compile,缺省值,适用于所有阶段,会随着项目一起发布。
2. provided,类似compile,期望JDK、容器或使用者会提供这个依赖。
如servlet.jar。
3. runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。
4. test,只在测试时使用,用于编译和运行测试代码。
不会随项目发布。
5. system,类似provided,需要显式提供包含依赖的jar,Maven不会在 Repository中查找它。
通常SpringMVC项目所需要配置scope的jar包如下图:
有的时候发现servlet-api还是被打包到lib下面了,此时肯定会报错。
就需要把maven插件中的WTP也安装一下。
Eclipse在线安装路径:
http:
//m2eclipse.sonatype.org/sites/m2e-extras。
选择forEclipseWTP。
4、构建SpringMVC框架
4.1编辑web.xml文件
需要添加log4j,字符过滤,Spring的dispatcher等。
webx.xml代码如下:
Xml代码
1.
xml version="1.0" encoding="UTF-8"?
>
2.3. xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
4. xsi:
schemaLocation="
5.
6. version="2.5" >
7.
8.
-- 区分项目名称,防止默认重名 -->
9.
10. webAppRootKey
11. maven.example.root
12.
13.
14.
-- Spring的log4j监听器 -->
15.
16. org.springframework.web.util.Log4jConfigListener
17.
18.
19.
-- 字符集 过滤器 -->
20.
21. CharacterEncodingFilter
22. org.springframework.web.filter.CharacterEncodingFilter
23.
24. encoding
25. UTF-8
26.
27.
28. forceEncoding
29. true
30.
31.
32.
33. CharacterEncodingFilter
34. /*
35.
36.
37.
-- Spring view分发器 -->
38.
39. dispatcher
40. org.springframework.web.servlet.DispatcherServlet
41.
42. contextConfigLocation
43. /WEB-INF/dispatcher-servlet.xml
44.
45. 1
46.
47.
48. dispatcher
49. *.do
50.
51.
52.
xmlversion="1.0"encoding="UTF-8"?
>
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="
version="2.5">
--区分项目名称,防止默认重名-->
webAppRootKey
maven.example.root
--Spring的log4j监听器-->
org.springframework.web.util.Log4jConfigListener
--字符集过滤器-->
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
CharacterEncodingFilter
/*
--Springview分发器-->
dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/dispatcher-servlet.xml
1
dispatcher
*.do
4.2编写Spring配置文件dispatcher-servlet.xml
如要添加MVC驱动、注解检测、视图解析等。
dispatcher-servlet.xml代码如下:
Xml代码
1.
xml version="1.0" encoding="UTF-8"?
>
2.//www.springframework.org/schema/beans"
3. xmlns:
aop="http:
//www.springframework.org/schema/aop"
4. xmlns:
context="http:
//www.springframework.org/schema/context"
5. xmlns:
mvc="http:
//www.springframework.org/schema/mvc"
6. xmlns:
tx="http:
//www.springframework.org/schema/tx"
7. xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
8. xsi:
schemaLocation="http:
//www.springframework.org/schema/aop
9. http:
//www.springframework.org/schema/aop/spring-aop-3.0.xsd
10. http:
//www.springframework.org/schema/beans
11. http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
12. http:
//www.springframework.org/schema/context
13. http:
//www.springframework.org/schema/context/spring-context-3.0.xsd
14. http:
//www.springframework.org/schema/mvc
15. http:
//www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
16. http:
//www.springframework.org/schema/tx
17. http:
//www.springframework.org/schema/tx/spring-tx-3.0.xsd">
18.
19. annotation-driven />
20. component-scan base-package="liming.maven.example" />
21.
22.
23.
24.
25.
26.
27.
xmlversion="1.0"encoding="UTF-8"?
>
//www.springframework.org/schema/beans"
xmlns:
aop="http:
//www.springframework.org/schema/aop"
xmlns:
context="http:
//www.springframework.org/schema/context"
xmlns:
mvc="http:
//www.springframework.org/schema/mvc"
xmlns:
tx="http:
//www.springframework.org/schema/tx"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="http:
//www.springframework.org/schema/aop
http:
//www.springframework.org/schema/aop/spring-aop-3.0.xsd
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/tx
http:
//www.springframewo