声明:原创文章,转载请注明出处。http://www.jianshu.com/u/e02df63eaa87
1、环境清单
- Intellij idea 2017.1.1 Ultimate
- JDK 1.8.0_121
- Maven 3.5.0
- Jetty 9.2.21.v20170120
- Spring 4.3.8.RELEASE
具体代码请参照:https://github.com/hawkingfoo/webDemo
2、环境配置
分别按照上面的清单,下载并配置好IDE、JDK、Maven和Jetty。
3、Maven web项目创建
首先是创建新的项目,按照下图所示,勾选Create from archetype
并选择maven-archetype-webapp
后,点Next
继续。

填写项目信息GroupId
和ArtifactId
。一般来讲,GroupID 就像是公司、部门,ArtifactID 就像是不同的项目。

填写具体的项目名字。并点击Finish
完成项目创建。

最终项目创建完成如下图所示。

4、配置Web服务
这里我们需要配置Jetty服务。

首先进行项目配置,选择
Jetty Server Local
,并选定Jetty的安装路径和JMX模块。最后切换到Deployment
这个tab上。


选择
war:exploded
。

最后添加Jetty配置完成整体配置。
点击运行按钮,等deployed后会打开http://localhost:8080/web/

5、配置Spring MVC
5.1 添加Spring Maven依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
5.2 添加Controller类
src/main/java/controller/MainController
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "/")
public class MainController {
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String welcome() {
return "hello world main controller";
}
}
5.3 添加Spring配置文件
src/main/resources/dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="controller"/>
</beans>
5.4 配置web.xml
<web-app>
<!-- 展示名称 -->
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
5.5 最终的项目结构图

6、配置project

把java包添加到Sources
中。

7、运行

网友评论