代码改变世界

【转】SSH标准配置

2012-07-20 10:56  weiofn  阅读(209)  评论(0编辑  收藏  举报

    最近啊现在公司面试可能会有一些的上机题出现了,不过还好,不是很难,都是一些SSH配置然后做一个很简单的小功能啊什么的。那么这个大家平常的SSH都是怎么配的呢,如果让你上网还好,如果不让你上网是不是当时就蒙了呢。

    其实不让你上网那我们就自己弄吧,没什么大不了的。熟悉一两次就好了,面试的时候千万不能让那些没有技术含量的东西给卡下来了,那可真就太不值得了。

    如果只给你hibenrate包,spring包和struts包,那么让你将这三个框架配置起来做以
个登陆,那么你应该怎么去集成呢?这几个配置文件怎么去弄呢?
   
    1.首先拷贝jar包,
    a)那么struts中的jar包都需要拷贝什么呢?将struts中的所有的包都拷贝过来,一共是八个.然后就是jstl的包也要拷过来.所以struts的jar包一共是十个.
    b)然后是hibernate包的拷贝.hibernate的拷贝比较简单,一个lib下面是38个,然后还有以个核心包hibernate3.jar,所以一共是39个jar包.
    c)然后就是spring的配置了,spring比较麻烦一点点,它一共有四个,以个是核心包spring.jar,一个是lib\aspectj下面的两个jar包,以个是junit测试包lib\junit下的junit.jar.
    d)最后就是mysql的包了,那么你用哪个数据库就自己往里面加哪个数据库的包吧.

    2.当jar包拷贝完了之后呢我们开始写配置文件了,首先我们从底层来写这个配置文件,从hibernate.cfg.xml开始.这个文件我们可以从我们下的hibernate的jar包里面找到. 我们可以在hibernate-3.2.0\etc里面找到hibernate.cfg.xml这个文件,然后我们拷贝到我们的src中,然后我们删掉其中没有用的东西,只留下下面这些.

 

    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>

<hibernate-configuration>
    <session-factory>
        
    </session-factory>
</hibernate-configuration>

 

    3.紧接着我们开始陪上一层的配置文件,这是以个spring的配置文件,这个spring的配置文件一共有三个,第一个就是applicationContext-common.xml,它是负责我们的事务的配置,我们要保持事务,那么就要用spring来给我们管理session,那么我们的sessionFactory就是由spring来创建的,而且在这个里面我们需要配置事务的传播特性,哪些方法要使用事务,事务的传播特性.这个xml文件前面的头部信息可以从另外一个文件(spring-framework-2.0\samples\jpetstore\war\WEB-INF)里面拷过来,这样这个头文件就有了,注意其他的applicationContext拷贝过来它的头部信息可能会少一些,那么有可能影响我们的程序的运行,所以我们用这个里面的头文件.

 

<?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:aop
="http://www.springframework.org/schema/aop"
         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.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
>
    <!-- 配置sessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>    
    </bean>         
    
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>
    
    <!-- 配置事务的传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <!-- 
            <tx:method name="del*" propagation="REQUIRED"/>
            
-->
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- 配置哪些类的哪些方法使用事务 -->
    <aop:config>
        <aop:pointcut id="allManagerMethod" expression="execution(* com.xxxx.xxxx.xxxx.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
    </aop:config>
</beans>

 

 

    4.然后我们还要准备以个applicationContext-bean.xml文件,这个文件里面我们要放一些我们的业务逻辑类的配置,因为我们所有的业务逻辑类的创建都是由spring替我们完成的,那么我们就应该将所有的xxxManager都放到这个spring里面来配置,那么这个文件里面放置的都是xxxManager的配置.
    5.然后我们还要准备以个applicationContext-action.xml来放置struts的action的配置,也就是我们的path对应的处理类是要在这个里面去配置的.
    6.这三个文件我们建立完成了我们开始建立struts-config.xml文件,这个文件里面我们只需要留下最外层的框架就可以了.而且这个文件我们直接可以从struts-1.2.9-bin\webapps\struts-blank\WEB-INF这个里面去找到,然后把中间所有的东西都删掉.剩下的就就像这样:

 

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"
>

<struts-config>    
</struts-config>

 

    7.然后我们开始配置我们的web.xml文件,这个文件的配置是最麻烦的,因为我们要控制session的打开和关闭,我们用到了openSessionInView,这个文件可以先从这个地方拿到tomcat\server\webapps\admin\WEB-INF,然后我们把这个文件里面没有用的东西都给它拿掉,留下这些内容:

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    xmlns
="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- Standard Action Servlet Configuration (with debugging) -->
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>


  <!-- Standard Action Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
      </context-param>
      
     <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  
    <filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
     
     <filter-mapping>
        <filter-name>hibernateFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping> 
  
    <filter>
        <filter-name>Spring character encoding filter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>GBK</param-value>
        </init-param>
      </filter>
     <filter-mapping>
        <filter-name>Spring character encoding filter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
  
</web-app>

    8.现在我们的配置文件写完了,我们的任务完成了.
    9.值得注意的是现在的公司越来越倾向与让你用ssh框架来做一个小的登陆系统,那么这个时候我们就要迅速的不加思索的知道我们要干什么,我们应该怎么去做,不要再在那想上一回,然后再去做,这个东西要相当相当的熟了才行啊.
    10.在这个里面我们主要就是要记住spring的jar包都要加入哪些,然后我们要知道这个事务管理怎么去配置,然后我们要知道这些文件都要在哪个里面去拷贝.