zoukankan      html  css  js  c++  java
  • SSM之旅(一)--最最简易的框架实现

    SSM框架的主要配置主要在以下几个配置文件上:

    1、web.xml

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    <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"
             version="3.0">
    
    
      <!-- 初始化数据库 -->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
          classpath:dal/mybatis-config.xml
        </param-value>
      </context-param>
    
      <!-- Spring字符集过滤器(解決POST请求乱码的问题) -->
      <filter>
        <filter-name>SpringEncodingFilter</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>
        <init-param>
          <param-name>forceEncoding</param-name>
          <param-value>true</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>SpringEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    
      <!--1、Spring提供ServletContentListener的一个实现类ContextLoaderListener监听器,该类可以作为Listener使用,在启动Tomcat
          容器的时候,该类的作用就是自动装载ApplicationContext的配置信息,如果没有设置contextConfigLocation的初始参数则会
          使用默认参数WEB-INF路径下的application.xml文件。如果需要自定义读取多个配置文件或者修改默认路径,则可以在web.xml
          中设置。
          2、ContextLoaderListener会读取这些XML文件并产生 WebApplicationContext对象,然后将这个对象放置在ServletContext的属性
          里,这样我们只要可以得到Servlet就可以得到WebApplicationContext对象,并利用这个对象访问spring容器管理的bean。
         -->
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <servlet>
        <servlet-name>controller</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>
            /WEB-INF/spring/servlet-context.xml,
            classpath:biz/biz-*.xml
          </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
      </servlet>
      <!--为DispatcherServlet核心控制器建立映射 -->
      <servlet-mapping>
        <servlet-name>controller</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
      <display-name>Archetype Created Web Application</display-name>
    </web-app>
    View Code

    2、servlet-context.xml是springMVC的配置文件

    <?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:p="http://www.springframework.org/schema/p"
           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.1.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    
        <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
        <context:component-scan base-package="cn.com.youlove.jxt.web.controller" />
    
        <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name = "prefix" value="/WEB-INF/views/"></property>
             <property name = "suffix" value = ".jsp"></property>
         </bean>
    </beans>
    View Code

    3、mybatis-config.xml为数据库配置,是mybatis与spring的集成

    <?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:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    
        <!-- 数据源配置 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
              destroy-method="close">
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
            <!-- 初始化连接大小 -->
            <property name="initialSize" value="${jdbc.initialSize}"></property>
            <!-- 连接池最大数量 -->
            <property name="maxActive" value="${jdbc.maxActive}"></property>
            <!-- 连接池最大空闲 -->
            <property name="maxIdle" value="${jdbc.maxIdle}"></property>
            <!-- 连接池最小空闲 -->
            <property name="minIdle" value="${jdbc.minIdle}"></property>
            <!-- 获取连接最大等待时间 -->
            <property name="maxWait" value="${jdbc.maxWait}"></property>
            <!-- 连接属性 -->
            <property name="connectionProperties">
                <value>clientEncoding=UTF-8</value>
            </property>
        </bean>
    
        <!-- 自动扫描
             1、在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的Java文件,
             如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean。 -->
        <context:component-scan base-package="cn.com.youlove.jxt" />
    
        <!-- 加载jdbc -->
        <context:property-placeholder location="classpath:config/jdbc.properties"/>
    
        <!-- mybatis的配置文件与spring Bean的加载对应-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <!-- 加载mybatis的配置文件 -->
            <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
        </bean>
    
        <!-- mybatis额配置文件对应的bean -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="cn.com.youlove.jxt.dao"/>
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        </bean>
    
        <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
        <bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <!--注解方式实现事务-->
        <tx:annotation-driven transaction-manager="transactionManager"/>
    
    </beans>
    View Code

    上述的基本配置集成完毕之后,可以进行控制器编写

    4、IndexController.java 首页示例控制器

    package cn.com.youlove.jxt.web.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    /**
     * 首页控制器
     * Created by YGL on 2017/5/17.
     */
    @Controller
    public class IndexController {
    
        @RequestMapping(value = "/index")
        public String indexApi(final Model model){
    
            ModelAndView mv = new ModelAndView();
            mv.addObject("message","xiaomign");
            mv.setViewName("index");
            return "index";
        }
    }
    View Code

    5.index.jsp  最简单的这个框架中springMVC视图解析中只支持jsp

    <html>
    <body>
    <h2>Hello World!</h2>
    </body>
    </html>
    View Code

    上述代码即是最最最简单的SSM框架,是不是简单爆了。

    下面我们要开始慢慢丰富和填充该框架了。下一讲,我们换个视图解析。

     
  • 相关阅读:
    由前序和中序遍历结果构建二叉树
    Java学习笔记数组与ArrayList
    Java学习笔记字符串
    Java学习笔记关于默认类型或访问权限的总结
    javascript学习笔记之事件和事件处理
    2010年2月1日学习笔记
    Web.config保存整个站点的设置
    ANT的十五大最佳实践
    配置ajaxToolkit的方法【转】
    Java学习笔记Iterator迭代器(Ps.instanceof的用法)
  • 原文地址:https://www.cnblogs.com/DASOU/p/7325248.html
Copyright © 2011-2022 走看看