zoukankan      html  css  js  c++  java
  • SSH框架的搭建

    之前自己跟着大神们,把Spring MVC+Spring+Mybatis框架搭好了,觉得SSM框架挺好用的,方便、快捷开发。但是出来工作后,看到SSH才是必不可少的框架,不仅要会,还要非常熟练,

    因为SSH框架是很多系统开发的最基础的框架。但是我以前却忘了巩固基础,今天突然心血来潮,也想自己搭建一个SSH框架来用用,进一步深入了解SSH框架。

    首先要下载好搭建SSH框架需要的jar包:

     1.Spring所需jar包 

    2.Struts2所需jar包(由于struts2.3.32以前的版本已出现多种漏洞,建议还是用较新的版本,以免日后还要填补漏洞,不断修改jar包)

      

    3.Hibernate所需jar包

     将这些jar包都导入到WEB-INF/lib下后就可以开始写配置文件了

    (1)首先是web.xml的配置

     1 <!-- 配置Spring的ContextLoaderListener监听器 -->
     2 
     3 <listener>
     4 
     5 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     6 </listener>
     7 <context-param>
     8 <param-name>contextConfigLocation</param-name>
     9 <param-value>classpath:applicationContext*.xml</param-value>
    10 </context-param>
    11

    <!-- session监听器 用于监听session,当session销毁时,把用户名从session列表中移除-->
    <listener>
    <listener-class>cn.mym.basic.listener.MyHttpSessionListener</listener-class>
    </listener>

    12 <!-- 配置Spring的OpenSessionInViewFilter过滤器,以解决Hibernate的懒加载异常(LazyInitializationException) -->
    13 <filter>
    14 <filter-name>OpenSessionInViewFilter</filter-name>
    15 <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    16 </filter>
    17 <filter-mapping>
    18 <filter-name>OpenSessionInViewFilter</filter-name>
    19 <url-pattern>*.action</url-pattern>
    20 </filter-mapping>
    21 <!-- 配置Struts2的过滤器 -->
    22 <filter>
    23 <filter-name>struts2</filter-name>
    24 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    25 </filter>
    26 <filter-mapping>
    27 <filter-name>struts2</filter-name>
    28 <url-pattern>/*</url-pattern>
    29 </filter-mapping>

     配置好web.xml后,开始分别配置spring、struts、hibernate

    (1)spring的配置applicationContext.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:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
        <!-- 自动扫描与装配bean -->
        <context:component-scan base-package="cn.mym"></context:component-scan>
        <!-- Kaptcha验证码 -->
        <bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
            <property name="config">
                <bean class="com.google.code.kaptcha.util.Config">
                    <constructor-arg>
                        <props>
                            <prop key="kaptcha.border">yes</prop>
                            <prop key="kaptcha.border.color">150,150,150</prop>
                            <prop key="kaptcha.textproducer.font.color">red</prop>
                            <prop key="kaptcha.textproducer.font.size">22</prop>
                            <prop key="kaptcha.image.width">70</prop>
                            <prop key="kaptcha.image.height">24</prop>
                            <prop key="kaptcha.textproducer.char.string">1234567890</prop>
                            <prop key="kaptcha.textproducer.char.length">4</prop>
                            <prop key="kaptcha.textproducer.font.names">新宋体</prop>
                            <prop key="kaptcha.background.clear.from">white</prop>
                            <prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</prop>
                            <prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.ShadowGimpy</prop>
                        </props>
                    </constructor-arg>
                </bean>
            </property>
        </bean>
        <!-- 配置SessionFactory(与Hibernate整合) -->
        <!-- 引入配置文件 -->
        <bean id="propertyConfigurer" class="cn.mym.basic.cfg.SpringPropertyPlaceholderConfigurer">
           <property name="locations">  
              <list>  
                    <value>classpath:jdbc.properties</value>  
              </list>
            </property>  
        </bean>
        <!--  <context:property-placeholder location="classpath:jdbc.properties" />  -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="close">
            <!-- 指定Hibernate的配置文件的位置 -->
            <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
            <!-- 配置DataSource -->
            <property name="dataSource">
                <bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
                    <!-- 数据库连接信息 -->
                    <property name="jdbcUrl" value="${jdbcUrl}"></property>
                    <property name="driverClass" value="${driverClass}"></property>
                    <property name="user" value="${username}"></property>
                    <property name="password" value="${password}"></property>
                    <!-- 其他一些配置 -->
                    <property name="initialPoolSize" value="${initialPoolSize}"></property>
                    <property name="minPoolSize" value="${minPoolSize}"></property>
                    <property name="maxPoolSize" value="${maxPoolSize}"></property>
                    <property name="acquireIncrement" value="${acquireIncrement}"></property>
                    <property name="maxStatements" value="${maxStatements}"></property>
                    <property name="maxStatementsPerConnection" value="${maxStatementsPerConnection}"></property>
                    <property name="maxIdleTime" value="${maxIdleTime}"></property>
                </bean>
            </property>
        </bean>
        <!-- 配置声明式事务,使用基于注解的方式 -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        <tx:annotation-driven transaction-manager="transactionManager"/>
    </beans>

    (1)struts的配置struts.xml文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
        <!-- 设定为开发模式 -->
        <constant name="struts.devMode" value="false" />
        <!-- 指定action的后缀 -->
        <constant name="struts.action.extension" value="action" />
        <!-- 指定主题为simple -->
        <constant name="struts.ui.theme" value="simple" />
    
        <package name="default" namespace="/" extends="struts-default,jasperreports-default">
            <!-- 全局的result配置 -->
            <!-- 配置拦截器 -->
            <interceptors>
                <!-- 声明拦截器 -->
                <interceptor name="checkPrivilege" class="cn.mym.basic.interceptor.CheckPrivilegeInterceptor"></interceptor>
                <!-- 声明拦截器栈 -->
                <interceptor-stack name="myStack">
                    <interceptor-ref name="checkPrivilege" />
                    <interceptor-ref name="defaultStack" />
                </interceptor-stack>
            </interceptors>  
            <!-- 默认本包中的所有action都要经过myStack这个拦截器栈 -->
            <default-interceptor-ref name="myStack"></default-interceptor-ref>       
            <!-- 全局的result配置 -->
            <global-results>
                <result name="loginUI">/WEB-INF/jsp/loginLogoutAction/loginUI.jsp</result>
                <result name="index">/WEB-INF/jsp/indexAction/index.jsp</result>
                <result name="privilegeError">/privilegeError.jsp</result>
                <result name="error" >/error.jsp</result>
            </global-results>  
            <!-- 全局错误跳转 -->
            <global-exception-mappings>
                <exception-mapping exception="java.lang.NullPointerException" result="error" />
                <exception-mapping exception="java.lang.Exception" result="error" />
            </global-exception-mappings>
    
            <!-- Struts2与Spring整合后,class属性中写的是bean的名称 -->
            <action name="testAction" class="testAction">
                <result name="success">/test.jsp</result>
            </action>
            <!-- 岗位管理 -->
            <action name="roleAction_*" class="roleAction" method="{1}">
                <result name="list">/WEB-INF/jsp/roleAction/list.jsp</result>
                <result name="saveUI">/WEB-INF/jsp/roleAction/saveUI.jsp</result>
                <result name="setPrivilegeUI">/WEB-INF/jsp/roleAction/setPrivilegeUI.jsp</result>
                <result name="toList" type="redirectAction">roleAction_list</result>
    </action>

    </package>

    </struts>

    (3)hibernate的配置hibernate.cfg.xml

    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
    <session-factory>
        <!-- 数据库信息 -->
        <property name="dialect">
            org.hibernate.dialect.MySQL5InnoDBDialect
        </property>
        <!-- 其他配置 -->
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <!-- 开启二级缓存.  -->
        <property name="hibernate.cache.use_second_level_cache">
            true
        </property>
        <!-- 指定二级缓存提供商  EhCacheProvider:此类仅仅是一个缓存的加载类.  -->
        <property name="hibernate.cache.provider_class">
            org.hibernate.cache.EhCacheProvider
        </property>
        <!-- 启用查询缓存 -->
        <property name="cache.use_query_cache">true</property>
        <!-- 声明映射文件 -->
        <!-- 基本模块 -->
        <mapping resource="cn/mym/basic/domain/User.hbm.xml" />
        <!-- 招生及收费管理系统模块 -->
        <mapping resource="cn/mym/scms/domain/Secondary.hbm.xml" />
        <mapping resource="cn/mym/scms/domain/SecondaryCheck.hbm.xml" />
        <class-cache usage="read-write"
            class="cn.mym.basic.domain.CodeType" />
    </session-factory>
    </hibernate-configuration>

    session监听类

    public class MyHttpSessionListener implements HttpSessionListener{
    
        public void sessionCreated(HttpSessionEvent se) {//创建session       
        }
        @SuppressWarnings("unchecked")//当session销毁时,移除登录名
        public void sessionDestroyed(HttpSessionEvent se) {
            User user = (User) se.getSession().getAttribute("user");
            Map<String,Object> userMap = (Map<String, Object>) se.getSession().getServletContext().getAttribute("userMap");
            if(user!=null){
                if(userMap.get(user.getLoginName())!=null){
                    userMap.remove(user.getLoginName());
                }
            }
        }
    }

    引入配置jdbc配置文件jdbc.properties的SpringPropertyPlaceholderConfigurer

    public class SpringPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
    throws BeansException
    {
    String password = (String)props.get("password");
    if (password != null)
    props.setProperty("password", SecurityUtils.decryptAES(password));
    
    super.processProperties(beanFactoryToProcess, props);
    }
    }
  • 相关阅读:
    Java的HashMap
    为什么 char c = 'A';c += 32; 结果输出的是 'a'?
    java中整数的常量优化机制
    IDEA2019版中文汉化包
    asp.net项目协作开发时,常常要忽略版本控制的目录
    SQLServer同步数据到ElasticSearch
    为什么不建议在微信小程序的TarBar上检查统一登录后跳转页面
    nginx的热备方式
    HTTP 和FTP 状态信息总结(留着自己用)
    Web Api 简介
  • 原文地址:https://www.cnblogs.com/ouyanxia/p/6601142.html
Copyright © 2011-2022 走看看