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

📄 modifyuseraction.java

📁 这是《Struts网络编程实例》一书zhogn 的源代码部分
💻 JAVA
字号:
package org.ithinking.strutsExample.Action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionError;
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 org.apache.struts.actions.DispatchAction;
import org.ithinking.strutsExample.ActionForm.UserInfoActionForm;
import org.ithinking.strutsExample.entity.Userinfo;
import org.ithinking.strutsExample.service.ServiceFactory;
import org.ithinking.strutsExample.service.UserInfoService;
import org.ithinking.strutsExample.serviceImpl.UserInfoUtil;
import org.ithinking.utils.IntegerUtil;
import org.ithinking.utils.MessageDigestUtil;
import org.ithinking.utils.StringUtil;

public class ModifyUserAction extends DispatchAction {

	//更新用户
	public ActionForward execute(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) {
		
		UserInfoActionForm form = (UserInfoActionForm) actionForm;
				
		UserInfoService userInfoservice = ServiceFactory.getUserinfService();
		Userinfo userInfo = userInfoservice.getUserInfoById(form.getId());
		
		if(userInfo==null)
		{
			ActionErrors errors = new ActionErrors();
			errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
					"global.errors.usernotexit"));
			saveErrors(httpServletRequest, errors);
			return actionMapping.findForward("modifyUser");
		}
		
		Userinfo savinguser = UserInfoUtil.getUserInfoFromActionForm(form);
		//如果用户输入的密码不等于数据库里面的原密码,则认为密码已经修改,需要重新计算信息摘要的值
		if(!userInfo.getPassword().equals(form.getPassword()))
		{
			  MessageDigestUtil messageDigest=new MessageDigestUtil(MessageDigestUtil.MD5);
			  String passwordMD5=StringUtil.convertBytesToString(messageDigest.computeDigest(form.getPassword()));
			  savinguser.setPassword(passwordMD5);
		}
		else
		{
			savinguser.setPassword(form.getPassword());
		}
		
		try {
			if (userInfoservice.saveUser(savinguser)) {
				ActionErrors errors = new ActionErrors();
				errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
						"global.saveusersucess"));
				saveErrors(httpServletRequest, errors);
				return actionMapping.findForward("modifyUser");
			} else {
				ActionErrors errors = new ActionErrors();
				errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
						"global.errors.saveusererror"));
				saveErrors(httpServletRequest, errors);
				return actionMapping.findForward("modifyUser");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			ActionErrors errors = new ActionErrors();
			errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
					"global.errors.general"));
			saveErrors(httpServletRequest, errors);
			return actionMapping.findForward("error");
		}
		
	}
}

⌨️ 快捷键说明

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