📄 305d3dcfa932001d13bac57449acedd7
字号:
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 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.ClientDAO;
import com.qrsx.qrsxcrm.dao.ClientTypeDAO;
import com.qrsx.qrsxcrm.dao.EmployeeDAO;
import com.qrsx.qrsxcrm.form.ClientForm;
import com.qrsx.qrsxcrm.model.Client;
import com.qrsx.qrsxcrm.model.ClientType;
import com.qrsx.qrsxcrm.model.Employee;
import com.qrsx.qrsxcrm.web.Pager;
/**
* 客户业务操作类
* @author Administrator
*
*/
public class ClientAction extends BaseDispatchAction {
/**
* 列出符合条件的数据
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
@SuppressWarnings("unchecked")
public ActionForward list(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IllegalAccessException, InvocationTargetException {
Client client = new Client();
BeanUtils.copyProperties(client, form);
//组装客户类型
ClientTypeDAO ctDao=new ClientTypeDAO(ClientType.class);
List clientTypes=ctDao.findAll("from ClientType");
request.setAttribute("clientTypes", clientTypes);
ClientTypeDAO clientTypeDao=new ClientTypeDAO(ClientType.class);
//组装客户类型
if(client.getClientTypeId()!=null&&client.getClientTypeId().trim().length()>0){
ClientType clientType=(ClientType) clientTypeDao.findById(ClientType.class, client.getClientTypeId());
client.setClientType(clientType);
}
try {
Pager pager = null;
ClientDAO cdao = new ClientDAO(Client.class);
List results = cdao.findAll("from Client");//得到总数据
pager = new Pager(); // 构造分页对象
int totalRows = results.size(); // 得到总数据量
pager.init(totalRows);
if (request.getParameter("action") != null) {
pager.doAction(request.getParameter("action").toString());
}
// 使用分页标签的方法
List list = cdao.findAllByPage(client, (pager.getCurrentPage() - 1)* pager.getPageSize(),pager.getPageSize());
request.getSession().setAttribute("pagerstruts", pager);
request.setAttribute("clients", 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");
ClientDAO cdao = new ClientDAO(Client.class);
// 组装客户类型
ClientTypeDAO ctDao=new ClientTypeDAO(ClientType.class);
List clientTypes=ctDao.findAll("from ClientType");
request.setAttribute("clientTypes", clientTypes);
//组装客户负责人
EmployeeDAO edao=new EmployeeDAO(Employee.class);
List employees=edao.findAll("from Employee");
request.setAttribute("employees", employees);
if (id != null && id.trim().length() > 0) {
Client client = (Client) cdao.findById(Client.class, id);
if(client.getClientType()!=null){
client.setClientTypeId(client.getClientType().getId());
}
if (client != null) {
BeanUtils.copyProperties(form, client);
}
}
return mapping.findForward("edit");
}
/**
* 保存客户信息
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public ActionForward save(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IllegalAccessException, InvocationTargetException {
ActionErrors errors = form.validate(mapping, request);
if (!errors.isEmpty()) {
saveErrors(request, errors);
return edit(mapping, form, request, response);
}
ClientForm clientForm=(ClientForm)form;
Client client=new Client();
BeanUtils.copyProperties(client, form);//将from组装到client对象中
//组装一个客户负责人
EmployeeDAO edao=new EmployeeDAO(Employee.class);
client.setEmployee((Employee)edao.findById(Employee.class, client.getEmployeeId()));
//组装一个客户类型
ClientTypeDAO ctDao=new ClientTypeDAO(ClientType.class);
client.setClientType((ClientType)ctDao.findById(ClientType.class, client.getClientTypeId()));
ClientDAO cdao=new ClientDAO(Client.class);
if(clientForm.getId()==null||clientForm.getId().trim().length()==0){
cdao.create(client);
saveMessage(request, "announcementForm.added", client.getClientName());
//saveMessage创建成功
}
else{
cdao.updates(client);
saveMessage(request, "announcementForm.updated", client.getClientName());
//saveMessage更新成功
}
return mapping.findForward("success");
}
/**
* 删除一条信息
* @param mapping
* @param form
* @param request
* @param response
* @return
*/
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
String id=request.getParameter("id");
if(id!=null&&id.trim().length()>0){
ClientDAO cdao=new ClientDAO(Client.class);
Client client=(Client) cdao.findById(Client.class, id);
cdao.delete(client);
saveMessage(request, "announcementForm.deleted", client.getClientName());
//saveMessage
}
return mapping.findForward("success");
}
/**
* 检索一条信息
* @param mapping
* @param form
* @param request
* @param response
* @return
*/
public ActionForward info(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
String id=request.getParameter("id");
if(id!=null&&id.trim().length()>0){
ClientDAO cdao=new ClientDAO(Client.class);
Client client=(Client) cdao.findById(Client.class, id);
request.setAttribute("client", client);
//saveMessage
}
return mapping.findForward("info");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -