beans.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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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">
<context:annotation-config />
<context:component-scan base-package="demo" />
<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="sf" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- <property name="annotatedClasses">
<list><value>com.bjsxt.model.User</value><value>com.bjsxt.model.Log</value></list>
</property> -->
<property name="packagesToScan">
<list>
<value>demo.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sf"></property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sf" />
</bean>
<aop:config>
<aop:pointcut id="bussinessService"
expression="execution(public * demo.service.*.*(..))" />
<aop:advisor pointcut-ref="bussinessService"
advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="exists" read-only="true" />
<tx:method name="add*" 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.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="registration" extends="struts-default">
<action name="u" class="u">
<result name="success">/registerSuccess.jsp</result>
<result name="fail">/registerFail.jsp</result>
<result name="list">/userlist.jsp</result>
<result name="load">/user.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
<!-- default: /WEB-INF/applicationContext.xml -->
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> -->
<param-value>classpath:beans.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>GBK</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>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sf</param-value>
</init-param>
</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>
</web-app>
UserAction.java
package demo.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import demo.model.User;
import demo.service.UserManager;
import demo.vo.UserRegisterInfo;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Component("u")
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven {
private UserRegisterInfo info = new UserRegisterInfo();
private UserManager userManager;
private List<User> users;
private User user;
public UserAction(){
System.out.println("useraction created!");
}
@Override
public String execute() throws Exception {
User u = new User();
u.setUsername("aa");
u.setPassword("aa");
if(userManager.exists(u)) {
return "fail";
}
userManager.add(u);
if(userManager.exists(u)) {
System.out.println("00000000000000000");
return "fail";
}
System.out.println("111111111111111");
return "success";
}
public UserRegisterInfo getInfo() {
return info;
}
public void setInfo(UserRegisterInfo info) {
this.info = info;
}
//@Override
public Object getModel() {
return info;
}
public String list() {
this.users = this.userManager.getUsers();
return "list";
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public String load() {
this.user = this.userManager.loadById(info.getId());
return "load";
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserManager getUserManager() {
return userManager;
}
@Resource
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
}
UserDaoImpl.java
package demo.dao.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import demo.dao.UserDao;
import demo.model.User;
@Component("userDao")
public class UserDaoImpl implements UserDao {
private HibernateTemplate hibernateTemplate;
public void save(User u) {
hibernateTemplate.save(u);
}
public boolean checkUserExistsWithName(String username) {
List<User> users = hibernateTemplate.find("from User u where u.username = '" + username + "'");
if(users != null && users.size() > 0) {
return true;
}
return false;
/*long count = (Long)hibernateTemplate.getSessionFactory()
.getCurrentSession().createQuery("select count(*) from User u where u.username = :username")
.setString("username", username).uniqueResult();
if(count > 0) return true;
return false;*/
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public List<User> getUsers() {
// TODO Auto-generated method stub
return (List<User>)this.hibernateTemplate.find("from User");
}
public User loadById(int id) {
return (User)this.hibernateTemplate.load(User.class, id);
}
}
User.java
package demo.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class User {
private int id;
private String username;
private String password;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserManagerImpl.java
package demo.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import demo.dao.UserDao;
import demo.model.User;
import demo.service.UserManager;
@Component("userManager")
public class UserManagerImpl implements UserManager {
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
@Resource
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public boolean exists(User u) throws Exception {
return userDao.checkUserExistsWithName(u.getUsername());
}
public void add(User u) throws Exception {
userDao.save(u);
}
public List<User> getUsers() {
return this.userDao.getUsers();
}
public User loadById(int id) {
return this.userDao.loadById(id);
}
}
HibernateUtil.java
package demo.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static SessionFactory sf;
static {
sf = new AnnotationConfiguration().configure().buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return sf;
}
}