SSH框架的簡單實現

 

SSH框架的程序架構java

 

框架所需jar文件web

項目所需配置文件:spring

  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
相關文章
相關標籤/搜索