login.java

来自「是一个网站的博客系统」· Java 代码 · 共 89 行

JAVA
89
字号
package com.opensource.blog.web.action;

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

import org.apache.struts.action.Action;
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 com.laoer.comm.util.Util;
import com.opensource.blog.comm.Constant;
import com.opensource.blog.model.Blog;
import com.opensource.blog.model.UserInfo;
import com.opensource.blog.service.BlogService;
import com.opensource.blog.service.UserInfoService;
import com.opensource.blog.web.form.LoginForm;
import com.opensource.blog.web.servlet.UserSession;

public class Login
    extends Action {

  private UserInfoService userInfoService;

  private BlogService blogService;

  public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm,
                               HttpServletRequest servletRequest,
                               HttpServletResponse servletResponse) {
    ActionMessages errors = new ActionMessages();

    LoginForm form = (LoginForm) actionForm;

    if (form.getAction().equalsIgnoreCase("index")) {
      form.setAction("login");
      return actionMapping.findForward("login");
    }

    if (form.getAction().equalsIgnoreCase("login")) {
      UserInfo ui = this.getUserInfoService().findUserInfoByUserName(form.getUsername());
      if (ui == null) {
        errors.add("error.user.noexist", new ActionMessage("error.user.noexist"));
        saveErrors(servletRequest, errors);
        return actionMapping.getInputForward();
      }
      if (!ui.getPasswdre().equals(Util.hash(form.getPasswd()))) {
        errors.add("error.login.passwd", new ActionMessage("error.login.passwd"));
        saveErrors(servletRequest, errors);
        return actionMapping.getInputForward();
      }

      Blog blog = this.getBlogService().findBlogByUserName(form.getUsername());
      if (blog == null) {
        errors.add("error.blogblog.getinfo", new ActionMessage("error.blogblog.getinfo"));
        saveErrors(servletRequest, errors);
        return actionMapping.getInputForward();
      }
      UserSession us = new UserSession(ui, blog);
      HttpSession session = servletRequest.getSession();
      session.setAttribute(Constant.USER_SESSION_KEY, us);

      ActionForward f = new ActionForward("/manage.do", true);
      return f;

    }

    return actionMapping.findForward("error");
  }

  public BlogService getBlogService() {
    return blogService;
  }

  public UserInfoService getUserInfoService() {
    return userInfoService;
  }

  public void setBlogService(BlogService blogService) {
    this.blogService = blogService;
  }

  public void setUserInfoService(UserInfoService userInfoService) {
    this.userInfoService = userInfoService;
  }
}

⌨️ 快捷键说明

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