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

📄 useraction.java

📁 ssh整合在一起的例子.适合初学者,希望对大家能有一定的帮助作用
💻 JAVA
字号:
package com.softdevelopstudio.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.softdevelopstudio.domain.User;
import com.softdevelopstudio.struts.form.UserActionForm;
import com.softdevelopstudio.util.SystemUtil;

public class UserAction extends BaseAction {
	private static final Logger LOG = Logger.getLogger(UserAction.class);

	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("toAdd");
	}

	public ActionForward doAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UserActionForm userForm = (UserActionForm) form;
		User user = new User();
		BeanUtils.copyProperties(user, userForm);
		this.getUserService().getUserDAO().save(user);
		return toList(mapping, form, request, response);
	}

	public ActionForward toList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UserActionForm empActionForm = (UserActionForm) form;

		// 处理每页显示多少条的参数
		int perpage = 0;
		String strPerpage = request.getParameter("perpage");
		if (strPerpage == null || strPerpage.equals("")) {
			strPerpage = "5";
		}
		perpage = Integer.parseInt(strPerpage);
		request.setAttribute("perpage", new Integer(perpage));

		// 处理当前第几页的参数
		String strCurrent = request.getParameter("page");
		if (strCurrent == null || strCurrent.equals("")) {
			strCurrent = "1";
		}
		int current = Integer.parseInt(strCurrent);
		request.setAttribute("current", new Integer(current));

		// 得到搜索的条件
		String strCondition = this.getSearchCondition(request);
		String hql = "from User";
		if (SystemUtil.isNotNull(strCondition)) {
			hql += strCondition;
		}
		System.out.println("hql: " + hql);

		// 得到总数量并将其放入request中
		int total = this.getUserService().getUserDAO().count(strCondition);
		request.setAttribute("total", new Integer(total));
		request.setAttribute("strCondition", strCondition);
		List userList = this.getUserService().getUserDAO().findPaginatedList(
				hql, perpage, current - 1);
		request.setAttribute("users", userList);
		return mapping.findForward("toList");
	}

	public ActionForward toView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String strId = "" + request.getParameter("id");
		Integer id = new Integer(strId);
		User user = this.getUserService().getUserDAO().findById(id);
		request.setAttribute("user", user);

		return mapping.findForward("toView");
	}

	public ActionForward doDelete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String strId = "" + request.getParameter("id");
		Integer id = new Integer(strId);
		User user = this.getUserService().getUserDAO().findById(id);
		this.getUserService().getUserDAO().delete(user);
		return this.toList(mapping, form, request, response);
	}

	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String strId = "" + request.getParameter("id");
		Integer id = new Integer(strId);
		User user = this.getUserService().getUserDAO().findById(id);
		request.setAttribute("user", user);
		return mapping.findForward("toUpdate");
	}

	public ActionForward doUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UserActionForm userActionForm = (UserActionForm) form;
		User user = new User();
		BeanUtils.copyProperties(user, userActionForm);
		this.getUserService().getUserDAO().attachDirty(user);

		return toList(mapping, form, request, response);
	}
}

⌨️ 快捷键说明

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