码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-09-02 20:30:37      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:

整合Spring和SpringMVC
之前已经整合了spring和mybatis,现在在此基础上整合SSM。
项目目录:
技术分享
思路:SpringMVC的配置文件独立,然后在web.xml中配置整合。
(1)配置spring-mvc.xml
主要是自动扫描控制器,视图模式。
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:mvc="http://www.springframework.org/schema/mvc"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-3.2.xsd
  12. http://www.springframework.org/schema/mvc
  13. http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
  14. <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
  15. <context:component-scanbase-package="com.aheizi.controller"/>
  16. <!-- 避免IE在ajax请求时,返回json出现下载 -->
  17. <beanid="jacksonMessageConverter"class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
  18. <propertyname="supportedMediaTypes">
  19. <list>
  20. <value>text/html;charset=UTF-8</value>
  21. </list>
  22. </property>
  23. </bean>
  24. <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
  25. <beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
  26. <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
  27. <propertyname="prefix"value="/WEB-INF/jsp/"/>
  28. <propertyname="suffix"value=".jsp"/>
  29. </bean>
  30. </beans>
 
(2)配置Web.xml
包含spring和mybatis的配置文件,编码过滤器,日志记录,spring监听器,防止spring内存溢出监听器,springmvc 核心配置和错误跳转页面。
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-appxmlns: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">
  3. <display-name>spring_mybatis_springmvc</display-name>
  4. <!-- Spring和mybatis的配置文件 -->
  5. <context-param>
  6. <param-name>contextConfigLocation</param-name>
  7. <param-value>classpath:config/spring-mybatis.xml</param-value>
  8. </context-param>
  9. <!-- 编码过滤器 -->
  10. <filter>
  11. <filter-name>encodingFilter</filter-name>
  12. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  13. <async-supported>true</async-supported>
  14. <init-param>
  15. <param-name>encoding</param-name>
  16. <param-value>UTF-8</param-value>
  17. </init-param>
  18. </filter>
  19. <filter-mapping>
  20. <filter-name>encodingFilter</filter-name>
  21. <url-pattern>/*</url-pattern>
  22. </filter-mapping>
  23. <!-- 日志记录 (必须在ContextLoaderListener之前)-->
  24. <context-param>
  25. <!-- 日志配置文件路径 -->
  26. <param-name>log4jConfigLocation</param-name>
  27. <param-value>classpath:properties/log4j.properties</param-value>
  28. </context-param>
  29. <context-param>
  30. <!-- 日志页面的刷新间隔 -->
  31. <param-name>log4jRefreshInterval</param-name>
  32. <param-value>6000</param-value>
  33. </context-param>
  34. <listener>
  35. <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  36. </listener>
  37. <!-- Spring监听器 -->
  38. <listener>
  39. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  40. </listener>
  41. <!-- 防止Spring内存溢出监听器 -->
  42. <listener>
  43. <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  44. </listener>
  45. <!-- Spring MVC 核心配置 -->
  46. <servlet>
  47. <servlet-name>SpringMVC</servlet-name>
  48. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  49. <init-param>
  50. <param-name>contextConfigLocation</param-name>
  51. <param-value>classpath:spring-mvc.xml</param-value>
  52. </init-param>
  53. <load-on-startup>1</load-on-startup>
  54. <async-supported>true</async-supported>
  55. </servlet>
  56. <!-- 后缀 -->
  57. <servlet-mapping>
  58. <servlet-name>SpringMVC</servlet-name>
  59. <url-pattern>*.do</url-pattern>
  60. </servlet-mapping>
  61. <!-- 欢迎页面 -->
  62. <welcome-file-list>
  63. <welcome-file>/index.jsp</welcome-file>
  64. </welcome-file-list>
  65. <!-- 错误跳转页面 -->
  66. <error-page>
  67. <!-- 路径不正确 -->
  68. <error-code>404</error-code>
  69. <location>/WEB-INF/errorpage/404.html</location>
  70. </error-page>
  71. <error-page>
  72. <!-- 没有访问权限,访问被禁止 -->
  73. <error-code>405</error-code>
  74. <location>/WEB-INF/errorpage/405.html</location>
  75. </error-page>
  76. <error-page>
  77. <!-- 内部错误 -->
  78. <error-code>500</error-code>
  79. <location>/WEB-INF/errorpage/500.html</location>
  80. </error-page>
  81. </web-app>
(3)创建showUser.jsp页面
  1. <%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <metacharset="UTF-8">
  6. <title>User Index</title>
  7. </head>
  8. <body>
  9. <h3>hello</h3><br/>
  10. ${user.userName}
  11. </body>
  12. </html>
执行maven clean install,运行tomcat,输入 
127.0.0.1/项目名/user/showUser?id=1
页面如下:
技术分享
OK,搞定!
 
源码下载地址:()
 
 





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

标签:

原文地址:http://www.cnblogs.com/aheizi/p/4779237.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!