搭建Spring + SpringMVC + Mybatis框架之三(整合Spring、Mybatis和Spring MVC)

目录

整合Spring和SpringMVC

整合Spring和SpringMVC

之前已经整合了spring和mybatis,现在在此基础上整合SSM。
项目目录:

思路:SpringMVC的配置文件独立,然后在web.xml中配置整合。

(1)配置spring-mvc.xml

主要是自动扫描控制器,视图模式。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:p="http://www.springframework.org/schema/p"
  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"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
     
    <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
    <context:component-scan base-package="com.aheizi.controller"/>
    
    <!-- 避免IE在ajax请求时,返回json出现下载 -->
    <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">    
         <property name="supportedMediaTypes">
             <list>
                 <value>text/html;charset=UTF-8</value>
             </list>
         </property>
     </bean>
     
    <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
</beans>

(2)配置Web.xml

包含spring和mybatis的配置文件,编码过滤器,日志记录,spring监听器,防止spring内存溢出监听器,springmvc 核心配置和错误跳转页面。

<?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">
    <display-name>spring_mybatis_springmvc</display-name>
    
    <!-- Spring和mybatis的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:config/spring-mybatis.xml</param-value>
    </context-param>
    
    <!-- 编码过滤器 -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <async-supported>true</async-supported>
        <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>
    
    <!-- 日志记录 (必须在ContextLoaderListener之前)-->
    <context-param>
        <!-- 日志配置文件路径 -->
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:properties/log4j.properties</param-value>
    </context-param>
    <context-param>
        <!-- 日志页面的刷新间隔 -->
        <param-name>log4jRefreshInterval</param-name>
        <param-value>6000</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    
    <!-- Spring监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- 防止Spring内存溢出监听器 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
 
    <!-- Spring MVC 核心配置 -->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.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>*.do</url-pattern>
    </servlet-mapping>
    
    <!-- 欢迎页面 -->
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
    
    <!-- 错误跳转页面 -->
    <error-page>
        <!-- 路径不正确 -->
        <error-code>404</error-code>
        <location>/WEB-INF/errorpage/404.html</location>
    </error-page>
    <error-page>
        <!-- 没有访问权限,访问被禁止 -->
        <error-code>405</error-code>
        <location>/WEB-INF/errorpage/405.html</location>
    </error-page>
    <error-page>
        <!-- 内部错误 -->
        <error-code>500</error-code>
        <location>/WEB-INF/errorpage/500.html</location>
    </error-page>
    
</web-app>

(3)创建showUser.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User Index</title>
</head>
<body>
        <h3>hello</h3><br/>
        ${user.userName}
</body>
</html>

执行maven clean install,运行tomcat,输入
127.0.0.1/spring_mybatis_springmvc/user/showUser.do?id=1(我把tomcat端口设置成了80)
页面如下:

OK,搞定!

时间: 2024-08-24 19:11:26

搭建Spring + SpringMVC + Mybatis框架之三(整合Spring、Mybatis和Spring MVC)的相关文章

myBatis,Spring,SpringMVC三大框架ssm整合模板

整合步骤 创建web工程 导入整合所需的所有jar包 编写各层需要的配置文件 1) mybatis的全局配置文件 <configuration> <!-- 批量别名的设置 -->    <typeAliases>        <package name="cn.ssm.pojo"/>    </typeAliases>     </configuration> 2) spring管理mybatis的配置文件 &l

spring ,springMVC,shiro 框架搭建。

1.  导入相关jar包. 2.完成spring的相关配置 1) 在web.xml中完成spring的声明 contextLoaderListener的配置,如果是location,那么配置文件必须放到web项目的根目录中. 如果是放到类路径下,需要改为classpath:applicationContext.xml 2)新建spring的配置文件 这个名称必须与web.xml中,配置的一致.如applicationContext.xml 3.完成springMVC的配置 1)在web.xml中

Spring+SpringMVC+Hibernate 与 shiro 整合步骤

通过这篇文章你可以了解到: SSH 三大框架(spring + springMVC + Hiberante) 与 shiro 安全验证框架如何整合: 通过一个示例,快速理解 shiro 框架. [TOC] 1. 业务需求分析 用户 N - 角色 N - 权限 N 我们可以想象一下,在平时工作中的职务,比如:业务经理,部门主管等,他们拥有很多的权力,而一个公司中不会只有一个业务经理,也不会只有一个部门主管,如果我们要给不同的人分配职务权力时,每次都是具体的条条框框去分配,人累心也累.而如果我们事先

2016.5.25 Mybatis 框架(三) Mybatis 框架原理

1. mybatis是什么? mybatis是一个持久层框架,是apache下的顶级项目 先是托管到googlecode下,后托管到github下(https://github.com/mybatis-3/release) mybatis让程序员将主要精力放在sql上,通过mybatis提供的映射方式,自由灵活生成满足 需要的sql语句(半自动化,大部分需要程序员编写sql) mybatis可以将向preparedStatement中的输入参数 自动进行输入映射, 将查询结果集灵活映射成java

搭建Spring + SpringMVC + Mybatis框架之三(整合Spring和Mybatis)

整合Spring和SpringMVC 之前已经整合了spring和mybatis,现在在此基础上整合SSM. 项目目录: 思路:SpringMVC的配置文件独立,然后在web.xml中配置整合. (1)配置spring-mvc.xml 主要是自动扫描控制器,视图模式. <?xml version="1.0" encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/sch

空气质量管理系统ssm(mybatis+spring+springMVC)框架+前后端分离

1.目录结构: 2.需要注意的地方 2.1在WEB-INFO下新建 2.1.1 springMVC-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-instanc

spring+springmvc+hibernate 框架搭建

1.新建web项目,将所需jar包放到 lib 目录下 2.配置web.xml 配置文件 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLo

spring + springmvc + jdbc + quartz + maven整合

个人搭建框架: pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

java 的 struts2 Spring Hibernate 三大框架的整合

原理就不说了,直接上配置文件及代码,用来备用 首先,将三大框架所需要的jar包导入项目中 导入  struts2-spring-plugin-2.3.3.jar包  此包的作用是作为struts2 与spring 的桥梁 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml