eclipse用maven创建web项目

1 篇文章 0 订阅
1 篇文章 0 订阅

源码下载:https://github.com/cinling118/spring-web-share

1、File->New->Project,选中Maven Project,点“Next”

2、如下选择,然后点Next

3、填写Group Id、Artifact Id,然后点Finish

4、项目右键:properties,弹出如下界面,找到Project Facets,把Dynamic Web Module前面的勾去掉,然后点“Apply”

5、选择JAVA版本1.8,然后再勾选“Dynamic Web Module”,选择3.0,再点击“Further configuration avaliable...”

6、Webcontent改为:src/main/webapp,然后点OK,点Apply

点完“Apply”后出现src/main/resouces和src/test/java两个目录

7、选择main目录,右键->New-Folder,输入java后,点Finish

8、项目右键:Build Path->Coonfigure Build Path...,配置一下JRE,默认为1.5,点击Edit..,更改为1.8,如果没有配置JDK,先去配置,再来操作这一步。

9、选择webapp目录,右键:New->HTML File,输入名称:index.html,点Finish(之前的index.jsp我删除了)。

10、index.html文件body输入“

Hello,this my first spring web.

”后保存

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h2>Hello,this my first spring web.</h2>
</body>
</html>

11、打算用Jetty启动项目,所以pom.xml引入Jetty依赖包

<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>spring-web</groupId>
  <artifactId>spring-web</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>spring-web Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- 引入jetty依赖包 -->
    <dependency>
		<groupId>org.mortbay.jetty</groupId>
		<artifactId>jetty</artifactId>
		<version>6.1.26</version>
	</dependency>
  </dependencies>
  <build>
    <finalName>spring-web</finalName>
  </build>
</project>

12、选中src/test/java后,New->Package,输入src.test.java,点Finish

13、src.test.java包下,New->Class,输入JettyUtils后,点Finish

JettyUtils.java文件

package src.test.java;

import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;

public class JettyUtils {

	public static Server buildDebugServer(int port, String context) {
		Server server = new Server(port);
		WebAppContext webContext = new WebAppContext("src/main/webapp", context);
		webContext.setClassLoader(Thread.currentThread().getContextClassLoader());
		server.setHandler(webContext);
		server.setStopAtShutdown(true);
		return server;
	}

}

14、src.test.java包下,New->Class,创建启动类:Spring_web_Start.java

package src.test.java;

import org.mortbay.jetty.Server;

public class Spring_web_Start {
	public static final int PORT = 8888;
	public static final String CONTEXT = "/";
	public static final String BASE_URL = "http://localhost:"+PORT+CONTEXT;

	public static void main(String[] args) throws Exception {
		System.setProperty("B2BCENTER_HOME", "C:/supply-chain");
		System.setProperty("java.awt.headless", "true");

		Server server = JettyUtils.buildDebugServer(PORT, CONTEXT);
		server.start();
		System.out.println(BASE_URL);

		if (System.in.read() != 0) {
			server.stop();
			System.out.println("Server stopped");
		}
	}
}

15、启动:选中Spring_web_Start.java->Run As->Java Application,启动后如下图

16、浏览器输入:http://localhost:8888,出现如下界面,代表你已经搭建成功

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值