Eclipse 中建立 Maven Web项目
2016-02-19 21:53
555 查看
Eclipse 中建立 Maven Web项目
Eclipse 中建立 Maven Web项目软件版本
建立Archetype为web的maven项目
在Java Resources文件夹中建立标准的maven目录结构
1 去掉隐藏的missing文件夹
2 建立标准的maven项目目录结构
配置项目的 Web Deployment Assembly
测试
1 配置pomxml导入servlet-api
2 新建一个servlet类testHelloWorldjava
3 新建一个JSP页面
5 执行测试
软件版本
Eclipse Java EE IDE for Web Developers. Version: Mars.1 Release (4.5.1) Build id: 20150924-1200Maven 3.3.3
Servlet 2.5
tomcat 1.6+
jdk1.6+
本文将定Eclipse已配置好 jdk 和 tomcat
eclipse中的maven web项目包含两种配置:maven项目和web项目,所以有两种方式建立maven web项目,即
- 先建立maven项目,再转成web项目
- 先建立web项目,再转成maven项目
由于先建立web项目的方式相对比较麻烦,本文只介绍先建立maven项目,再配置的方式建立maven web项目
1. 建立Archetype为web的maven项目
1.1 打开Eclipse,Ctrl + N弹窗新建向导, 如下图
1.2 在搜索框中输入
maven project,
)
1.3 选择
Next
1.4 选择
Next
1.5 在
Fileter右边的搜索框中输入
web,并选中出现的配置,
Next
1.6 输入好信息后,点击
Next
1.7 至此,一个初始的maven web项目就建好了,目录结构如下
2. 在Java Resources文件夹中建立标准的maven目录结构
可以看到Java Resources文件夹中只有一个
src/main/resources,这并不符合maven的默认结构,所以需要在其中建立起src/main/java, src/test/java , src/test/resources 源码文件夹。
2.1 去掉隐藏的“missing”文件夹
选中项目名,右键,选择Build Path->
Configure Build Path
将带有missing的文件夹选中并
Remove掉(如果你有洁癖,也可把那个
src/main/resources也删掉,然后一块重建)
点击右下角的
OK
2.2 建立标准的maven项目目录结构
选中项目结构中的Java Resources文件夹,右键,选择
New->
Source Folder
点击
Project name右边的
Browse按钮,选择刚才建立的项目
在
Folder name中输入src/main/java,点击
Finish按钮
按照上述方法,一次建立src/test/java和src/test/resources文件夹
最后的目录结构
3. 配置项目的 Web Deployment Assembly
这是比较关键的一步!
选中项目名,右键,选择底部的
Properties
找到
Deployment Assembly,并点击
依次选中并Remove掉上图中的
src/test/java
src/test/resources
/target/m2e-wtp/web-resources三个目录,最终形成下图的结构
点击右下角的
OK
至此,一个可成功部署的、符合maven风格的maven web项目就建立起来了!
4. 测试
为了检验建立项目是否成功,需要进行简单地测试。这里通过建立一个servlet类和一个jsp页面来测试。
4.1 配置pom.xml,导入servlet-api
为了不在编译期让Eclipse报错,需要配置一下pom.xml将servlet的jar包导进来加入
servlet-api包:将下面的代码写入复制进pom的
<dependency>节点
<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency>
pom.xml的配置为
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.zdwei.web</groupId>
<artifactId>maven-web</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>maven-web Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>maven-web</finalName>
</build>
</project>
4.2 新建一个servlet类:test.HelloWorld.java
通过Eclipse的新建servlet向导在src/main/java建立
test.HelloWorld.java(先建立
test包,再建立
servlet类)
向导生成的servlet代码
package test; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class HelloWorld */ public class HelloWorld extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public HelloWorld() { // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
可以看到
doGet方法中的代码是将
"Served at: " + request.getContextPath()字符串作为相应返回到前端页面
4.3 新建一个JSP页面
将已存在的index.jsp页面删除,通过向导重新建立
index.jsp页面,修改代码如下
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> Hello World! <a href="http://localhost:8080/maven-web/HelloWorld">request.getContextPath()</a> </body> </html>
4.5 执行测试
选中项目名,右键,Run as->
Run on Server,浏览器会自动跳转到index.jsp页面,如下图
点击
request.getContextPath()超链接,结果:
测试结果符合预期!
成功建立Eclipse中的Maven Web项目!
相关文章推荐
- 关于Spring的AOP的学习随记,心情
- Java IO 装饰者模式
- Java-Java温习之Java中的重点和难点
- java 写99法则表
- Struts和SpringMVC的比较
- Chapter 2、Eclipse(19th,Feb1)
- java项目遇到的 java.security.InvalidKeyException:illegal Key Size 报错解决方案
- 巩固下java基础,接口与抽象类的区别
- java和c#程序,试试语法
- [Java 基础]数据类型
- spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)
- Eclipse的使用及Java程序的标识符和关键字
- java实现二分检索树
- Eclipse使用
- 初探12306售票算法(二)-java代码实践
- java文件操作+流的使用
- java:堆栈,队列,枚举,链表
- spring InitializingBean init-method postConstruct 执行顺序
- java排序(选择排序+插入排序+冒泡排序+递归)
- java数组(一维数组,二维数组)