源码下载: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,出现如下界面,代表你已经搭建成功