1.新建项目
2.选择如下选项,然后点击next,进入下一步
3.输入相应的信息,点next,进入下一步
4.配置maven环境,设置好了的话,就不用修改
5.点击finish,等待maven下载相应的jar包(第一次需要),之后一个基本的maven web项目就搭建起来了,项目结构如下:
6.搭建一个服务器,运行项目,若运行成功,则项目初步搭建成功;
7.引入spring和springmvc的jar依赖,pom文件中添加如下:
<!--spring框架的三个基础包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springVersion}</version>
</dependency>
<!--springMVC的两个包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springVersion}</version>
</dependency>
8.创建java文件和resource文件夹并设置文件夹属性
9.配置web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--加载spring上下文-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContex.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置SpringMVC-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
10.新建spring框架的配置文件如下,其中applicationContext.xml是spring上下文的配置文件,Dispatcher-Servlet.xml是springMVC的配置文件
11.配置springmvc.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:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描包-->
<context:component-scan base-package="controller"/>
<!--使用action-->
<mvc:annotation-driven/>
<!--使用静态资源-->
<mvc:default-servlet-handler/>
<!--视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
12.编写IndexController,其中两个路由,一个返回json字符串,一个返回jsp页面
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created By Seven.wk
* Description: 前端控制器
* Created At 2018/09/08
*/
@Controller
@RequestMapping(value = "/", produces = "application/json;charset=utf-8")
public class IndexController {
@RequestMapping(value = "/a")
@ResponseBody
public String a() {
return "中文";
}
@RequestMapping("/b")
public String b() {
return "a";
}
}
访问http://localhost:8080/a,正常返回字符串;
访问http://localhost:8080/b,出现500错误:
解决方法:
在项目的依赖中,加入jstl.jar 和 standard.jar这两个依赖包,之后就可以成功运行了;
a.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>我是测试</title>
</head>
<body>
I am test<br/>
我是中文<br/>
<img src="/images/test.png" alt="测试"/>
</body>
</html>
运行效果:
项目整体目录如下:
至此,spring + SpringMVC环境搭建完毕!