【ActionForm】:
package com.eshop.forms;
import org.apache.struts.action.ActionForm;
/**
* 用户ActionForm,定义用户类的各个私有属性
* 为各个属性提供get和set方法
* @author qianjunsheng
*
*/
public class User extends ActionForm {
private int id;//用户id编号,数据库自动生成
private String username;//用户昵称
private String password;//用户密码
private String rePassword;//用户确认密码
private String passwordQuestion;//密码保密问题
private String passwordAnswer;//密码保密答案
private String realName;//真实姓名
private String telphone;//电话
private String address;//送货地址
private String email;//E-mail地址
private String cellPhone;//手机号码
private String postNo;//邮政编码
private String idCardNo;//用户身份证号码
//以下为各个属性的Getter和Setter方法
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;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRePassword() {
return rePassword;
}
public void setRePassword(String rePassword) {
this.rePassword = rePassword;
}
public String getPasswordQuestion() {
return passwordQuestion;
}
public void setPasswordQuestion(String passwordQuestion) {
this.passwordQuestion = passwordQuestion;
}
public String getPasswordAnswer() {
return passwordAnswer;
}
public void setPasswordAnswer(String passwordAnswer) {
this.passwordAnswer = passwordAnswer;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getTelphone() {
return telphone;
}
public void setTelphone(String telphone) {
this.telphone = telphone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getCellPhone() {
return cellPhone;
}
public void setCellPhone(String cellPhone) {
this.cellPhone = cellPhone;
}
public String getPostNo() {
return postNo;
}
public void setPostNo(String postNo) {
this.postNo = postNo;
}
public String getIdCardNo() {
return idCardNo;
}
public void setIdCardNo(String idCardNo) {
this.idCardNo = idCardNo;
}
}
【Action】:
package com.eshop.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.eshop.forms.User;
import com.eshop.managers.UserManager;
import com.eshop.managers.UserManagerImpl;
/**
* 与用户ActionForm对应的Action,继承Struts的Action类
* 重写父类的Execute方法
* 为UserManagerImpl提供setter方法,为Spring注入做准备
* @author qianjunsheng
*
*/
public class AddUserAction extends Action {
private UserManager userManager;
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String action = request.getParameter("action");
User user = (User)form;
userManager.addUser(user);
return mapping.findForward("success");
}
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
}
package com.eshop.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.eshop.forms.User;
import com.eshop.managers.UserManager;
public class LoginUserAction extends Action {
private UserManager userManager;
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String action = request.getParameter("action");
User user = (User)form;
String username =user.getUsername();
String password = user.getPassword();
userManager.getUserByUsernameAndPassword(username, password);
return mapping.findForward("success");
}
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
}
【Dao】:
package com.eshop.dao;
import java.util.ArrayList;
import java.util.List;
import com.eshop.forms.User;
public interface UserDao {
/*
* 添加用户
*/
public void addUser(User user);
/*
* 更新用户
*/
public void updateUser(User user);
/*
* 删除用户,通过id
*/
public void deleteUserById(int id);
/*
* 通过id数组,调用getUserById()方法,获取所有用户对象
*/
public void deleteUsersByIds(int[] ids);
/*
* 通过id获取用户信息
*
*/
public User getUserById(int id);
/*
* 通过UserActionForm实例获取用户信息
* 直接返回该UserActionForm对象的实例
*/
public User getUserByUsernameAndPassword(String username,String password);
/*
* 获取所有用户信息
* 存放到List里,返回该List
*
*/
public List getUsers();
}
package com.eshop.dao;
import java.util.ArrayList;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.eshop.forms.User;
/**
* Dao层
* 继承HibernateDaoSupport父类
* 采用getHibernateTemplate()的save()、find()、delete()等方法
* 实现对数据库的各种操作
* @author qianjunsheng
*
*/
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
public static UserDaoImpl userDaoImpl;
public static UserDaoImpl getInstance(){
if(userDaoImpl == null){
userDaoImpl = new UserDaoImpl();
}
return userDaoImpl;
}
/*
* 添加用户
*/
public void addUser(User user){
this.getHibernateTemplate().save(user);
}
/*
* 更新用户
*/
public void updateUser(User user){
this.getHibernateTemplate().update(user);
}
/*
* 删除用户,通过id
*/
public void deleteUserById(int id){
this.getHibernateTemplate().delete(this.getUserById(id));
}
/*
* 通过id数组,调用getUserById()方法,获取所有用户对象
* 把所有对象封装在一个List里,调用getHibernateTemplate().deleteAll()方法
* 删除list中所有用户
*/
public void deleteUsersByIds(int[] ids){
List <User> userlist = new ArrayList<User>();
for(int i=0; i<ids.length;i++){
User user = this.getUserById(ids[i]);
userlist.add(user);
}
this.getHibernateTemplate().deleteAll(userlist);
}
/*
* 通过id获取用户信息
* 通过getHibernateTemplate().find()方法,获取查询的list
* 再调用该list的get(index)方法,强制转换,获取UserActionForm对象
*/
public User getUserById(int id){
return (User)this.getHibernateTemplate()
.find("from UserActionForm where id like ? ", id).get(0);
}
/*
* 通过UserActionForm实例获取用户信息
* 直接返回该UserActionForm对象的实例
*/
public User getUserByUsernameAndPassword(String username,String password){
return (User)this.getHibernateTemplate().find("from UserActionForm where username like ? and password like ?",
new String[]{username,password}).get(0);
}
/*
* 获取所有用户信息
* 存放到List里,返回该List
*
*/
public List getUsers(){
return this.getHibernateTemplate().find("from UserActionForm");
}
}
【service】:
package com.eshop.managers;
import java.util.List;
import com.eshop.forms.User;
/**
* UserManager接口
* 定义用户操作的各个方法,在子类中实现
* @author qianjunsheng
*
*/
public interface UserManager {
public void addUser(User user);//添加用户
public void updateUser(User user);//更新用户
public void deleteUserById(int id);//删除用户,通过id
public void deleteUsersByIds(int[] ids);//通过id数组,删除一组用户
public User getUserById(int id);//通过id获取用户信息
public User getUserByUsernameAndPassword(String username,String password);//获取用户信息
public List<User> getUsers();//获取所有用户信息,返回用户信息List
}
package com.eshop.managers;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.eshop.dao.UserDao;
import com.eshop.forms.User;
/**
* 对UserManager接口方法的具体实现
* 调用Dao层方法
* 为UserDao提供setter方法,为Spring注入提供准备
* @author qianjunsheng
*
*/
public class UserManagerImpl implements UserManager {
/*
* 对UserDao的引用,提供Setter方法,用Spring注入
*/
public UserDao userDao;
/*
* 通过用户的id号,删除用户
* @see com.eshop.managers.UserManager#deleteUserById(int)
*/
@Override
public void deleteUserById(int id) {
userDao.deleteUserById(id);
}
/*
* 通过整型id数组,删除一组用户
* @see com.eshop.managers.UserManager#deleteUsersByIds(int[])
*/
@Override
public void deleteUsersByIds(int[] ids) {
userDao.deleteUsersByIds(ids);
}
/*
* 通过用户id号,读取用户信息
* @see com.eshop.managers.UserManager#getUserById(int)
*/
@Override
public User getUserById(int id) {
return userDao.getUserById(id);
}
/*
* 通过用户名和密码,查询该用户
* 返回该实例
* @see com.eshop.managers.UserManager#getUserByUser(com.eshop.forms.UserActionForm)
*/
@Override
public User getUserByUsernameAndPassword(String username,String password) {
return userDao.getUserByUsernameAndPassword(username,password);
}
/*
* 获取所有的用户
*返回UserActionForm类型的List,
* @see com.eshop.managers.UserManager#getUsers()
*/
@Override
public List<User> getUsers() {
List<User> userlist = new ArrayList<User>();
List list = userDao.getUsers();
for(Iterator iter = list.iterator(); iter.hasNext();){
User user = (User) iter.next();
userlist.add(user);
}
return userlist;
}
/*
* 添加新用户或用户注册
* @see com.eshop.managers.UserManager#addUser(com.eshop.forms.UserActionForm)
*/
@Override
public void addUser(User user) {
userDao.addUser(user);
}
/*
* 通过User对象的实例,更新User信息
* @see com.eshop.managers.UserManager#updateUser(com.eshop.forms.UserActionForm)
*/
@Override
public void updateUser(User user) {
userDao.updateUser(user);
}
/*
* Dao的Setter方法,为Spring提供注入准备
*/
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}