netbeans6.1环境下struts2+spring2.5+hibernate3.2集成

xiaoxiao2022-06-13  44

这三个框架是最流行的框架,这三个版本是比较新的版本,他们的组合是现在做项目完美的解决方案。 [b][color=red]纯粹做过备忘录[/color][/b] 1、三个框架的结合: 由于spring在内部已经集成了hibernate,所以他俩的组合很容易,基本不用怎么管,只是需要加一些.jar文件,在后面我会说到。而spring和struts的组合稍微麻烦一点, 首先要明白,struts主要解决的是图层方面的功能,所以要在web.xml添加过滤器struts2 <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> 所有模式为/*的URL都首先经过struts2的处理, 其次,由于要用到spring的IOC所以要添加spring的监听器设置如下: <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 默认它会在/WEB-INF下找applicationContext.xml,如果你没有把applicationContext.xml放到这个位置就要在web.xml里设置她的位置: <context-param> <param-name>contextConfigLocation</param-name> <param-value>/路径/applicationContext.xml</param-value> </context-param> 三个框架的结合基本完成了。 2、数据库的配置: 首先在spring的配置文件applicationContext.xml里建立数据源,例如数据库是oracle: <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" /> <property name="username" value="system" /> <property name="password" value="orcl" /> </bean> 其次要配置会话工厂: <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect </prop> <prop key="hibernate.show_sql">true </prop> </props> </property> <property name="annotatedClasses"> <list> <value>facility.DynamicFacilityTable</value> </list> </property> </bean> 由于采用hibernate的注解功能所以用的类为:AnnotationSessionFactoryBean 所以属性annotatedClasses的值直接写实体类(要加上路径),这也比以前要用映射配置文件简单了。 随后配置hibernateTempllate <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> 配置好模板后,以后调用的时候直接注入就可以了。在netbeans里有自动生成实体类的功能,如果在数据库里已经建好相应的表,这是很方面的啊O(∩_∩)O。 3、关于编码过滤的配置: 在开发软件的过程中经常出现中文显示乱码问题,为了统一解决,需要在配置里完成相应的设置。 首先,在web.xml里加一个编码过滤器: <filter> <filter-name>encodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 这采用的是spring的编码过滤器,过滤所有url为/*的模式 其次还要在struts.xml里加上: <constant name="struts.i18n.encoding" value="GBK"/> 这样所有的URL采用的都是GBK编码了,在程序中就不用再为编码问题而头疼了。 最后把完整的配置文件贴出来: web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <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> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <filter> <filter-name>encodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> applicationContex.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" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!--bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="/WEB-INF/jdbc.properties" /> /--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" /> <property name="username" value="system" /> <property name="password" value="****" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect </prop> <prop key="hibernate.show_sql">true </prop> </props> </property> <!-- spring2.5.6 可以直接 整个包搜索读取 <property name="packagesToScan" value="org.eline.entity*.*" /> --> <property name="annotatedClasses"> <list> <value>facility.DynamicFacilityTable</value> </list> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> </beans> struts.xml 采用netbeans自动生产的就可以: <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <include file="example.xml"/> <!-- Configuration for the default package. --> <constant name="struts.i18n.encoding" value="GBK"/> <constant name="struts.objectFactory" value="spring"/> <package name="default" extends="struts-default"> <action name="login"> <result>success.jsp</result> </action> </package> </struts> 差一点忘了,在netbeans里默认加入spring2.5和struts2的包还不够还要加一下jar文件: struts2-spring-plugin-2.0.6.jar spring和struts结合的插件 classes12.jar oracle的驱动程序 ejb3-persistence.jar 对实体类进行注解所需的包文件 hibernate-annotations.jar 对hibernate的注解的支持 hibernate-commons-annotations.jar 对hibernate的注解的支持 搞了好长时间才搭好这个框架的集成,中间历尽千辛,个中滋味开发程序的应该都可以理解……
转载请注明原文地址: https://www.6miu.com/read-4936222.html

最新回复(0)