@(SPRing)[Spring, hibernate, struts2, 框架整合]
SSH框架整合基于注解SSH框架整合第一步導入Jar包第二步導入配置文件第三步創建相關的包和類第四步創建界面第五步配置組件注解掃描第六步配置Action注解第七步配置業務層接口和實現類第八步配置實體類和映射注解第九步配置會話工廠第十步配置Hibernate模板第十一步配置事務管理器和事務注解掃描第十二步配置持久層接口和實現類配置OpensessionInView過濾器配置全站編碼過濾器
導入的jar包在《SSH框架整合——基于xml配置文件》博文中都有所介紹,這里不再贅述。唯一不同的是,基于注解的SSH整合還需要導入Struts2注解整合包struts2-convention-plugin-x.x.x.jar。
db.propertiesjdbc.driverClass=com.MySQL.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/testjdbc.user=rootjdbc.passWord=123456applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置外部屬性持有對象 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置c3p0數據源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean></beans>web.xml<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> <!-- 配置Struts2核心過濾器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置Spring核心監聽器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener></web-app>log4j.properties### direct log messages to stdout ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.errlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### direct messages to file mylog.log ###log4j.appender.file=org.apache.log4j.FileAppenderlog4j.appender.file.File=c/:mylog.loglog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### set log levels - for more verbose logging change 'info' to 'debug' ###log4j.rootLogger=info, stdoutapplicationContext.xml中添加<!-- 開啟注解掃描 --><context:component-scan base-package="com.pc.crm.web.action"/><context:component-scan base-package="com.pc.crm.*.impl"/>applicationContext.xml中添加<!-- 配置Hibernate的SessionFactory,無hibernate配置文件 --><bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 配置數據源 --> <property name="dataSource" ref="dataSource"/> <!-- 配置hibernate屬性--> <property name="hibernateProperties"> <props> <!-- 數據庫的方言 --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!-- 是否顯示SQL語句 --> <prop key="hibernate.show_sql">true</prop> <!-- 是否格式化顯示SQL語句 --> <!-- <prop key="hibernate.format_sql">true</prop> --> <!-- 采用何種策略來創建表結構: --> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 配置實體注解掃描 --> <property name="packagesToScan"> <list> <value>com.pc.crm.domain</value> </list> </property></bean>applicationContext.xml中添加<!-- 配置hibernate模板 --><bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"/></bean>applicationContext.xml中添加<!-- 配置事務管理器 --><bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/></bean><!-- 配置事務注解掃描 --><tx:annotation-driven transaction-manager="transactionManager"/>web.xml中添加,注意要放在Struts2核心過濾器上面<!-- 配置OpenSessionInview過濾器 --><filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class></filter><filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>*.action</url-pattern></filter-mapping>web.xml中添加,注意要放在Struts2核心過濾器上面<!-- 配置Spring的字符編碼過濾器 --><filter> <filter-name>CharacterEncodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param></filter><filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping>新聞熱點
疑難解答