?? cellphoneeditaction.java
字號:
package com.longtime.wap.module.cellphone.web.action;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.Cellphone;
import com.longtime.wap.module.cellphone.service.CellphoneService;
import com.longtime.wap.module.cellphone.web.form.CellphoneForm;
/**
* 處理修改請求
*
* @author shiz
* @date Nov 19, 2007
*/
public class CellphoneEditAction extends DispatchActionSupport {
private static final Log LOGGER =
LogFactory.getLog(CellphoneEditAction.class);
private CellphoneService cellphoneService;
/**
* 注入服務(wù)對象
*
* @param cellphoneService
* 設(shè)置服務(wù)對象
*/
public void setCellphoneService(CellphoneService cellphoneService) {
this.cellphoneService = cellphoneService;
}
/**
* 獲得相應(yīng)手機(jī)信息
*
* @param mapping
* ActionMapping對象
* @param form
* ActionForm對象
* @param request
* HttpServletRequest對象
* @param response
* HttpServletResponse對象
* @return ActionForward對象
* @throws Exception
* 異常處理
*/
public ActionForward getCellphone(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Cellphone cellphone = null;
String type = request.getParameter("type");
// 設(shè)置返回的formbean對象
CellphoneForm cellphoneForm = (CellphoneForm) form;
String cellphoneId = request.getParameter("id");
try {
// 根據(jù)Id取information
if (null != cellphoneId) {
cellphone = cellphoneService.getCellphoneById(Long
.parseLong(cellphoneId));
cellphoneForm.setCellphoneId(String.valueOf(cellphone
.getCellphoneId()));
cellphoneForm.setCellphoneUA(String.valueOf(cellphone.getUa()));
cellphoneForm.setCellphoneType(String.valueOf(cellphone
.getModel()));
cellphoneForm.setCellphoneCompany(String.valueOf(cellphone
.getBrand()));
cellphoneForm.setUniqueUa(String.valueOf(cellphone.getUa()));
cellphoneForm.setUniqueType(String
.valueOf(cellphone.getModel()));
if ("view".equalsIgnoreCase(type)) {
String imageType = "";
if (cellphone.getSupportImageType() == 0) {
imageType = "Png";
} else if (cellphone.getSupportImageType() == 1) {
imageType = "Jpg";
} else if (cellphone.getSupportImageType() == 2) {
imageType = "Bmp";
} else {
imageType = "Gif";
}
cellphoneForm.setCellphonePicture(imageType);
} else {
cellphoneForm.setCellphonePicture(String.valueOf(cellphone
.getSupportImageType()));
}
request.setAttribute("cellphoneForm", cellphoneForm);
}
// 設(shè)置返回頁面是否允許編輯
if ("edit".equalsIgnoreCase(type)) {
cellphoneForm.setEditFlug("edit");
}
if ("view".equalsIgnoreCase(type)) {
cellphoneForm.setEditFlug("view");
}
if ("new".equalsIgnoreCase(type)) {
cellphoneForm.setEditFlug("new");
}
} catch (Exception ex) {
LOGGER.error(ex);
ex.printStackTrace();
}
return mapping.findForward("detailCellphone");
}
/**
* 處理新增或修改中的保存請求
*
* @param mapping
* ActionMapping對象
* @param form
* ActionForm對象
* @param request
* HttpServletRequest對象
* @param response
* HttpServletResponse對象
* @return ActionForward對象
* @throws Exception
* 異常對象
*/
public ActionForward saveCellphone(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
List<String> messages = new ArrayList<String>();
try {
CellphoneForm cellphoneForm = (CellphoneForm) form;
String flug = cellphoneForm.getEditFlug();
String ua = cellphoneForm.getCellphoneUA().trim();
String model = cellphoneForm.getCellphoneType().trim();
Cellphone cellphone;
if (flug.equals("new")) {
// 增加
cellphone = new Cellphone();
if (null == cellphoneService.getCellphoneByUA(ua)) {
if (null == cellphoneService.getCellphoneByModel(model)) {
cellphone.setUa(cellphoneForm.getCellphoneUA().trim());
cellphone.setModel(cellphoneForm.getCellphoneType()
.trim());
cellphone.setBrand(cellphoneForm.getCellphoneCompany()
.trim());
cellphone.setSupportImageType(Long
.parseLong(cellphoneForm.getCellphonePicture()
.trim()));
cellphoneService.saveCellphone(cellphone);
messages.add("手機(jī)信息保存成功!");
} else {
messages.add("手機(jī)型號已經(jīng)存在!");
request.setAttribute(WapConstant.WAP_GLOBAL_MESSAGES,
messages);
return mapping.findForward("failed");
}
} else {
messages.add("手機(jī)UA已經(jīng)存在!");
request.setAttribute(WapConstant.WAP_GLOBAL_MESSAGES,
messages);
return mapping.findForward("failed");
}
} else if (flug.equals("edit")) {
// 修改
cellphone = cellphoneService.getCellphoneById(Long
.parseLong(cellphoneForm.getCellphoneId()));
if (null != cellphoneForm.getCellphoneId()
&& cellphoneForm.getCellphoneId().length() > 0) {
if (cellphoneForm.getUniqueUa().equals(ua)
|| (null == cellphoneService.getCellphoneByUA(ua)))
{
cellphone.setUa(cellphoneForm.getCellphoneUA().trim());
} else {
messages.add("手機(jī)UA已經(jīng)存在!");
request.setAttribute(WapConstant.WAP_GLOBAL_MESSAGES,
messages);
return mapping.findForward("failed");
}
if (cellphoneForm.getUniqueType().equals(model)
|| (null == cellphoneService
.getCellphoneByModel(model))) {
cellphone.setModel(cellphoneForm.getCellphoneType()
.trim());
} else {
messages.add("手機(jī)型號已經(jīng)存在!");
request.setAttribute(WapConstant.WAP_GLOBAL_MESSAGES,
messages);
return mapping.findForward("failed");
}
cellphone.setBrand(cellphoneForm.getCellphoneCompany()
.trim());
cellphone.setSupportImageType(Long.parseLong(cellphoneForm
.getCellphonePicture().trim()));
cellphoneService.saveCellphone(cellphone);
messages.add("手機(jī)信息修改成功!");
} else {
messages.add("沒有此手機(jī)信息");
}
}
} catch (Exception ex) {
LOGGER.error(ex);
messages.add("手機(jī)信息保存失敗");
ex.printStackTrace();
}
request.setAttribute(WapConstant.WAP_GLOBAL_MESSAGES, messages);
return mapping.findForward("cellphones");
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -