web.xml 中内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 配置spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Spring配置文件 applicationContext.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"
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="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/ssh"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 配置session工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<!-- 指定bean -->
<property name="mappingResources">
<list>
<value>org/chq/ssh/entity/UserBean.hbm.xml</value>
</list>
</property>
</bean>
<!--
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="/WEB-INF/hibernate.cfg.xml">
</property> </bean>
-->
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 配置事物的传播特性 -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="upd*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 那些类的哪些方法参与事物 -->
<aop:config>
<aop:pointcut id="alldao" expression="execution(* dao.imp.*.*(..))" />
<aop:advisor pointcut-ref="alldao" advice-ref="txadvice" />
</aop:config>
<!-- 目标对象 -->
<bean id=" userDaoImp " class="org.chq.ssh.dao.imp.UserDaoImp">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!--配置action -->
<bean name="/register" class="org.chq.ssh.struts.action.RegisterAction">
<property name=" iudDao ">
<ref bean=" userDaoImp " />
</property>
</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="registerForm" type="org.chq.ssh.struts.form.RegisterForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action attribute="register" input="/form/register.jsp" name="registerForm"
parameter="method" path="/register" scope="request"
type="org.chq.ssh.struts.action.RegisterAction">
<set-property property="cancellable" value="true" />
<forward name="fail" path="error.jsp" />
<forward name="success" path="/form/index.jsp" />
</action>
</action-mappings>
<!-- 请求委托 -->
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
<!-- 本行自动生成的必须放在请求委托和插件spring管理action中间 -->
<message-resources parameter="org.chq.ssh.struts.ApplicationResources" />
<!-- 插件 spring管理action -->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext*.xml" />
</plug-in>
</struts-config>
sql
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
strut
package org.chq.ssh.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.chq.ssh.dao.IUserDao;
import org.chq.ssh.struts.form.RegisterForm;
import org.chq.ssh.entity.UserBean;
public class RegisterAction extends DispatchAction {
private IUserDao iudDao ;
// 增加
public ActionForward addUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
RegisterForm registerForm = (RegisterForm) form;
String name = registerForm.getName();
String password = registerForm.getPassword();
System.out.println(name +"-dsd-"+ password + "-111111");
UserBean user = new UserBean();
user.setName(name);
user.setPassword(password);
System.out.println(user.getName()+"----------"+"/n");
iudDao.addUser(user);
return mapping.findForward("success");
}
public void setIudDao(IUserDao iudDao) {
this.iudDao = iudDao;
}
}
dao
package org.chq.ssh.dao;
import org.chq.ssh.entity.UserBean;
import org.hibernate.ejb.HibernateEntityManager;
public interface IUserDao {
// 增加
public void addUser(UserBean user);
}
imp
package org.chq.ssh.dao.imp;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.chq.ssh.dao.IUserDao;
import org.chq.ssh.entity.UserBean;
public class UserDaoImp extends HibernateDaoSupport implements IUserDao {
// 增加
public void addUser(UserBean user) {
this.getHibernateTemplate().save(user);
}
}