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 ...
随机推荐
- 关于xml加载提示: Error on line 1 of document : 前言中不允许有内容
我是在java中做的相关测试, 首先粘贴下报错: 读取xml配置文件:xmls\property.xml org.dom4j.DocumentException: Error on line 1 of ...
- 无脑简单 命令升级git Centos
yum remove git yum install zlib (系统默认已经装上) yum install zlib-devel ># wget https://github.com/git/ ...
- JAVA 问题集中之处以及解决的办法
也许当你看的时候,你可能认为这些都是简单的问题,有什么好记的.其实不是,我认为,我们往往是因为粗心而造成的错误,当你在开发中碰到这些问题时,你能一下看出来是什么错误,达到提高效率.而且往往你把小的问题 ...
- 学习笔记 HTTP参数污染注入
HTTP参数污染注入源于网站对于提交的相同的参数的不同处理方式导致. 例如: www.XX.com/a?key=ab&key=3 如果服务端返回输入key的值,可能会有 一: ab 二:3 三 ...
- ArcGIS Engine开发之鹰眼视图
鹰眼是GIS软件的必备功能之一.它是一个MapControl控件,主要用来表示数据视图中的地理范围在全图中的位置. 鹰眼一般具有的功能: 1)鹰眼视图与数据视图的地理范围保持同步. 2)数据视图的当前 ...
- 中国式商业智能报表ActiveReports免费公开课,10月20日开讲
ActiveReports公开课全方位报表解决方案,满足商业报表五大需求 [开课时间]10月20日[主讲老师]葡萄城报表产品经理[开课形式]网络在线公开课[活动费用]前50名免费 适合人群:报表开发人 ...
- iOS之17个提升iOS开发效率的必用工具
时间就是金钱.编码效率的提升意味着更多的收入.可是当我们的开发技巧已经到达一定高度时,如何让开发效率更上一层楼呢?答案就是使用开发工具!在这篇文章中,我会向你介绍一些帮助我提升编码速度和工作效率的工具 ...
- android Canvas 和 Paint用法
自定义view里面的onDraw方法,在这里我们可以绘制各种图形,onDraw里面有两个API我们需要了解清楚他们的用法:Canvas 和 Paint. Canvas翻译成中文就是画布的意思,Canv ...
- IOS 杂笔-14(被人遗忘的owner)
*owner在开发中现在已经很少用了 有兴趣的童鞋可以看看* 我们遇到owner通常是在类似 [[[NSBundle mainBundle] loadNibNamed:@"Food" ...
- SQL SERVER 数据库各版本功能对比
以前写了篇SQL SERVER 2008数据库各版本功能对比,官网提供的那个功能确实很好很强大,后面发现那个链接失效了.今天又遇到要对比SQL Server 2014数据库版本功能需求,搜索找了好久才 ...