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

📄 loginaction.java

📁 Struts+Spring+Hibernate实现的在线拍卖系统,去掉了这些组件自身的jar包
💻 JAVA
字号:
package org.bestteam.web.struts.action;

import java.lang.reflect.InvocationTargetException;
import java.util.Locale;
import java.util.ResourceBundle;

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

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.locale.LocaleBeanUtils;
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.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.DynaValidatorForm;
import org.apache.velocity.texen.util.PropertiesUtil;
import org.bestteam.domain.UserInfo;
import org.bestteam.service.IsExistService;
import org.bestteam.service.LogonService;
import org.bestteam.service.RegisterService;

public class LoginAction extends DispatchAction {
	private LogonService logonService;

	private RegisterService registerService;

	private IsExistService isExistService;

	public void setIsExistService(IsExistService isExistServic) {
		this.isExistService = isExistServic;
	}

	public void setLogonService(LogonService logonService) {
		this.logonService = logonService;
	}

	public ActionForward login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		UserInfo userinfo = new UserInfo();
		ActionForward forward = mapping.findForward("failure");
		try {
			// 获取表单中的值
			String username = (String) PropertyUtils.getProperty(form,
					"username");
			String password = (String) PropertyUtils.getProperty(form,
					"password");

			userinfo.setUserName(username);
			userinfo.setPassword(password);

			userinfo = logonService.logon(userinfo);

			if (userinfo != null) {
				forward = mapping.findForward("success");
				HttpSession  session = request.getSession();
				session.setAttribute("userId", userinfo.getUserId());
				session.setAttribute("username", userinfo.getUserName());
			}
			else {
				request.setAttribute("error", "用户名或者密码错误!");
			}

		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return forward;
	}

	public ActionForward register(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		ActionForward forward = mapping.findForward("failure");
		UserInfo userinfo = new UserInfo();
		try {
			String username = (String) PropertyUtils.getProperty(form,
					"username");
			String password = (String) PropertyUtils.getProperty(form,
					"password");
			String email = (String) PropertyUtils.getProperty(form, "email");

			userinfo.setUserName(username);
			userinfo.setPassword(password);
			userinfo.setEmail(email);
		
//			ResourceBundle resourceBundle = ResourceBundle.getBundle(
//					"org.bestteam.i18n.ApplicationResources.properties", this
//							.getLocale(request));
//			System.out.println("aaa");
//			String errorMsg = resourceBundle.getString("errors.isusernameexists");

			if (!isExistService.isExistUserInfo(userinfo)) {
				userinfo = registerService.registerUser(userinfo);
				if (userinfo != null) {
					forward = mapping.findForward("success");
					request.getSession().setAttribute("userId", userinfo.getUserId());
					request.setAttribute("username", userinfo.getUserName());
				}

			} else {
				request.setAttribute("isexist", "用户已经存在");
				// ActionErrors errors = new ActionErrors();
				// errors.add("errors.isexists", new ActionMessage("username"));

			}

		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return forward;
	}

	
	public ActionForward certifyinfoRegister(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		ActionForward forward = mapping.findForward("failure");
		System.out.println("in the certifyinfoRegister");
		return forward;
	}
	
	public void setRegisterService(RegisterService registerService) {
		this.registerService = registerService;
	}

}

⌨️ 快捷键说明

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