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

📄 adminlogincontroller.java

📁 这是基于spring +hibernate的项目
💻 JAVA
字号:
package com.pure.web.spring.manager;

import java.util.HashMap;
import java.util.Map;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.pure.domain.Admin;
import com.pure.domain.logic.AdminFacade;
import com.pure.util.PureUtil;
import com.pure.web.spring.AdminSession;
import com.pure.web.spring.manager.member.MemberValidator;

public class AdminLoginController extends SimpleFormController {
	protected final Log logger = LogFactory.getLog(getClass());

	private AdminFacade adminFacade;

	public AdminFacade getAdminFacade() {
		return adminFacade;
	}

	public void setAdminFacade(AdminFacade adminFacade) {
		this.adminFacade = adminFacade;
	}

	/**
	 * 初始化表单数据
	 */
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		Admin admin = new Admin();
		return admin;
	}

	/**
	 * 处理表单提交事件
	 */
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		HttpSession session = request.getSession();
		Admin admin = (Admin) command;
		/**
		 * 此参数从login.jsp页面表单hidden对象传入,保存为用户登陆前所请求的地址。
		 */
		String forwardAction = request.getParameter("forwardAction");
		
		/**
		 * 登陆必填项未填写或失败时仍需将请求地址保存,以便登陆成功后跳转。
		 * </P>
		 * 由于在表单hidden中使用了<code><c:url value="${signonForwardAction}"/></code>,所以下面需要处理去掉上下文环境。
		 */
		Map model = new HashMap();
		if(null!=forwardAction)
			model.put("signonForwardAction",forwardAction.replace(request.getContextPath(),""));
		
		if ("".equals(admin.getName())) {
			errors.rejectValue("name", "LOGIN FAIL", AdminValidator.adornMsg("对不起!用户名是必须的! "));
			return showForm(request, response, errors,model);
		}

		if ("".equals(admin.getPassword())) {
			errors.rejectValue("password", "LOGIN FAIL", AdminValidator.adornMsg("对不起!登录密码是必须的! "));
			return showForm(request, response, errors,model);
		}

		Admin loginAdmin = getAdminFacade().login(admin);
		if (null != loginAdmin) {
			session.setAttribute("adminSession",new AdminSession(loginAdmin));
			loginAdmin.setLoginip(request.getRemoteHost());
			loginAdmin.setLogintime(PureUtil.getDateFormatStr("yyyy-MM-dd HH:mm:ss"));
			Integer count = loginAdmin.getLogincount();
			// TODO Integer相加?
			if (count == null) {
				loginAdmin.setLogincount(new Integer(1));
			} else {
				loginAdmin.setLogincount(new Integer(Integer.parseInt(String.valueOf(count)) + 1));
			}
			getAdminFacade().update(loginAdmin);
		} else {
			errors.reject("LOGIN FAIL", MemberValidator.adornMsg("对不起!登陆失败!请检查你的用户名及密码! "));
			return showForm(request, response, errors,model);
		}
		
		if (forwardAction != null) {
			response.sendRedirect(forwardAction);
			return null;
		} else {
			return new ModelAndView(getSuccessView());
		}
	}
}

⌨️ 快捷键说明

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