SSM整合(三):Spring4与Mybatis3与SpringMVC整合
源码下载 SSMDemo
上一节整合了Mybatis3与Spring4,接下来整合SpringMVC!
说明:整合SpringMVC必须是在web项目中,所以前期,新建的就是web项目!
本节全部采用注解式,如要了解其他的配置方式请查看相关详细的文章!
一、新建SpringMVC配置文件applicationContext-mvc.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 使用注解配置 -->
<mvc:annotation-driven />
<!--自动扫描包 -->
<context:component-scan base-package="com.ssm.demo2.controller" />
<!-- ViewResolver 视图解释器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property
name="viewClass"
value="org.springframework.web.servlet.view.JstlView"/>
<property
name="prefix"
value="/WEB-INF/jsp/" />
<property
name="suffix"
value=".jsp" />
</bean>
</beans>
二、在WebApp/WEB-INF新建jsp目录存放jsp页面!
更改WebApp下index.jsp文件(如没有,新建)内容如下

三、在web.xml文件中添加spring的相关配置文件:
因springmvc是spring的一相模块,也通称为Spring配置文件!
内容如下:
<?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"
id="WebApp_ID"
version="3.0" >
<!-- Spring应用上下文,
理解层次化的ApplicationContext ,
applicationContext-*.xml这里用了通配符,
这里只要与spring整合的文件符合这样的格式将自动全部加载
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<!--
DispatcherServlet, Spring MVC的核心 -->
<servlet>
<servlet-name>SSMDemo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- DispatcherServlet对应的上下文配置, 默认为/WEB-INF/$servlet-name$-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SSMDemo</servlet-name>
<!-- mvc-dispatcher拦截所有的请求
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--forwarding 跳转的servlet-->
<servlet>
<servlet-name>forwarding</servlet-name>
<servlet-class>com.spring.mvcdemo.servlet.ForwardingServlet</servlet-class>
</servlet>
<!--配置文件编码 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Spring 全局上下文的
监听,当配置必须有 applicationContext.xml文件,否则报错 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
四、配置tomcat
在eclipse 中windows->preferences
->Server中runtime
environment 添加:

选择自己本地的安装版本号

选择tomcat安装目录:

点Finish 完成!
4.2)在server控制面进行添加服务器

前面配置好了,直接就会显示出,刚配好的tomcat,点完成!
双击服务器进行配置

把配置改成以上图所示:
4.2)部署项目
在项目名右键 run on server

五、开启tomcat部署,报错:

这是因为在上一节中 配置sqlSessionFactory时配置文件少加了classpath: !
在applicationContext-dao.xml中把定义sqlSessionFactory中的配置加上classpath:

再部署再报如下错:

提示缺少jar包,在pom.xml中添加:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.0.Final</version>
</dependency>
然后maven ->update project 一下,再重新部署,ok!

这时显示index.jsp页面的内容只说明web环境搭建好,配置文件是ok滴,并不代表与springmvc整合成功,接下来,写Controller与Service,从数据库中查询数据并显示,来测试整合成功!
六、写Controller
在com.ssm.demo2.controller新建UserController.java内容如下:
@Controller
@RequestMapping("/user")
publicclass UserController {
@Autowired
UserMapper userMapper;
@RequestMapping("/list")
public ModelAndView userlist() throws Exception{
List<User>
users=userMapper.findUserList("王");
ModelAndView
mView=new ModelAndView();
mView.addObject("users",users);
mView.setViewName("list");//返回的jsp页面名称
returnmView;
}
}
七、在jsp目录下新建list.jsp页面,内容:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<h2>SSM 部署成功! </h2>
<p>用户名:</p>
<h3>${users}</h3>
</body>
</html>
八、重新部署:
在输入地址:http://localhost:8080/ssm_demo2/user/list
结果如下:

查询与显示的结果是一样ok!
九、写Service
上面在Controller里直接操作 mapper,这是不规范的,现在在中间加上Service层
在com.ssm.demo2.service中新建UserService.java接口层
publicinterface UserService {
public List<User>findUser(String name) throws Exception;
}
9.1)新建实现类的包名com.ssm.demo2.service.impl,
并写实现类UserServiceImpl.java
@Service
publicclass UserServiceImpl implements UserService{
@Autowired
UserMapper userMapper; //spring框架配置了注解,自动注入
@Override
public List<User>
findUser(String name) throws Exception {
// TODO Auto-generated method
stub
List<User>
users=userMapper.findUserList(name);
returnusers;
}
}
再把UserController.java改成如下:
@Controller
@RequestMapping("/user")
publicclass UserController {
/*@Autowired
UserMapper
userMapper;*/
@Autowired
UserServiceImpl userServie;
@RequestMapping("/list")
public ModelAndView userlist() throws Exception{
// List<User>
users=userMapper.findUserList("王");
List<User> users=userServie.findUser("刘");
ModelAndView mView=new ModelAndView();
mView.addObject("users",users);
mView.setViewName("list");//返回的jsp页面名称
returnmView;
}
}
再重新布署:报如下错:

提示在UserController.java出错,往后再看,显示如下:
说无法注入字段,这说明spring注解配置的无效,根据配置的分类applicationContext-dao.xml是配置管理数据库及spring框架配置的,
applicationContext-mvc.xml是配置SpringMVC模块相关的:
打开applicationContext-dao.xml查看注解配置,发现包名写错:

这是Spring注解,这里包应该是根包名,而不是配置mvc注解的包名,
改成 com.ssm.demo2,再重新部署:显示

显示结果为空:可以自行把日志打印的SQl语句 运行,验证结果,的确为空!
部署ok!
但时细心的话,会发现在启动Tomcat有报错一条错误:
因太快,没有截图上来,错误
如下:

提示在 spring配置文件的 xsi:schemaLocation= 中必须是偶数uri
去掉最后spring-dwr-3.0.xsd这条uri,即可;
注:在配置这些uri时,后面最好不要加上版本号信息,让系统自动适配版本号!减少因版本号不配而报错!
此时,Mybatis3与Spring4与SpringMVC整合成功!
源码下载 SSMDemo
SSM整合(三):Spring4与Mybatis3与SpringMVC整合的更多相关文章
- springMVC整合Junit4进行单元测试
springMVC整合Junit4进行单元测试 标签: springMVC整合Junit4junit单元测试教程springMVC入门教程 spring(10) 版权声明:本文为博主原创文章,未 ...
- SSM框架整合环境构建——基于Spring4和Mybatis3
目录 环境 配置说明 所需jar包 配置db.properties 配置log4j.properties 配置spring.xml 配置mybatis-spring.xml 配置springmvc.x ...
- SSM框架整合的详细过程(含每一步的分析及代码)。实质上是SpringMVC与mybatis的整合,应为spring与SpringMVC不需要整合。
为了更好的学习 springmvc和mybatis整合开发的方法,需要将springmvc和mybatis进行整合. 整合目标:控制层采用springmvc.持久层使用mybatis实现. 1.1 需 ...
- Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x
Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x ============================== 蕃薯耀 2018年 ...
- JAVAEE——SpringMVC第一天:介绍、入门程序、架构讲解、SpringMVC整合MyBatis、参数绑定、SpringMVC和Struts2的区别
1. 学习计划 第一天 1.SpringMVC介绍 2.入门程序 3.SpringMVC架构讲解 a) 框架结构 b) 组件说明 4.SpringMVC整合MyBatis 5.参数绑定 a) Sp ...
- spring mybatis springmvc整合
使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...
- springmvc整合mybatis 配置文件
使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...
- ActiveMQ与SpringMVC整合实现发送PTP和订阅发布消息功能
实现一个基于SpringMVC+JMS+ActiveMQ+Tomcat+JDK1.8+IDEA工具 ,Spring4.1.0和ActiveMQ5.15整合的实例,实现PTP和订阅/发布两种消息模型 一 ...
- 【springmvc thymeleaf】springmvc整合thymeleaf
概述 Thymeleaf提供了一组Spring集成,使您可以将其用作Spring MVC应用程序中JSP的全功能替代品. 这些集成将使您能够: @Controller像使用JSP一样,将Spring ...
随机推荐
- .NET 实现并行的几种方式(四)
本随笔续接:.NET 实现并行的几种方式(三) 八.await.async - 异步方法的秘密武器 1) 使用async修饰符 和 await运算符 轻易实现异步方法 前三篇随笔已经介绍了多种方式.利 ...
- freemarker页面中文乱码
一.前言 简单的记录freemarker遇到的错误问题:ftl页面中文乱码 由于freemarker整合在ssm框架中,所以笔者直接贴配置代码 <beans xmlns="http:/ ...
- viewport理解
viewport预备知识 dpr === dppx dpr:device pixel ratio 设备像素比 dppx:Number of dots per px unit 每像素有多少点 . 1dp ...
- 深入理解javascript函数参数与闭包(一)
在看此文章,希望先阅读关于函数基础内容 函数定义与函数作用域 的章节,因为这篇文章或多或少会涉及函数基础的内容,而基础内容,我放在函数定义函数作用域 章节. 本文直接赘述函数参数与闭包,若涉及相关知识 ...
- HTML的doctype和编码
DOCTYPE Doctype是为了告诉浏览器用哪种版本的html去解析指令,必须位于第一行,在某些版本中需要引入DTD,html5无需引入DTD 中文乱码 原因 浏览器解析代码的编码格式与源代码编码 ...
- Unable to download data from http://ruby.taobao.org/ & don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
安装cocoapods,记录两个问题! 1.镜像已经替换成了 http://ruby.taobao.org/, 还是不能不能安装cocoapods, 报错:Unable to download dat ...
- linux jexus 服务 设置开机启动
linux的服务开机设置一般在 /etc/init.d/里 而jexus的默认安装目录在 /usr/jexus里 启动文件为 jws 参数 有start stop restart 这里贡献一个刚写好的 ...
- android Content Provider介绍
ContentProvider(内容提供者)是Android中的四大组件之一.主要用于对外共享数据,也就是通过ContentProvider把应用中的数据共享给其他应用访问,其他应用可以通过Conte ...
- 软件工程随笔(1)--jetbrain在软件工程中的应用
接下来几天我要写半年的软件工程学习后的感想,今天从介绍IDE开始.首先,本人至今为止全部项目都是在mypclise上完成的.本人采用myeclipse唯一的原因就是它使用方便.但是,我也承认myecl ...
- centos 6 YUM安装 lmp
准备篇: 1.配置防火墙,开启80端口.3306端口 vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m t ...