ssh框架 使用spring的注解以及hibernate的正向生成,做一个注册的例子
1.导入spring,struts,hibernate的jar包
2.项 目 的 结 构
3 web.xml的配置的核心代码
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<!-- 配置struts -->
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts- plugin.xml,struts.xml</param-value>
</init-param>
</filter>
<!-- spring容器的初始化 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 指定加载的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*: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>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
4.applicationContext.xml的配置文件
<!-- 头文件 --!>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 开启spring注解方式 -->
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.huang.ssh">
</context:component-scan>
<!-- 配置数据集 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property
name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/test">
</property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- 配置sessionFactory -->
<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.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 加载实体类的包名 -->
<property name="packagesToScan" value="com.huang.ssh.entity"></property>
</bean>
<!-- 配置事务管理器,使用spring来管理hibernate的事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<aop:config>
<!-- 需要进行事务管理的方法 -->
<aop:pointcut expression="execution(* com.huang.ssh.biz.*.*(..))"
id="daoMethods" />
<aop:advisor advice-ref="transcationAdvice" pointcut-ref="daoMethods" />
</aop:config>
<tx:advice id="transcationAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
5.实体类User
6. BaseDao类的代码
7. UserDao类的代码
8.UserServlte类的代码
9 Add.action类的代码
10.struts.xml的配置文件
11.一个注册的index.jspyem
12.注册页面
13.结果