📄 loginaction.java
字号:
/*
* Created on 2005-7-27
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package banksystem.action;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import org.apache.commons.beanutils.BeanUtils;
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 banksystem.Constants;
import banksystem.PO.Customer;
import banksystem.VO.LoginActionForm;
import banksystem.VO.UserInfoActionForm;
import banksystem.business.BusinessDelegate;
import banksystem.exception.*;
import banksystem.business.SearchGene;
/**
* @author 曲本盛
*
* TODO Struts 项目实践
*/
public class LoginAction extends Action {
BusinessDelegate business = null;
/**
* TODO 处理用户登录请求的Action
* @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.ServletRequest, javax.servlet.ServletResponse)
*/
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm,
HttpServletRequest request, HttpServletResponse response) throws Exception {
//若选择取消则重定向到Input
if(this.isCancelled(request)){
return actionMapping.getInputForward();
}
// TODO Auto-generated method stub
//用户登录检查session
HttpSession session = request.getSession(true);
//设置该session的生存周期
//session.setMaxInactiveInterval(20);
ActionMessages errors = this.getErrors(request);
LoginActionForm login = (LoginActionForm)actionForm;
Customer customer = new Customer();
UserInfoActionForm userinfo = new UserInfoActionForm();
String userType = login.getUserType();
String accountID = login.getAccountID();
String password = login.getPassword();
//System.out.println(accountID);
//System.out.println(password);
//填充Customer对象
customer.setAccountID(accountID);
customer.setLoginPass(password);
business = BusinessDelegate.getInstance();
SearchGene gene = new SearchGene();
try{
//使用查询条件代理类自动生成查询条件
gene.setAccountID(accountID);
gene.setUserType(userType);
//System.out.println(userType);
DataSource dataSource = this.getDataSource(request);
business.checkLogin(dataSource,customer,gene);
//将customer中的值存入login
BeanUtils.copyProperties(userinfo,customer);
//关键:若无异常抛出则将userinfo对象存入session中,已备各页查看
session.setAttribute(Constants.KEY_CUSTOMER_INFO,userinfo);
//System.out.println("成功");
if("normal".equals(userType)){
return actionMapping.findForward("normal");
}
else if("admin".equals(userType)){
return actionMapping.findForward("admin");
}
else{
throw new SQLException(Constants.ERRORS_INVALID_INPUT);
}
}
catch(AccountNotExistException ae){
errors.add("failed",new ActionMessage(ae.getKey()));
this.saveErrors(request,errors);
//System.out.println("无该帐号");
return actionMapping.getInputForward();
}
catch(UserPasswordInvalidException ue){
errors.add("failed",new ActionMessage(ue.getKey()));
this.saveErrors(request,errors);
//System.out.println("密码错");
return actionMapping.getInputForward();
}
catch(SQLException e){
errors.add("failed",new ActionMessage(Constants.ERRORS_DATA_SAVE_EXCEPTION,e.getMessage()));
this.saveErrors(request,errors);
//e.printStackTrace();
return actionMapping.getInputForward();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -