`
tiantian911
  • 浏览: 215340 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

SSH框架結構

阅读更多
一個spring2.5+hibernate3.2+struts2.0的組合框架,使用spring的IoC來管理應用的所有bean,包括struts2的action,充分發揮了spring輕量級框架的優勢。


<?xml version="1.0" encoding="utf-8"?>
<web-app id="WebApp_ID" 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">
    <display-name>FashionBookShop</display-name>
   <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
   
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

    <filter>   
      <filter-name>encodingFilter</filter-name>   
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>   
      <init-param>   
          <param-name>encoding</param-name>   
          <param-value>UTF-8</param-value>   
      </init-param>   
  </filter>   
  <filter-mapping>   
      <filter-name>encodingFilter</filter-name>   
      <url-pattern>/*</url-pattern>   
  </filter-mapping>   
  
   
  <filter>   
      <filter-name>struts2</filter-name>   
      <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>   
  </filter>   
  <filter-mapping>   
      <filter-name>struts2</filter-name>   
      <url-pattern>/*</url-pattern>   
  </filter-mapping>  
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
   <resource-ref>
        <res-ref-name>jdbc/MyDataSource</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
</web-app>


在web.xml中使用listener的方式載入spring的web上下文,另外,還載入了struts2的過濾器。

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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
 <bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/MyDataSource"/>
  </bean>

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="mappingResources">
      <list>
        <value>org/tiantian/pojo/Book.hbm.xml</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <value>
        hibernate.dialect=org.hibernate.dialect.MySQLDialect
      </value>
    </property>
  </bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
</bean>
<bean id="bookDao" class="org.tiantian.dao.impl.BookDAOImpl">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
 <bean id="BookAction" scope="prototype"
        class="org.tiantian.struts2.action.BookAction">
        <constructor-arg ref="bookDao" />
    </bean>
</beans>


配置了如下的依賴:sessionFactory->hibernateTemplate->bookDao->BookAction。

這樣整個框架就配置好了,在jsp中,可以使用下面的代碼獲得WebApplicationContext:

WebApplicationContext context = WebApplicationContextUtils. 
getWebApplicationContext(this.getServletContext()); 
BookDAOImpl b=(BookDAOImpl)context.getBean("bookDao");


在servlet或者daoimpl中,可以直接調用bean:

public void removeProduct(int id) {
		Book b = (Book)this.getHibernateTemplate().get(Book.class, id);
		this.getHibernateTemplate().delete(b);
		// TODO Auto-generated method stub

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics