📄 cpeditaction.java
字号:
package com.longtime.wap.module.cp.web.action;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.struts.DispatchActionSupport;
import com.longtime.wap.common.WapConstant;
import com.longtime.wap.model.Cp;
import com.longtime.wap.module.cp.service.CpService;
import com.longtime.wap.module.cp.web.form.CpEditForm;
/**
* 处理修改请求
*
* @author liuzb
* @date Nov 19, 2007
*/
public class CpEditAction extends DispatchActionSupport {
private CpService cpService;
/**
* 注入服务对象
*
* @param cpService
* 服务对象
*/
public void setCpService(CpService cpService) {
this.cpService = cpService;
}
/**
* 处理修改页请求
*
* @param mapping
* ActionMapping对象
* @param form
* ActionForm对象
* @param request
* HttpServletRequest对象
* @param response
* HttpServletResponse对象
* @return ActionForward对象
* @throws Exception
* 异常对象
*/
public ActionForward getCp(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
// 根据Id取cp
String cpId = request.getParameter("id");
Cp cp = cpService.getCpById(Long.parseLong(cpId));
// 设置返回的formbean对象
CpEditForm cpEditForm = (CpEditForm) form;
cpEditForm.setCpId(String.valueOf(cp.getCpId()));
cpEditForm.setCompanyCode(String.valueOf(cp.getCompanyCode()));
cpEditForm.setCompanyName(cp.getCompanyName());
cpEditForm.setAddress(cp.getAddress());
cpEditForm.setPhone(cp.getPhone());
cpEditForm.setServiceDesc(cp.getServiceDesc());
cpEditForm.setRegDate(cp.getRegDate().toString());
cpEditForm.setEmail(cp.getEmail());
cpEditForm.setClientPhone(cp.getClientPhone());
String type = request.getParameter("type");
if ("edit".equalsIgnoreCase(type)) {
cpEditForm.setEditFlug("edit");
}
request.setAttribute("cpEditForm", cpEditForm);
} catch (Exception ex) {
ex.printStackTrace();
}
return mapping.findForward("detailCp");
}
/**
* 处理新增或修改请求
*
* @param mapping
* ActionMapping对象
* @param form
* ActionForm对象
* @param request
* HttpServletRequest对象
* @param response
* HttpServletResponse对象
* @return ActionForward对象
* @throws Exception
* 异常对象
*/
public ActionForward saveCp(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String forward = "detailCp";
List<String> messages = new ArrayList<String>();
try {
CpEditForm cpEditForm = (CpEditForm) form;
String companyCode = cpEditForm.getCompanyCode();
String companyName = cpEditForm.getCompanyName();
// 新增
if (cpEditForm.getEditFlug().equals("save")) {
if (cpService.getCountByCompanyCode(companyCode) > 0) {
messages.add("企业代码已经存在!");
} else if (cpService.getCountByCompanyName(companyName) > 0) {
messages.add("单位名称已经存在!");
} else {
Cp cp = new Cp();
cp.setCompanyCode(Long.parseLong(cpEditForm
.getCompanyCode()));
cp.setCompanyName(cpEditForm.getCompanyName());
cp.setAddress(cpEditForm.getAddress());
cp.setPhone(cpEditForm.getPhone());
cp.setServiceDesc(cpEditForm.getServiceDesc());
cp.setRegDate(new Date());
cp.setEmail(cpEditForm.getEmail());
cp.setClientPhone(cpEditForm.getClientPhone());
cpService.saveCp(cp);
messages.add("新增信息成功!");
forward = "cps";
}
} else if (cpEditForm.getEditFlug().equals("edit")) {
long cpId = Long.parseLong(cpEditForm.getCpId());
int count = cpService.getCountByCompanyName(cpId, companyName);
if (cpService.getCountByCompanyCode(cpId, companyCode) > 0) {
messages.add("企业代码已经存在!");
} else if (count > 0) {
messages.add("单位名称已经存在!");
} else {
Cp cp = cpService.getCpById(cpId);
cp.setCompanyCode(Long.parseLong(cpEditForm
.getCompanyCode()));
cp.setCompanyName(cpEditForm.getCompanyName());
cp.setAddress(cpEditForm.getAddress());
cp.setPhone(cpEditForm.getPhone());
cp.setServiceDesc(cpEditForm.getServiceDesc());
cp.setEmail(cpEditForm.getEmail());
cp.setClientPhone(cpEditForm.getClientPhone());
cpService.saveCp(cp);
messages.add("修改信息成功!");
forward = "cps";
}
}
} catch (Exception ex) {
messages.add(ex.getMessage());
ex.printStackTrace();
}
request.setAttribute(WapConstant.WAP_GLOBAL_MESSAGES, messages);
return mapping.findForward(forward);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -