前言:本次搭建SpringMVC项目,建立在已完成从无到有整合SpringMVC-MyBatis项目(1):搭建JavaWeb项目 的基础上,本篇的重点在于如何将SpringMVC框架引入到普通的JavaWeb项目中去,项目基于SpringMVC4.3.18版本进行搭建,所有配置亲测可用;
疑难点分析:搭建SpringMVC项目的难点在于如何建立起各种XXX.xml配置文件的关系,以及每个配置文件中的每行配置是什么作用,搞清楚了这些,针对自己不懂得点有针对性的学习,会事半功倍。先交代下本次搭建用到的工具和框架:
- 开发工具:Idea 2018.2.2
- spring框架版本:4.3.18.release
- maven版本:3.5.2
首先看看搭建完成之后的项目结构:
新增了HelloController和HelloService类,在WEN-INF下新增SpringMVC的配置文件,applicationContext.xml和SpringMVC-servlet.xml,在本次搭建中,采用父子容器的概念,即SpringMVC-servlet.xml只配置controller相关的,业务交给applicationContext.xml;
好了,开始搭建:
1、引入SpringMVC和切面依赖,最终pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dongnao</groupId>
<artifactId>SpringMVC-Sample</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>SpringMVC-Sample</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- 引入SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- 引入切面依赖 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<!-- 引入Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- 引入JSP -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!-- 引入JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2、找到web.xml,进行如下配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3、SpringMVC-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: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-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 配置自动扫描controller包下面带有@Controller注解的类 -->
<context:component-scan base-package="com.dongnao.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 开启MVC注解驱动,请求分发必须 -->
<mvc:annotation-driven />
<!-- 配置静态资源处理 -->
<mvc:resources mapping="/static/**" location="/resources/" />
<!-- 配置视图解析器 将逻辑视图名映射为物理视图名 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value="/WEB-INF/views/"></property>
<property name = "suffix" value = ".jsp"></property>
</bean>
</beans>
4、applicationContext.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 扫描业务代码 -->
<context:component-scan base-package="com.dongnao">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service"></context:include-filter>
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"></context:exclude-filter>
</context:component-scan>
<!-- 开启aop支持 -->
<aop:aspectj-autoproxy />
</beans>
5、HelloController类如下,就是一个普通的Controller,代码如下:
import com.dongnao.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/helloMvc")
public ModelAndView helloMvc(String msg) {
helloService.sayHello(msg);
ModelAndView mv = new ModelAndView();
mv.addObject("msg", "搭建SpringMVC项目成功");
mv.setViewName("helloMvc");
return mv;
}
}
6、HelloService类如下:
import org.springframework.stereotype.Service;
@Service
public class HelloService {
public void sayHello(String msg){
System.out.println(msg);
}
}
7、helloMvc.jsp代码如下:
好了,到此为止,启动项目,http://localhost:8080/mvc/helloMvc ,显示如下,搭建成功