SSH标准配置

最近啊现在公司面试可能会有一些的上机题出现了,不过还好,不是很难,都是一些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中,然后我们删掉其中没有用的东西,只留下下面这些.
Java代码 复制代码
  1.     <!DOCTYPE hibernate-configuration PUBLIC   
  2.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  3.     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">   
  4.   
  5. <hibernate-configuration>   
  6.     <session-factory>   
  7.            
  8.     </session-factory>   
  9. </hibernate-configuration>  
    <!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拷贝过来它的头部信息可能会少一些,那么有可能影响我们的程序的运行,所以我们用这个里面的头文件.
Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.          xmlns:aop="http://www.springframework.org/schema/aop"  
  6.          xmlns:tx="http://www.springframework.org/schema/tx"  
  7.          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
  8.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd   
  9.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">   
  10.     <!-- 配置sessionFactory -->   
  11.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  12.         <property name="configLocation">   
  13.             <value>classpath:hibernate.cfg.xml</value>   
  14.         </property>      
  15.     </bean>            
  16.        
  17.     <!-- 配置事务管理器 -->   
  18.     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">   
  19.         <property name="sessionFactory">   
  20.             <ref bean="sessionFactory"/>   
  21.         </property>   
  22.     </bean>   
  23.        
  24.     <!-- 配置事务的传播特性 -->   
  25.     <tx:advice id="txAdvice" transaction-manager="transactionManager">   
  26.         <tx:attributes>   
  27.             <tx:method name="add*" propagation="REQUIRED"/>   
  28.             <!--    
  29.             <tx:method name="del*" propagation="REQUIRED"/>   
  30.             -->   
  31.             <tx:method name="*" read-only="true"/>   
  32.         </tx:attributes>   
  33.     </tx:advice>   
  34.        
  35.     <!-- 配置哪些类的哪些方法使用事务 -->   
  36.     <aop:config>   
  37.         <aop:pointcut id="allManagerMethod" expression="execution(* com.xxxx.xxxx.xxxx.*.*(..))"/>   
  38.         <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>   
  39.     </aop:config>   
  40. </beans>  
<?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这个里面去找到,然后把中间所有的东西都删掉.剩下的就就像这样:

Java代码 复制代码
  1. <?xml version="1.0" encoding="ISO-8859-1" ?>   
  2.   
  3. <!DOCTYPE struts-config PUBLIC   
  4.           "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"  
  5.           "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">   
  6.   
  7. <struts-config>      
  8. </struts-config>  
<?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,然后我们把这个文件里面没有用的东西都给它拿掉,留下这些内容:

Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <web-app version="2.4"    
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"    
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">   
  7.   <welcome-file-list>   
  8.     <welcome-file>index.jsp</welcome-file>   
  9.   </welcome-file-list>   
  10.      
  11.   <!-- Standard Action Servlet Configuration (with debugging) -->   
  12.   <servlet>   
  13.     <servlet-name>action</servlet-name>   
  14.     <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>   
  15.     <init-param>   
  16.       <param-name>config</param-name>   
  17.       <param-value>/WEB-INF/struts-config.xml</param-value>   
  18.     </init-param>   
  19.     <init-param>   
  20.       <param-name>debug</param-name>   
  21.       <param-value>2</param-value>   
  22.     </init-param>   
  23.     <init-param>   
  24.       <param-name>detail</param-name>   
  25.       <param-value>2</param-value>   
  26.     </init-param>   
  27.     <load-on-startup>2</load-on-startup>   
  28.   </servlet>   
  29.   
  30.   
  31.   <!-- Standard Action Servlet Mapping -->   
  32.   <servlet-mapping>   
  33.     <servlet-name>action</servlet-name>   
  34.     <url-pattern>*.do</url-pattern>   
  35.   </servlet-mapping>   
  36.      
  37.     <context-param>   
  38.         <param-name>contextConfigLocation</param-name>   
  39.         <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>   
  40.      </context-param>   
  41.         
  42.     <listener>   
  43.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
  44.     </listener>   
  45.      
  46.     <filter>   
  47.         <filter-name>hibernateFilter</filter-name>   
  48.         <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>   
  49.     </filter>   
  50.        
  51.     <filter-mapping>   
  52.         <filter-name>hibernateFilter</filter-name>   
  53.         <url-pattern>/*</url-pattern>   
  54.     </filter-mapping>    
  55.      
  56.     <filter>   
  57.         <filter-name>Spring character encoding filter</filter-name>   
  58.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>   
  59.         <init-param>   
  60.             <param-name>encoding</param-name>   
  61.             <param-value>GBK</param-value>   
  62.         </init-param>   
  63.     </filter>   
  64.     <filter-mapping>   
  65.         <filter-name>Spring character encoding filter</filter-name>   
  66.         <url-pattern>/*</url-pattern>   
  67.     </filter-mapping>   
  68.      
  69. </web-app>  
<?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包都要加入哪些,然后我们要知道这个事务管理怎么去配置,然后我们要知道这些文件都要在哪个里面去拷贝.
本文来自:java民工的天堂
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值