📄 informationeditaction.java
字号:
package com.longtime.wap.module.information.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.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.apache.struts.upload.FormFile;
import org.springframework.web.struts.DispatchActionSupport;
import com.longtime.wap.common.WapConstant;
import com.longtime.wap.model.Business;
import com.longtime.wap.model.Information;
import com.longtime.wap.module.information.common.InformationConstant;
import com.longtime.wap.module.information.service.InformationService;
import com.longtime.wap.module.information.web.form.InformationEditForm;
/**
* 处理编辑信息的请求
*
* @author bulc
* @date Nov 30, 2007
*/
public class InformationEditAction extends DispatchActionSupport {
private static final Log LOGGER =
LogFactory.getLog(InformationEditAction.class);
private InformationService informationService;
/**
* 注入服务对象
*
* @param informationService
* 服务对象
*/
public void setInformationService(InformationService informationService) {
this.informationService = informationService;
}
/**
* 处理检索一个信息的请求
*
* @param mapping
* ActionMapping对象
* @param form
* ActionForm对象
* @param request
* HttpServletRequest对象
* @param response
* HttpServletResponse对象
* @return ActionForward对象
* @throws Exception
* 异常对象
*/
public ActionForward getInformation(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
try{
// 根据Id取information
String informationId = request.getParameter("id");
Information information = informationService
.getInformationById(Long.parseLong(informationId));
// 设置返回的formbean对象
InformationEditForm informationEditForm = (InformationEditForm)form;
if (information.getBusiness().getCategory() == 0){
informationEditForm.setDetailType("text");
} else if (information.getBusiness().getCategory() == 1) {
informationEditForm.setDetailType("music");
} else if (information.getBusiness().getCategory() == 2) {
informationEditForm.setDetailType("pic");
}
// 设置返回页面是否允许编辑
String type = request.getParameter("type");
if ("edit".equalsIgnoreCase(type)) {
informationEditForm.setEditFlug("edit");
}
informationEditForm.setFilePath(information.getFilePath());
informationEditForm.setInformationId(
String.valueOf(information.getInformationId()));
informationEditForm.setTitle(information.getTitle());
informationEditForm.setPrice(
String.valueOf(information.getPrice()));
informationEditForm.setBusinessName(
information.getBusiness().getBusinessName());
informationEditForm.setContent(information.getContent());
if (information.getIsHot() == 1) {
informationEditForm.setIsHot(true);
}
if (information.getIsPub() == 1) {
informationEditForm.setIsPub(true);
}
if (information.getIsRecommend() == 1) {
informationEditForm.setIsRecommend(true);
}
request.setAttribute(InformationConstant.INFORMATION_EDIT_FORM,
informationEditForm);
} catch (Exception ex) {
LOGGER.error(ex);
ex.printStackTrace();
}
return mapping.findForward("detailInformation");
}
/**
* 处理保存信息的请求
*
* @param mapping
* ActionMapping对象
* @param form
* ActionForm对象
* @param request
* HttpServletRequest对象
* @param response
* HttpServletResponse对象
* @return ActionForward对象
* @throws Exception
* 异常对象
*/
public ActionForward saveInformation(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
List<String> messages = new ArrayList<String>();
try {
InformationEditForm infoForm = (InformationEditForm)form;
Information info;
if (null != infoForm.getInformationId()
&& infoForm.getInformationId().length() > 0) {
info = informationService.getInformationById(
Long.parseLong(infoForm.getInformationId()));
} else {
info = new Information();
// 设置businessId
Business business = new Business();
business.setBusinessId(
Long.parseLong(infoForm.getBusinessId()));
info.setBusiness(business);
// 上传文件
if (!"text".equalsIgnoreCase(infoForm.getDetailType())){
FormFile file = infoForm.getUploadFile();
String uploadDir;
if ("music".equalsIgnoreCase(infoForm.getDetailType())) {
uploadDir = "/UserFiles/Music";
} else {
uploadDir = "/UserFiles/Pic";
}
String fileName = informationService.uploadFile(
servlet.getServletContext().getRealPath(uploadDir),
file);
file.destroy();
info.setFilePath(
uploadDir.substring(1, uploadDir.length())
+ "/" + fileName);
}
}
if (infoForm.getIsHot()) {
info.setIsHot(1);
} else {
info.setIsHot(0);
}
if (infoForm.getIsPub()) {
info.setIsPub(1);
info.setPubDate(new Date());
} else {
info.setIsPub(0);
}
if (infoForm.getIsRecommend()){
info.setIsRecommend(1);
} else {
info.setIsRecommend(0);
}
info.setTitle(infoForm.getTitle().trim());
info.setContent(infoForm.getContent().trim());
info.setPrice(Double
.parseDouble(infoForm.getPrice().trim()));
informationService.saveInformation(info);
messages.add("信息保存成功!");
} catch (RuntimeException ex) {
messages.add(ex.getMessage());
LOGGER.error(ex);
} catch (Exception ex) {
LOGGER.error(ex);
ex.printStackTrace();
}
request.setAttribute(WapConstant.WAP_GLOBAL_MESSAGES, messages);
return mapping.findForward("informations");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -