⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 useraction.java

📁 使用WEBWORK,SPRING,HIBERNATE编写的简单的添加
💻 JAVA
字号:
/**
 *文件功能: 
 */
package com.tech.action;

import java.net.URLEncoder;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.common.Config;
import com.common.action.BaseAction;
import com.common.hibernate.IdGenerator;
import com.common.util.PaginationSupport;
import com.common.util.StringUtil;
import com.common.util.Valid;
import com.common.dao.DaoHelper;

import com.tech.model.User;
import com.tech.service.UserService;


/**
 * @作者 徐建协
 * @日期 2009-02-25
 */
public class UserAction extends BaseAction {
	private Log log = LogFactory.getLog(UserAction.class);
	private User user;
	private List list;
	private PaginationSupport page;
	private UserService userService;
	private DaoHelper daoHelper;

	public void setDaoHelper(DaoHelper daoHelper) {
		this.daoHelper = daoHelper;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List getList() {
		return list;
	}

	public PaginationSupport getPage() {
		return page;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String add() {
		this.setActionName(Config.ADD);
		try {
			if (user == null) {
				user = new User();
			}
			page = new PaginationSupport(getMap());
			page.setContextPath(this.getContextPath());
			this.setUrlParams(page.getParams());
			return INPUT;
		} catch (Exception ex) {
			log.error("add " + ex.toString());
			if (ex.getMessage() == null) {
				setErrMessage(ex.toString());
			} else {
				setErrMessage(ex.getMessage());
			}
		}
		return LIST;
	}

	public String modify() {
		this.setActionName(Config.MODIFY);
		try {
			if (user == null || user.getId() == null) {
				setErrMessage("修改对象的ID为空!");
				return ERROR;
			}
			setUser(userService.getEntity(getUser().getId()));
			page = new PaginationSupport(getMap());
			page.setContextPath(this.getContextPath());
			this.setUrlParams(page.getParams());
			return INPUT;
		} catch (Exception ex) {
			log.error("modify " + ex.toString());
			if (ex.getMessage() == null) {
				setErrMessage(ex.toString());
			} else {
				setErrMessage(ex.getMessage());
			}
			return LIST;
		}
	}

	public String remove() {
		try {
			userService.removeEntity(user);
			setActionMessage(Config.DELETE_SUCCESS_MESSAGE);
			page = new PaginationSupport(getMap());
			page.setContextPath(this.getContextPath());
			this.setUrlParams(page.getParams() + "&actionMessage="
					+ URLEncoder.encode(Config.DELETE_SUCCESS_MESSAGE, "GBK"));
			return LIST;
		} catch (Exception ex) {
			log.error("remove " + ex.toString());
			if (ex.getMessage() == null) {
				setErrMessage(ex.toString());
			} else {
				setErrMessage(ex.getMessage());
			}

			return LIST;
		}
	}

	public String save() throws Exception {
		try {

			if (getUser().getId() != null) {
				this.setAction(this.ACTION_UPDATE);
				this.setActionName(Config.MODIFY);
				userService.updateEntity(user);
				setActionMessage(Config.MODIFY_SUCCESS_MESSAGE);
				page = new PaginationSupport(getMap());
				page.setContextPath(this.getContextPath());
				this.setUrlParams(page.getParams()
						+ "&actionMessage="
						+ URLEncoder.encode(Config.MODIFY_SUCCESS_MESSAGE,
								"GBK"));
				return LIST;
			} else {
				this.setAction(this.ACTION_ADD);
				this.setActionName(Config.ADD);
				user.setId(new Long(IdGenerator.getId()));
				userService.saveEntity(user);
				setActionMessage(Config.ADD_SUCCESS_MESSAGE);
				page = new PaginationSupport(getMap());
				page.setContextPath(this.getContextPath());
				this.setUrlParams(page.getParams() + "&actionMessage="
						+ URLEncoder.encode(Config.ADD_SUCCESS_MESSAGE, "GBK"));
				return LIST;
			}
		} catch (Exception ex) {
			log.error("save " + ex.toString());
			if (ex.getMessage() == null) {
				setErrMessage(ex.toString());
			} else {
				setErrMessage(ex.getMessage());
			}
			if (this.getAction().equals(this.ACTION_ADD)) {// 当前的操作异常时添加异常
				user.setId(null);
			}
			return INPUT;
		}
	}

	public String detail() throws Exception {
		try {
			setUser(userService.getEntity(getUser().getId()));
			try {
				getUser().getId();
			} catch (Exception ex) {
				throw new Exception("此信息已经不存在,请刷新数据");
			}
			return INPUT;
		} catch (Exception ex) {
			log.error("detail " + ex.toString());
			if (ex.getMessage() == null) {
				setErrMessage(ex.toString());
			} else {
				setErrMessage(ex.getMessage());
			}
			return LIST;
		}
	}

	public String list() {
//		try {
// page = new PaginationSupport(getMap());
// page.setUrl(this.getUrl());
// page.setContextPath(this.getContextPath());
// page.setParam("pageSize","15");
// this.setUrlParams(page.getParams());
// String countSQL="select count(t.id) count from user t where 1=1";
//		
// String countAlias="count";
// String sql="select {t.*} from user t where 1=1 order by t.id desc";
//		
// String sqlAlias="t";
// page=daoHelper.findBySQL(page, countSQL, countAlias, sql, sqlAlias,null,
// User.class, false);
// list=page.getItems();
// this.setRecordCount(page.getTotalCount());
// } catch (Exception ex) {
// log.error("list "+ ex.toString());
// if(ex.getMessage()==null){
// setErrMessage(ex.toString());
// }else{
// setErrMessage(ex.getMessage());
// }
// }
		return this.SUCCESS;
	}



	public String login() throws Exception {

		page = new PaginationSupport(getMap());
		page.setUrl(this.getUrl());
		page.setContextPath(this.getContextPath());
		page.setParam("pageSize", "15");
		this.setUrlParams(page.getParams());
		String countSQL = "select count(t.id) count from user t where 1=1";
		String countAlias = "count";
		String sql = "select {t.*} from user t where 1=1 order by t.id desc";

		String sqlAlias = "t";
		page = daoHelper.findBySQL(page, countSQL, countAlias, sql, sqlAlias,
				null, User.class, false);
		list = page.getItems();
		this.setRecordCount(page.getTotalCount());
		try {
			user  = userService.LoginUser(user);
			this.getSession().setAttribute("user", user);
			
			return SUCCESS;
		} catch (Exception ex) {
			log.error(ex.toString());
			setErrMessage(ex.getMessage());
			return list();
		}
	}
	


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -