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

📄 loginaction.java

📁 在线考试系统: 这是基于struts和Hibernate的最新技术的在线考试系统.数据库是MySql.能添加课程
💻 JAVA
字号:
package com.zjxy.hibernate.action;

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

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.hibernate.HibernateException;

import com.zjxy.hibernate.base.UserManager;
import com.zjxy.hibernate.form.LoginForm;
import com.zjxy.hibernate.model.User;

public class LoginAction extends DispatchAction{
	public ActionForward toLogin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	        throws HibernateException {
		
		return mapping.findForward("toLogin");
	}
	
	public ActionForward login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	        throws HibernateException {
		// Form的实例化
        LoginForm loginForm = (LoginForm) form; 
        // User的实例化,用于传递参数的实体类
        User user = new User();
        
        // User的另一个实例化,用于接收返回的用户信息
        User userInfo = new User();
        // 定义session
        HttpSession session = request.getSession();
        // 定义ActionErrors对象
        ActionErrors errors = new ActionErrors();
        // 从页面接收用户输入的信息
        String userID = loginForm.getUserID();
        String password = loginForm.getPassword();
        // 将用户输入信息放到user对象里
        user.setUserID(userID);
        user.setPassword(password);
        // 定义并实例化UserManager
        UserManager userManager = new UserManager();
        // 调用基类UserManager的userLogin方法
        userInfo = userManager.userLogin(user);
        
        if (userInfo == null) {
        	// 用户帐号不正确
        	errors.add("userIDIsWrong", new ActionError("oes.login.error.userIDIsWrong"));
        	saveErrors(request, errors);
        	return (new ActionForward(mapping.getInput()));
        }else if (!password.equals(userInfo.getPassword())) {
        	// 用户密码不正确
        	errors.add("passwordIsWrong", new ActionError("oes.login.error.passwordIsWrong"));
        	saveErrors(request, errors);
        	return (new ActionForward(mapping.getInput()));
        }
        // 将正确的用户信息放到session对象里
        session.setAttribute("User", userInfo);
		return mapping.findForward("login");
	}
}

⌨️ 快捷键说明

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