package su.bts1.action;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
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.util.LabelValueBean;
import su.bts1.bean.ProjectBean;
import su.bts1.bean.UserFormBean;
import su.bts1.dao.IUserDB;
import su.bts1.imp.AdminUserImp;
import su.bts1.imp.ProgramerDBImp;
import su.bts1.imp.ProjectBeansDB;
import su.bts1.imp.UserDBImp;
public class LoginAction extends Action {
/*
* (non-Javadoc)
*
* @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping,
* org.apache.struts.action.ActionForm,
* javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
@Override
public ActionForward execute(ActionMapping mapping, ActionForm actionform,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserFormBean user = (UserFormBean) actionform;
DataSource ds = this.getDataSource(request, "testDb");
Connection conn = ds.getConnection();
if (conn == null) {
return mapping.findForward("error.jsp");
}
// 如果为管理员
if ("3".equals(user.getDepartID())) {
IUserDB admin = new AdminUserImp(user);
if (admin.selectUser(conn)) {
HttpSession session = request.getSession();
session.setAttribute("user3", user);
session.setAttribute("isLog", true);
return mapping.findForward("admin");
}
}
// 测试员
if ("2".equals(user.getDepartID())) {
IUserDB userDB = new UserDBImp(user);
if (userDB.selectUser(conn)) {
// 取出所有ProjectBean
ProjectBeansDB project = new ProjectBeansDB();
List projList = project.projBeans(conn);
List<LabelValueBean> tempList = new ArrayList<LabelValueBean>();
if (projList != null) {
for (int i = 0; i < projList.size(); i++) {
ProjectBean pb = (ProjectBean) projList.get(i);
String projName = pb.getProjName();
tempList.add(new LabelValueBean(projName, projName));
}
}
HttpSession session = request.getSession();
session.setAttribute("project", tempList);
session.setAttribute("user2", user);
session.setAttribute("isLog", true);
return mapping.findForward("test");
}
}
// 程序员
if ("1".equals(user.getDepartID())) {
IUserDB programer = new ProgramerDBImp(user);
if (programer.selectUser(conn)) {
HttpSession session = request.getSession();
session.setAttribute("user1", user);
session.setAttribute("isLog", true);
return mapping.findForward("program");
}
}
// 关闭连接
try {
conn.close();
} catch (Exception e) {
e.getStackTrace();
}
// 不存在用户名
response
.getWriter()
.println(
"<script>alert('不好意思,用户名密码不正确!');location.href='login.jsp'</script>");
return null;
}
}