⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uncommandwordeditaction.java

📁 这个是完整的wap项目的源码 开发语言 Java 系统架构 Struts + hibernate + spring 数据库 Mysql5.0 应用服务器Tomcat5.0 开发工具 MyEc
💻 JAVA
字号:
package com.longtime.wap.module.cellphone.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.springframework.web.struts.DispatchActionSupport;

import com.longtime.wap.common.WapConstant;
import com.longtime.wap.model.UncommandWord;
import com.longtime.wap.module.cellphone.service.UncommandWordService;
import com.longtime.wap.module.cellphone.web.form.UncommandWordForm;

/**
 * 处理修改请求
 * 
 * @author shiz
 * @date Nov 19, 2007
 */
public class UncommandWordEditAction extends DispatchActionSupport {
	private static final Log LOGGER = 
		LogFactory.getLog(UncommandWordEditAction.class);
	private UncommandWordService uncommandWordService;

	/**
	 * 注入服务对象
	 * 
	 * @param uncommandWordService
	 *            设置服务对象
	 */
	public void setUncommandWordService(
			UncommandWordService uncommandWordService) {
		this.uncommandWordService = uncommandWordService;
	}

	/**
	 * 获得相应过滤字符信息
	 * 
	 * @param mapping
	 *            ActionMapping对象
	 * @param form
	 *            ActionForm对象
	 * @param request
	 *            HttpServletRequest对象
	 * @param response
	 *            HttpServletResponse对象
	 * @return ActionForward对象
	 * @throws Exception
	 *             异常处理
	 */
	public ActionForward getUncommandWord(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		UncommandWord uncommandWord = new UncommandWord();
		String type = request.getParameter("type");
		// 设置返回的formbean对象
		UncommandWordForm uncommandWordForm = (UncommandWordForm) form;
		String uncommandWordId = request.getParameter("id");
		if (!"yes".equals(request.getParameter("reset"))) {
			request.getSession().setAttribute("id", uncommandWordId);
		} else {
			if ("edit".equals(type)) {
				uncommandWordId = request.getSession().getAttribute("id")
						.toString();
			}
		}
		try {
			// 根据Id取information

			if (null != uncommandWordId) {
				uncommandWord = uncommandWordService.getUncommandWordById(Long
						.parseLong(uncommandWordId));
				uncommandWordForm.setUncommandWordId(String
						.valueOf(uncommandWord.getUncommandWordId()));
				uncommandWordForm.setWord(String.valueOf(uncommandWord
						.getWord()));
				uncommandWordForm.setDate(String.valueOf(uncommandWord
						.getCreateDate()));

				request.setAttribute("uncommandWordForm", uncommandWordForm);
			}

			// 设置返回页面是否允许编辑

			if ("edit".equalsIgnoreCase(type)) {
				uncommandWordForm.setEditFlug("edit");
				uncommandWordForm.setWord(String.valueOf(uncommandWord
						.getWord()));
				uncommandWordForm.setUncommandWordId(String
						.valueOf(uncommandWord.getUncommandWordId()));
			}
			if ("view".equalsIgnoreCase(type)) {
				uncommandWordForm.setEditFlug("view");
			}
			if ("new".equalsIgnoreCase(type)) {
				uncommandWordForm.setEditFlug("new");
				uncommandWordForm.setWord("");
			}

		} catch (Exception ex) {
			LOGGER.error(ex);
			ex.printStackTrace();
		}
		return mapping.findForward("detailUncommandWord");
	}

	/**
	 * 处理新增或修改中的保存请求
	 * 
	 * @param mapping
	 *            ActionMapping对象
	 * @param form
	 *            ActionForm对象
	 * @param request
	 *            HttpServletRequest对象
	 * @param response
	 *            HttpServletResponse对象
	 * @return ActionForward对象
	 * @throws Exception
	 *             异常对象
	 */
	public ActionForward editUncommandWord(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<String> messages = new ArrayList<String>();
		boolean success = true;
		try {
			UncommandWordForm uncommandWordForm = (UncommandWordForm) form;
			String word = uncommandWordForm.getWord().trim();
			String flug = uncommandWordForm.getEditFlug();
			UncommandWord uncommandWord;
			if (flug.equals("new")) {
				// 增加
				uncommandWord = new UncommandWord();

				if (null == uncommandWordService.getUncommandWordByWord(word)) {
					uncommandWord.setWord(word);
					uncommandWord.setCreateDate(new Date());
					uncommandWordService.saveUncommandWord(uncommandWord);
					messages.add("过滤字符信息保存成功!");
					success = true;
				} else {
					messages.add("该过滤字段已经存在!");
					success = false;
				}
			} else if (flug.equals("edit")) {
				// 修改
				if (null != uncommandWordForm.getUncommandWordId()
						&& uncommandWordForm.getUncommandWordId().length() > 0) 
				{
					uncommandWord = uncommandWordService
							.getUncommandWordById(Long
									.parseLong(uncommandWordForm
											.getUncommandWordId()));
					// 如果字段id不为空并且有效,则判断该word在数据库是否存在
					if (null == uncommandWordService
							.getUncommandWordByWord(word)) {
						// 若不存在,保存成功
						uncommandWord.setWord(word);
						uncommandWordService.saveUncommandWord(uncommandWord);
						messages.add("过滤字符信息修改成功!");
						success = true;
					} else if (Long.valueOf(uncommandWordForm
							.getUncommandWordId()) == uncommandWordService
							.getUncommandWordByWord(word).getUncommandWordId()) 
					{
						// 若存在并且查询数据库所得对象的id和要修改对象的id相同,则更新
						uncommandWord.setWord(word);
						uncommandWordService.saveUncommandWord(uncommandWord);
						messages.add("过滤字符信息修改成功!");
						success = true;
					} else {
						messages.add("该过滤字段已存在");
						success = false;
					}
				} else {
					messages.add("没有此过滤字段");
				}
			}
		} catch (Exception ex) {
			LOGGER.error(ex);
			messages.add("保存过滤字段对象失败!");
			ex.printStackTrace();
		}
		if (success == true) {
			request.setAttribute(WapConstant.WAP_GLOBAL_MESSAGES, messages);
			return mapping.findForward("uncommandWords");
		} else {
			request.setAttribute(WapConstant.WAP_GLOBAL_MESSAGES, messages);
			return mapping.findForward("wordFailed");
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -