您的位置:首页 > 编程语言 > Java开发

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

Maven 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/javasrc/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项目!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: