📄 roleaction.java
字号:
package com.qrsx.qrsxcrm.action;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.qrsx.qrsxcrm.dao.RoleDAO;
import com.qrsx.qrsxcrm.model.Role;
import com.qrsx.qrsxcrm.web.Pager;
public class RoleAction extends BaseDispatchAction {
/**
* 检索角色信息,进入列表页面
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
@SuppressWarnings("unchecked")
public ActionForward list(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IllegalAccessException, InvocationTargetException {
//
// HttpSession session = request.getSession();
// String flag = (String) session.getAttribute("wrong");
// if( flag != null && ! flag.equals(""))
// {
// request.setAttribute("sorry", "数据已被使用,请释放后再删除!");
// }
// session.setAttribute("wrong", "");
Role role=new Role();
BeanUtils.copyProperties(role,form);
try {
Pager pager = null;
RoleDAO rdao = new RoleDAO(Role.class);
List results = rdao.findAll("from Role");//得到总数据
pager = new Pager(); // 构造分页对象
int totalRows = results.size(); // 得到总数据量
pager.init(totalRows);
if (request.getParameter("action") != null) {
pager.doAction(request.getParameter("action").toString());
}
// 使用分页标签的方法
List list = rdao.findAllByPage(role, (pager
.getCurrentPage() - 1)
* pager.getPageSize(),pager.getPageSize());
request.getSession().setAttribute("pagerstruts", pager);
request.setAttribute("roles", list);
} catch (Exception e) {
e.printStackTrace();
}
return mapping.findForward("list");
}
/**
* 编辑角色信息,进入编辑页面
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public ActionForward edit(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IllegalAccessException, InvocationTargetException {
String id = request.getParameter("id");
if (id != null && id.trim().length() > 0) {
RoleDAO rdao = new RoleDAO(Role.class);
Role role = (Role) rdao.findById(Role.class, id);
if (role != null) {
BeanUtils.copyProperties(form, role);
}
}
return mapping.findForward("edit");
}
/**
* 删除一条角色信息,并返回列表页面
* @param mapping
* @param form
* @param request
* @param response
* @return
*/
@SuppressWarnings("unchecked")
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
String id=request.getParameter("id");
if(id!=null&&id.trim().length()>0){
RoleDAO rdao=new RoleDAO(Role.class);
Role role=(Role) rdao.findById(Role.class, id);
rdao.delete(role);
// saveMessage(request,"addressForm.deleted",role.getRoleName());
}
return mapping.findForward("success");
}
/**
* 保存角色信息,并转发到列表页面
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
@SuppressWarnings("unchecked")
public ActionForward save(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionErrors errors=form.validate(mapping,request);
if(!errors.isEmpty()){
saveErrors(request,errors);
return edit(mapping,form,request,response);
}
Role role=new Role();
BeanUtils.copyProperties(role, form);
RoleDAO rdao=new RoleDAO(Role.class);
if(role.getId()==null||role.getId().trim().length()==0){
rdao.create(role);
saveMessage(request,"addressForm.added",role.getRoleName());
}else{
rdao.updates(role);
saveMessage(request,"addressForm.updated",role.getRoleName());
}
return mapping.findForward("success");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -