Spring web项目开发
一、Spring MVC 项目搭建
二、Spring MVC 集成 MyBatis
三、Spring MVC 实现CRUD功能
1. 开发环境
jdk 1.8
apache-tomcat-8.5.15
apache-maven-3.5.0
intellij idea
springframework 5.1.2.RELEASE
2. 新建项目(多模块)
项目及各模块创建截图
项目创建完成后,右键项目->New->Module,创建模块
各模块创建完成后的代码结构
进入Project Structure窗口,将spring-mybatis-sample-war 改成web项目
修改各个pom.xml中packaging
设置如下
spring-mybatis-sample pom
spring-mybatis-sample-api jar
spring-mybatis-sample-service jar
spring-mybatis-sample-service-impl jar
spring-mybatis-sample-web jar
spring-mybatis-sample-war war
添加Spring依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>
项目配置
web.xml配置
文件路径:/webapp/WEB-INF/web.xml
待配置内容
- org.springframework.web.context.ContextLoaderListener
监听项目上下文启动、停止,初始化父容器 - org.springframework.web.servlet.DispatcherServlet
初始化子容器,此处加载的内容与View层相关的bean - org.springframework.web.filter.CharacterEncodingFilter
用于统一数据编码
在Spring中,子容器可以访问父容器中的bean,但父容器无法访问子容器中的bean
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--配置springmvc 监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-config/spring-context.xml</param-value>
</context-param>
<!--若无context-param则ContextLoaderListener默认加载WEB-INFO目录下的applicationContext.xml文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置springmvc DispatcherServlet-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--若无init-param则默认加载WEB-INFO目录下的xxx-servlet.xml(即此处默认加载springMVC-servlet.xml)文件 -->
<init-param>
<!--Sources标注的文件夹下需要新建一个spring文件夹-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--设置字符编码过滤器-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern><!--拦截全部请求-->
</filter-mapping>
</web-app>
spring-servlet.xml配置
文件路径:/src/main/resources/spring-config/spring-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="spring.mybatis.sample">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" />
</context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
spring-content.xml配置
文件路径:/src/main/resources/spring-config/spring-content.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="spring.mybatis.sample">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
</bean>
代码及效果图
package spring.mybatis.sample;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* Created by liuquan on 2019/3/20.
*/
@Controller
public class Welcome {
@RequestMapping("/welcome/sayHello.web")
public ModelAndView sayHello() {
ModelAndView mv = new ModelAndView();
mv.setViewName("pages/index");
return mv;
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
Welcome here!
</body>
</html>
添加 JSON 支持
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
在Welcome.java
中添加如下代码
@ResponseBody
@RequestMapping("welcome/queryUser.json")
public UserBO queryUser(){
UserBO userBO = new UserBO();
userBO.setUserName("野猴子");
userBO.setAddress("湖北 武汉");
userBO.setAge(28);
userBO.setSex(1);
userBO.setId("1");
return userBO;
}