實體類 package ecen.user.model; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="src_user") public class SrcUser implements Serializable{ private String srcUserId; private String srcUserName; private String srcUserPwd; @Id @GeneratedValue(generator="system-uuid") @GenericGenerator(name="system-uuid", strategy = "uuid") @Column(name = "src_user_id") public String getSrcUserId() { return srcUserId; } public void setSrcUserId(String srcUserId) { this.srcUserId = srcUserId; } @Column(name="src_user_name") public String getSrcUserName() { return srcUserName; } public void setSrcUserName(String srcUserName) { this.srcUserName = srcUserName; } @Column(name="src_user_pwd") public String getSrcUserPwd() { return srcUserPwd; } public void setSrcUserPwd(String srcUserPwd) { this.srcUserPwd = srcUserPwd; } @Override public String toString(){ return "id:"+srcUserId+";name:"+srcUserName+";pwd:"+srcUserPwd; } } 通用的Dao接口 package ecen.user.dao; import java.io.Serializable; import java.util.List; import ecen.user.model.SrcUser; public interface SrcUserDao extends Serializable{ public <T> List<T> findList(String sql) throws Exception; public <T> T findModelById(Class cla, Serializable id) throws Exception; public boolean saveOrUpdateModel(Object obj) throws Exception; public boolean deleteModel(Class cla, Serializable id) throws Exception; public SrcUser findUserByNameAndPwd(String name,String pwd)throws Exception; } dao的實現類 package ecen.user.dao.impl; import java.io.Serializable; import java.util.List; import javax.annotation.Resource; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import ecen.user.dao.SrcUserDao; import ecen.user.model.SrcUser; @Component("userDao") public class SrcUserDaoImpl implements SrcUserDao{ private HibernateTemplate hibernateTemplate; public <T> List<T> findList(String sql) throws Exception { return hibernateTemplate.find(sql); } public <T> T findModelById(Class cla, Serializable id) throws Exception { return (T) hibernateTemplate.get(cla, id); } public boolean saveOrUpdateModel(Object obj) throws Exception { try { hibernateTemplate.saveOrUpdate(obj); return true; } catch (Exception e) { return false; } } public boolean deleteModel(Class cla, Serializable id) throws Exception { try { hibernateTemplate.delete(hibernateTemplate.get(cla, id)); return true; } catch (Exception e) { return false; } } public SrcUser findUserByNameAndPwd(String name, String pwd) throws Exception { return (SrcUser) hibernateTemplate.find("from SrcUser where srcUserName=? and srcUserPwd=?",name,pwd).get(0); } public HibernateTemplate getHibernateTemplate() { return hibernateTemplate; } @Resource public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } } 業務處理的service的接口 package ecen.user.service; import java.io.Serializable; import java.util.List; import ecen.user.model.SrcUser; public interface SrcUserService extends Serializable{ public List<SrcUser> findList() throws Exception; public SrcUser findModelById(Class cla, String id) throws Exception; public boolean saveOrUpdateModel(SrcUser user) throws Exception; public boolean deleteModel(Class cla, Serializable id) throws Exception; public SrcUser findUserByNameAndPwd(SrcUser user)throws Exception; } 業務處理的實現類 package ecen.user.service.impl; import java.io.Serializable; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import ecen.user.dao.SrcUserDao; import ecen.user.model.SrcUser; import ecen.user.service.SrcUserService; @Component("userService") public class SrcUserSerivceImpl implements SrcUserService{ private SrcUserDao userDao; public List<SrcUser> findList() throws Exception { return userDao.findList("from SrcUser"); } public SrcUser findModelById(Class cla, String id) throws Exception { return userDao.findModelById(cla, id); } public boolean saveOrUpdateModel(SrcUser user) throws Exception { return userDao.saveOrUpdateModel(user); } public boolean deleteModel(Class cla, Serializable id) throws Exception { return userDao.deleteModel(cla, id); } public SrcUser findUserByNameAndPwd(SrcUser user) throws Exception { return userDao.findUserByNameAndPwd(user.getSrcUserName(), user.getSrcUserPwd()); } public SrcUserDao getUserDao() { return userDao; } @Resource public void setUserDao(SrcUserDao userDao) { this.userDao = userDao; } } 全部Action的父類 package ecen.user.action; import java.util.Enumeration; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class BaseAction extends ActionSupport implements SessionAware{ private static final long serialVersionUID = 1L; protected HttpServletRequest getRequest() { return ServletActionContext.getRequest(); } protected HttpServletResponse getResponse() { return ServletActionContext.getResponse(); } protected HttpSession getSession() { return getRequest().getSession(); } //獲取session值 protected Object getSessionValue(String key) { HttpSession session = getSession(); return session.getAttribute(key); } //設置session值 protected void setSessionValue(String key, Object value) { HttpSession session = getSession(); session.setAttribute(key, value); } //刪除指定的session值 protected void removeSessionValue(String key) { HttpSession session = getSession(); Enumeration<String> keys = session.getAttributeNames(); while(keys.hasMoreElements()) { String xkey = keys.nextElement(); if(xkey.equals(key)) { session.removeAttribute(key); return; } } } public void setSession(Map<String, Object> arg0) { // TODO Auto-generated method stub } } 具體的Action package ecen.user.action; import java.util.List; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import ecen.user.model.SrcUser; import ecen.user.service.SrcUserService; @Component("userAction") @Scope("prototype") public class SrcUserAction extends BaseAction{ private static final long serialVersionUID = 1L; private SrcUserService userService; private SrcUser user; private String id; private List<SrcUser> userList; public String login()throws Exception{ SrcUser model=userService.findUserByNameAndPwd(user); if(model !=null){ this.getUserlist(); } return "success"; } private void getUserlist() throws Exception{ userList=userService.findList(); } public String edit()throws Exception{ userService.saveOrUpdateModel(user); this.getUserlist(); return "success"; } public String findById()throws Exception{ user=userService.findModelById(SrcUser.class, id); return "edit"; } public String delete()throws Exception{ userService.deleteModel(SrcUser.class, id); this.getUserlist(); return "success"; } public SrcUserService getUserService() { return userService; } @Resource public void setUserService(SrcUserService userService) { this.userService = userService; } public SrcUser getUser() { return user; } public void setUser(SrcUser user) { this.user = user; } public String getId() { return id; } public void setId(String id) { this.id = id; } public List<SrcUser> getUserList() { return userList; } public void setUserList(List<SrcUser> userList) { this.userList = userList; } } appliactionContext.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: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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="ecen.user" /> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>ecen.user.model</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect </prop> <prop key="hibernate.show_sql">false</prop> <prop key="format_sql">true</prop> </props> </property> </bean> <!-- 讓spring管理sessionFactory的加一種方法 ,就是不刪除hibernate.cfg.xml文件; <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <aop:config> <aop:pointcut id="bussinessService" expression="execution(public * ecen.user.service.*.*(..))" /> <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="*" /> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="edit*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> </beans> struts.xml配置 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="user" extends="struts-default"> <action name="userAction" class="userAction"> <result name="success">/user/user_list.jsp</result> <result name="edit">/user/user_edit.jsp</result> </action> </package> </struts> web.xml的配置文件 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <context-param> <param-name> contextConfigLocation </param-name> <param-value> classpath*:applicationContext.xml </param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <context-param> <param-name>log4jExposeWebAppRoot</param-name> <param-value>false</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <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> </filter> <filter-mapping> <filter-name> encodingFilter </filter-name> <url-pattern> /* </url-pattern> </filter-mapping> <filter> <filter-name> openSessionInView </filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> </filter> <filter-mapping> <filter-name> openSessionInView </filter-name> <url-pattern> /* </url-pattern> </filter-mapping> <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> <welcome-file-list> <welcome-file> user/user_login.jsp </welcome-file> </welcome-file-list> </web-app>