源码下载 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整合的更多相关文章

  1. springMVC整合Junit4进行单元测试

    springMVC整合Junit4进行单元测试 标签: springMVC整合Junit4junit单元测试教程springMVC入门教程   spring(10)  版权声明:本文为博主原创文章,未 ...

  2. SSM框架整合环境构建——基于Spring4和Mybatis3

    目录 环境 配置说明 所需jar包 配置db.properties 配置log4j.properties 配置spring.xml 配置mybatis-spring.xml 配置springmvc.x ...

  3. SSM框架整合的详细过程(含每一步的分析及代码)。实质上是SpringMVC与mybatis的整合,应为spring与SpringMVC不需要整合。

    为了更好的学习 springmvc和mybatis整合开发的方法,需要将springmvc和mybatis进行整合. 整合目标:控制层采用springmvc.持久层使用mybatis实现. 1.1 需 ...

  4. Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x

    Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x ============================== 蕃薯耀 2018年 ...

  5. JAVAEE——SpringMVC第一天:介绍、入门程序、架构讲解、SpringMVC整合MyBatis、参数绑定、SpringMVC和Struts2的区别

    1. 学习计划   第一天 1.SpringMVC介绍 2.入门程序 3.SpringMVC架构讲解 a) 框架结构 b) 组件说明 4.SpringMVC整合MyBatis 5.参数绑定 a) Sp ...

  6. spring mybatis springmvc整合

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

  7. springmvc整合mybatis 配置文件

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

  8. ActiveMQ与SpringMVC整合实现发送PTP和订阅发布消息功能

    实现一个基于SpringMVC+JMS+ActiveMQ+Tomcat+JDK1.8+IDEA工具 ,Spring4.1.0和ActiveMQ5.15整合的实例,实现PTP和订阅/发布两种消息模型 一 ...

  9. 【springmvc thymeleaf】springmvc整合thymeleaf

    概述 Thymeleaf提供了一组Spring集成,使您可以将其用作Spring MVC应用程序中JSP的全功能替代品. 这些集成将使您能够: @Controller像使用JSP一样,将Spring ...

随机推荐

  1. iPhone开发学习

    Xcode6中怎么添加空工程模板  NSBundle 使用(程序的资源)     本地存储数据的简单方式:NSUserDefaults UIApplication隐藏statusBarStyle    ...

  2. Servlet3.0的注解

    1.@WebListener注解 表示的就是我们之前的在xml中配置的 <listener> <listener-class>ListenerClass</listene ...

  3. 分布式文件系统 - FastDFS 在 CentOS 下配置安装部署

    少啰嗦,直接装 看过上一篇分布式文件系统 - FastDFS 简单了解一下的朋友应该知道,本次安装是使用目前余庆老师开源的最新 V5.05 版本,是余庆老师放在 Github 上的,和目前你能在网络上 ...

  4. 【问题】Asp.net MVC 的cshtml页面中调用JS方法传递字符串变量参数

    [问题]Asp.net MVC 的cshtml页面中调用JS方法传递字符串变量参数. [解决]直接对变量加引号,如: <button onclick="deleteProduct('@ ...

  5. VBA常用代码解析

    031 删除工作表中的空行 如果需要删除工作表中所有的空行,可以使用下面的代码. Sub DelBlankRow() DimrRow As Long DimLRow As Long Dimi As L ...

  6. ipad和iphone的适配

    关于xib或者storybord下iphone的横竖屏的适配以及ipad的适配 ios8出现了Size Classes,解决了各种屏幕适配的问题,他把屏幕的宽和高分别分成了三种,把屏幕总共分成了九种情 ...

  7. Google C++单元测试框架GoogleTest(总)

    之前一个月都在学习googletest框架,对googletest的文档都翻译了一遍,也都发在了之前的博客里,另外其实还有一部分的文档我没有发,就是GMock的CookBook部分:https://g ...

  8. Android中的 init.rc文件简介

    init.rc脚本是由Android中linux的第一个用户级进程init进行解析的. init.rc 文件并不是普通的配置文件,而是由一种被称为"Android初始化语言"(An ...

  9. SQL Server快速查询某张表的当前行数

    传统做法可能是select count(1) 但是往往会比较慢.推荐如下做法: ) CurrentRowCount FROM sys.sysindexes WHERE id = OBJECT_ID(' ...

  10. ubuntu 16.04 安装nodejs

    经过几天的尝试,终于装好了: 1. nodejs官方推荐一下安装方式: NodeSource的二进制安装脚本NodeSource Using Ubuntu curl -sL https://deb.n ...