SSH框架的简单实现

 

SSH框架的程序架构

 

框架所需jar文件

项目所需配置文件:

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <beans xmlns="http://www.springframework.org/schema/beans"
  3     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
  4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
  6     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
  7     http://www.springframework.org/schema/aop 
  8     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
  9     http://www.springframework.org/schema/context 
 10     http://www.springframework.org/schema/context/spring-context-3.2.xsd 
 11     http://www.springframework.org/schema/tx 
 12     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
 13 
 14     <!-- 实现注解扫描,即配置此信息cn.houserent包以及其子包下的类中的注解才能生效 -->
 15     <context:component-scan base-package="cn.houserent"></context:component-scan>
 16 
 17     <!-- 由Spring提供数据源信息配置数据源(可以在hibernate配置文件中配置,也可以在Spring中配置) -->
 18     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">    
 19         <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
 20         <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:xe"></property>
 21         <property name="username" value="fang"></property>
 22         <property name="password" value="fang"></property>
 23     </bean>
 24 
 25     <!-- 配置SessionFactory提供Session会话工厂 -->
 26     <!-- 方法三 使用注解时应该使用AnnotationSessionFactoryBean来实现会话工厂 -->
 27     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
 28         <property name="dataSource" ref="dataSource"></property>
 29         <property name="hibernateProperties">
 30             <props>
 31                 <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
 32                 <prop key="show_sql">true</prop>
 33                 <prop key="format_sql">true</prop>
 34                 <prop key="javax.persistence.validation.mode">none</prop>
 35             </props>
 36         </property>
 37         <!-- 配置实体类包,读取实体类包中的注解 -->
 38         <property name="packagesToScan" value="cn.houserent.entity"></property>
 39 
 40         <!-- 方法二 -->
 41         <!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 42         <property name="dataSource" ref="dataSource"></property>
 43         <property name="hibernateProperties">
 44             <props>
 45                 <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
 46                 <prop key="show_sql">true</prop>
 47                 <prop key="format_sql">true</prop>
 48                 <prop key="javax.persistence.validation.mode">none</prop>
 49             </props>
 50         </property>
 51         添加对象关系映射文件(单个文件,逐个配置,比较繁琐)
 52         <property name="mappingResources">
 53             <list>
 54                 <value>cn/houserent/entity/HouseUser.hbm.xml</value>
 55             </list>
 56         </property> -->
 57         <!-- 添加对象关系映射文件所在路径 -->
 58         <!-- <property name="mappingDirectoryLocations">
 59             <list>
 60                 所在包及其子包
 61                 <value>classpath:cn/houserent/entity</value>
 62             </list>
 63         </property> -->
 64         
 65         <!-- 方法一 (由hibernate提供数据源信息来实现SessionFactory)-->
 66         <!--<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 67         <property name="configLocation">
 68             <value>
 69                 classpath:hibernate.cfg.xml
 70             </value>
 71         </property> 
 72         添加对象关系映射文件和上面一样
 73         -->
 74     </bean>
 75 
 76 
 77     <!-- Dao 因为继承了HibernateDaoSupport类,简化代码,必须给sessionFactory属性提供值 -->
 78     <!-- 因为上面实现了注解扫描,这里我们可以使用注解实现 -->
 79     <!-- <bean id="userDao" class="cn.houserent.dao.UserDaoImpl">
 80         <property name="sessionFactory" ref="sessionFactory"></property>
 81     </bean>
 82     
 83     Service
 84     <bean id="userBiz" class="cn.houserent.service.impl.UserBizImpl">
 85         <property name="userDao" ref="userDao"/>        
 86     </bean>
 87     Action    
 88     <bean id="userAction" class="cn.houserent.action.UserAction" scope="session">
 89         <property name="userBiz" ref="userBiz"></property>
 90     </bean> -->
 91     
 92     <!-- 事务管理器,使用事务则必须实现 -->
 93     <bean id="txManager" 
 94         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
 95         <property name="sessionFactory" ref="sessionFactory"/>
 96     </bean>
 97     <!-- 配置事务注解扫描及事务管理器 -->
 98     <tx:annotation-driven transaction-manager="txManager"/>
 99 
100     <!-- <tx:advice id="txAdvice" transaction-manager="txManager">
101         <tx:attributes>
102             <tx:method name="find*" read-only="true"/>
103             <tx:method name="add*" read-only="false" propagation="REQUIRED"/>
104             <tx:method name="*" read-only="true"/>            
105         </tx:attributes>
106     </tx:advice> -->
107     
108     <bean id="logAop" class="cn.houserent.aop.LogAop"/>
109     <!-- 配置AOP(切面)注解,但需要将用于切面的类使用bean声明,如上 -->
110     <aop:aspectj-autoproxy/> 
111 
112     <!-- 定义切面 -->
113     <!-- <aop:config>
114         <aop:pointcut expression="execution(* cn.houserent.service..*.*(..))" id="serviceMethod"/>
115         <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>    
116     </aop:config> -->
117 </beans>
applicationContext.xml
 1 <?xml version='1.0' encoding='UTF-8'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 
 6 <hibernate-configuration>
 7     <session-factory>
 8         <!-- 如果在applicationContext.xml中配置,这里就不需要配置了 -->
 9         <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
10         <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:xe</property>
11         <property name="connection.username">fang</property>
12         <property name="connection.password">fang</property>
13         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
14         <property name="javax.persistence.validation.mode">none</property>
15         <property name="current_session_context_class">thread</property> 
16         <property name="format_sql">true</property>
17         
18         
19         <mapping resource="cn/houserent/entity/HouseUser.hbm.xml" />
20         
21     </session-factory>
22 
23 </hibernate-configuration>
hibernate.cfg.xml
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7     <constant name="struts.enable.DynamicMethodInvocation" value="true" />
 8     <constant name="struts.devMode" value="true" />
 9     <constant name="struts.i18n.encoding" value="utf-8" />    
10     <constant name="struts.multipart.maxSize" value="5000000"/>
11     
12     <package name="default" namespace="/" extends="struts-default">
13         <!-- 这是使用bean声明的形式,使用全类名的话,注解实现就无效 -->
14         <action name="login" class="userAction" method="login">
15             <result>/index.jsp</result>
16             <result name="input">/login.jsp</result>
17         </action>
18            
19     </package>
20 </struts>
struts.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="3.0" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 7   <display-name></display-name>    
 8   <welcome-file-list>
 9     <welcome-file>index.jsp</welcome-file>
10   </welcome-file-list>
11   
12   <!-- 指定Spring配置文件 -->
13   <context-param>
14       <param-name>contextConfigLocation</param-name>
15       <param-value>classpath:applicationContext.xml</param-value>
16   </context-param>
17   
18   <!-- 配置监听器启动Spring -->
19   <listener>
20       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
21   </listener>
22   <listener>
23       <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
24   </listener>
25   <!-- 延迟加载,配置请求会话的实现 -->
26   <filter>
27       <filter-name>OpenSessionInViewFilter</filter-name>
28       <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
29       <init-param>
30           <param-name>sessionFactoryBeanName</param-name>
31           <param-value>sessionFactory</param-value>
32       </init-param>
33   
34   </filter>
35   
36   <filter-mapping>
37       <filter-name>OpenSessionInViewFilter</filter-name>
38       <url-pattern>*.action</url-pattern>
39   </filter-mapping>
40   
41   <!-- 配置Struts2所需的核心过滤器 -->
42   <filter>
43       <filter-name>Struts2</filter-name>
44       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
45   </filter>
46   <filter-mapping>
47       <filter-name>Struts2</filter-name>
48       <url-pattern>/*</url-pattern>
49   </filter-mapping>
50 </web-app>
web.xml

 

 

posted @ 2017-12-22 16:53  沉默-为啥  阅读(198)  评论(0编辑  收藏  举报