一個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="" xmlns:xsi=""
- xsi:schemaLocation=" /web-app_2_4.xsd">
- <display-name>FashionBookShopdisplay-name>
- <context-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>/WEB-INF/applicationContext.xmlparam-value>
- context-param>
-
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
- listener>
-
- <filter>
- <filter-name>encodingFilterfilter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
- <init-param>
- <param-name>encodingparam-name>
- <param-value>UTF-8param-value>
- init-param>
- filter>
- <filter-mapping>
- <filter-name>encodingFilterfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
-
-
- <filter>
- <filter-name>struts2filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcherfilter-class>
- filter>
- <filter-mapping>
- <filter-name>struts2filter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
- <welcome-file-list>
- <welcome-file>index.htmlwelcome-file>
- <welcome-file>index.htmwelcome-file>
- <welcome-file>index.jspwelcome-file>
- <welcome-file>default.htmlwelcome-file>
- <welcome-file>default.htmwelcome-file>
- <welcome-file>default.jspwelcome-file>
- welcome-file-list>
- <resource-ref>
- <res-ref-name>jdbc/MyDataSourceres-ref-name>
- <res-type>javax.sql.DataSourceres-type>
- <res-auth>Containerres-auth>
- <res-sharing-scope>Shareableres-sharing-scope>
- resource-ref>
- web-app>
render_code();在web.xml中使用listener的方式載入spring的web上下文,另外,還載入了struts2的過濾器。
applicationContext.xml文件如下:
代码
- xml version="1.0" encoding="UTF-8"?>
- <beans xmlns=""
- xmlns:xsi=""
- xsi:schemaLocation=" /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.xmlvalue>
- 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>
render_code();配置了如下的依賴:sessionFactory->hibernateTemplate->bookDao->BookAction。
這樣整個框架就配置好了,在jsp中,可以使用下面的代碼獲得WebApplicationContext:
代码
- WebApplicationContext context = WebApplicationContextUtils.
- getWebApplicationContext(this.getServletContext());
- BookDAOImpl b=(BookDAOImpl)context.getBean("bookDao");
render_code();在servlet或者daoimpl中,可以直接調用bean:
代码
- public void removeProduct(int id) {
- Book b = (Book)this.getHibernateTemplate().get(Book.class, id);
- this.getHibernateTemplate().delete(b);
-
-
- }
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/374552/viewspace-6697/,如需转载,请注明出处,否则将追究法律责任。