package com.yu.znt.dao;public interface IUserDAO { public void register(User user) throws Exception;}
package com.yu.znt.dao.impl;public class IUserDAOImpl extends HibernateDaoSupport implements IUserDAO { public void register(User user) throws Exception{ super.getSession().save(user); }}
在userAction中已经set/get方法
public class UserAction extends DispatchAction { private IUserDAO iuserdao; ... public ActionForward register(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { ... this.iuserdao.register(user); ...}...
其它代码,我只帖重要地方
web.xml
... <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>context</servlet-name> <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <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>...
applicationContext.xml
<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> <!-- 表示允许自动提交 --> <prop key="hibernate.connection.autocommit">true</prop> <!-- 表示显示sql语句 --> <prop key="hibernate.show_sql">false</prop> </props> </property> <property name="mappingResources"> <list> <value>com/yu/znt/model/User.hbm.xml</value> </list> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="iuserdao" class="com.yu.znt.dao.IUserDAO" abstract="true"></bean> <bean id="iuserdaoimpl" class="com.yu.znt.dao.impl.IUserDAOImpl" parent="iuserdao"> <property name="hibernateTemplate"> <ref bean="hibernateTemplate"/> </property> </bean>
struts-config.xml
<form-beans > <form-bean name="userForm" type="com.yu.znt.struts.form.UserForm" /> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings > <action input="/jsp/errors.jsp" name="userForm" parameter="status" path="/jsp/user" scope="request" type="com.yu.znt.struts.action.UserAction"> <forward name="registersuccess" path="jsp/index.jsp"></forward> <forward name="registerfail" path="/jsp/register.jsp"></forward> </action> </action-mappings> <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller> <message-resources parameter="com.yu.znt.struts.ApplicationResources" /> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" /> </plug-in>