SSM

1、ssm

1.1、认识

ssm 是 spring mvc,spring,mybatis 的集合,是标准的 mvc 模式,将整个系统划分成为表现层,controller 层,service 层,dao 层共四层。
spring mvc 负责请求的转发与视图管理
spring 负责业务对象管理
mybatis 作为数据对象的持久化引擎

1.2、优点

1.2.1、Spring 的优势:

通过 Spring 的 IOC 特性,将对象之间的依赖关系交给了 Spring 控制,方便解耦,简化了开发
             通过 Spring 的 AOP 特性,对重复模块进行集中,实现事务,日志,权限的控制
             提供了对其他优秀开源框架的集成支持

1.2.2、Spring MVC 的优势:

SpringMVC 是使用了 MVC 设计思想的轻量级 web 框架,对 web 层进行解耦,使我们开发更简洁
             与 Spring 无缝衔接
             灵活的数据验证,格式化,数据绑定机制

1.2.3、Mybatis 的优势:

数据库的操作(sql)采用 xml 文件配置,解除了 sql 和代码的耦合
            提供映射标签,支持对象和和数据库 orm 字段关系的映射,支持对象关系映射标签,支持对象关系的组建
            提供了 xml 标签,支持动态的 sql

2、整合案例

2.1、之前

在进行 ssm 整合时,需要建立 maven web 项目
建立之后,首先将 spring 与 mybatis 整合在一起,在将 spring mvc 整合进去。添加一些文件。
spring 与 mybatis 的整合办法请看spring-mybatis
这里主要是将 spring mvc 整合进去。

2.2、新增依赖包

这些包是在原有的基础上新增加的包。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<artifactId>org.springframework</artifactId>
<groupId>spring-aspects</groupId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<artifactId>org.springframework</artifactId>
<groupId>spring-context</groupId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<artifactId>org.springframework</artifactId>
<groupId>spring-context-support</groupId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<artifactId>org.springframework</artifactId>
<groupId>spring-web</groupId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<artifactId>org.springframework</artifactId>
<groupId>spring-webmvc</groupId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>

2.3、controller 层

采用未使用注解方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import bean.LinBook;
import service.ILinBookService;
public class LoginController implements Controller {
ILinBookService linBookService;
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-mybatis.xml");
linBookService = (ILinBookService) applicationContext.getBean("ILinBookService");
int id = Integer.parseInt(request.getParameter("id"));
LinBook linBook = linBookService.getLinBook(id);
ModelAndView mve = new ModelAndView();
mve.addObject("linbook", linBook.getBookId());
mve.setViewName("index");
return mve;
}
}

2.4、前端页面

index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%> <%@ taglib
uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ page
isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<h1>输入用户:${linbook }</h1>
</body>
</html>

login.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<form action="login.do" method="post">
<table>
<tr>
<td>id:</td>
<td><input type="text" name="id" /></td>
</tr>
<tr>
<td>班级</td>
<td><input type="text" name="clazz" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交" /></td>
</tr>
</table>
</form>
</body>
</html>

2.5、spring-mvc.xml

需要建立 spring mvc 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 将url映射到具体的业务控制器 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="login.do">login</prop>
</props>
</property>
</bean>
<bean id="login" class="controller.LoginController"></bean>
<!-- 配置视图解析器 -->
<!-- InternalResourceViewResolver:用于支持servlet,jsp的解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 当jsp模板需要使用jstl标签库时,需要配置,没有则不需要配置 -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<!-- 查找视图页面的前缀和后缀 -->
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

2.6、web.xml

web 工程配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- 前端控制器 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

2.7、测试

到此,就将 spring mvc,spring,mybatis 整合好了
测试时在浏览器输入网址 localhost:8080/ssm1/login.jsp 即可

3、其他

3.1、说明

整合也可以采用注解的方式

3.2、controller

对于 controller 使用注解@Controller
其中的方法采用注解@RequestMapping(value=”***“,method=RequestMethod.GET/POST)

1
2
3
4
5
6
7
@Controller
public class LinBookController {
private LinBookService linBookService;
@RequestMapping(value = "/toindex", method = RequestMethod.POST)
public ModelAndView toindex(HttpServletRequest request) {
}
}

3.3、修改 spring-mvc.xml

加入

1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
<mvc:annotation-driven />
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="controller" />
<mvc:default-servlet-handler />
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>

删除

1
2
3
4
5
6
7
8
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="login.do">login</prop>
</props>
</property>
</bean>
<bean id="login" class="controller.LinBookController"></bean>

3.4、修改 web.xml 文件

加入

1
2
3
<!-- Spring监听器 --><listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> -->

修改

1
2
3
4
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

不能配成

1
2
3
4
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

3.5、问题

目前还无法完成对 controller 中的 service 实现注解注入,有待解决。