java web 开发中常用的 框架 就是 ,struts 1 ,hibernate ,spring 整合,
在这里把它贴出来,经常忘记,加深一下印象。
首先配置 基本的 struts 1
web.xml 配置方式如下,先加上这个配置:
下面是spring 与 hibernate,
这个与hibernate 配置 ,细分成了 三个spring文件,看起来更加清晰。
先在web.xml 加入这个配置:
下面分别是 三个spring 配置,,简单的book操作,做个例子。
以上配置完,都是依赖注入。 这些框架的配置 都差不多。主要还是根据实际的业务来 加其他的组件进去。
在这里把它贴出来,经常忘记,加深一下印象。
首先配置 基本的 struts 1
web.xml 配置方式如下,先加上这个配置:
- <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>
- </servlet>
- <servlet-mapping>
- <servlet-name>action</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
这个与hibernate 配置 ,细分成了 三个spring文件,看起来更加清晰。
先在web.xml 加入这个配置:
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- /WEB-INF/config/spring-basic-Context.xml,
- /WEB-INF/config/spring-dao-Context.xml,
- /WEB-INF/config/spring-services-Context.xml
- </param-value>
- </context-param>
下面分别是 三个spring 配置,,简单的book操作,做个例子。
- <!-- spring-basic-Context.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="propertyConfigurer"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>/WEB-INF/config/db.properties</value>
- </list>
- </property>
- </bean>
- <bean id="c3P0dataSource"
- class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="driverClass" value="${DB.DRIVER}" />
- <property name="jdbcUrl" value="${DB.URL}" />
- <property name="user" value="${DB.USERNAME}" />
- <property name="password" value="${DB.PASSWORD}" />
- <property name="initialPoolSize" value="10" />
- <property name="maxPoolSize" value="30" />
- </bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref bean="c3P0dataSource" />
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.MySQLDialect
- </prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.format_sql">true</prop>
- <prop key="hibernate.jdbc.batch_size">25</prop>
- </props>
- </property>
- <property name="mappingResources">
- <list>
- <value>com/birds/book/bean/Book.hbm.xml</value>
- </list>
- </property>
- </bean>
- <bean id="myTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
- <property name="transactionManager" ref="myTxManager" />
- <property name="transactionAttributes">
- <props>
- <prop key="save*">PROPAGATION_REQUIRED,-com.birds.book.common.BookException</prop>
- <prop key="update*">PROPAGATION_REQUIRED</prop>
- <prop key="delete*">PROPAGATION_REQUIRED</prop>
- <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
- </props>
- </property>
- </bean>
- <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <property name="beanNames">
- <value>*Services</value>
- </property>
- <property name="interceptorNames">
- <list>
- <value>transactionInterceptor</value>
- </list>
- </property>
- </bean>
- </beans>
- <!-- spring-dao-Context.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="bookDao" class="com.birds.book.dao.impl.BookDaoImpl">
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
- </beans>
- <!-- spring-services-Context.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="bookServices" class="com.birds.book.services.impl.BookServicesImpl">
- <property name="bookDaoImpl" ref="bookDao" />
- </bean>
- </beans>
- <!-- spring-action-Context.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 name="/book" class="com.birds.view.action.BookAction" scope="prototype">
- <property name="bookServicesImpl" ref="bookServices" />
- </bean>
- </beans>
- <!-- struts-config.xml 配置-->
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
- <struts-config>
- <data-sources />
- <form-beans>
- <form-bean name="bookForm" type="com.birds.view.form.BookForm">
- </form-bean>
- </form-beans>
- <global-exceptions />
- <global-forwards />
- <action-mappings>
- <action path="/book"
- name="bookForm"
- type="org.springframework.web.struts.DelegatingActionProxy"
- scope="request">
- <forward name="success" path="/book.jsp" />
- </action>
- </action-mappings>
- <plug-in
- className="org.springframework.web.struts.ContextLoaderPlugIn">
- <set-property property="contextConfigLocation"
- value="/WEB-INF/config/spring-action-Context.xml" />
- </plug-in>
- </struts-config>
以上配置完,都是依赖注入。 这些框架的配置 都差不多。主要还是根据实际的业务来 加其他的组件进去。
本文介绍了一个JavaWeb项目的框架整合过程,包括Struts1、Spring和Hibernate的配置示例。通过详细的步骤展示了如何设置这些框架以实现一个简单的图书管理应用。

3053

被折叠的 条评论
为什么被折叠?



