1、搭建Struts2环境
2、创建实体类
admin、manager、student
package cn.sushe.model;
public class Admin {
private int adminId;
private String userName;
private String password;
private String name;
private String sex;
private String tel;
//get,set方法
}
package cn.sushe.model;
public class Manager {
private int manId;
private String userName;
private String password;
private int departId;
private String departName;
private String manName;
private String sex;
private int tel;
//get,set方法
}
package cn.sushe.model;
public class Student {
private int stuId;
private String stuNum;
private String userName;
private String password;
private int departId = 0;
private String departName;
private int dormId = 0;
private String dormName;
private String stuName;
private String sex;
private String tel;
private String stuDesc;
//get,set方法
}
3、数据库连接类
package cn.sushe.utils;
import java.sql.Connection;
import java.sql.DriverManager;
public class DbUtil {
private String dbUrl="jdbc:mysql://localhost:3306/db_dormitory";
private String dbUserName="root";
private String dbPassword="123456";
private String jdbcName="com.mysql.jdbc.Driver";
/**
* 获取数据库链接
* @return
* @throws Exception
*/
public Connection getCon() throws Exception{
Class.forName(jdbcName);
Connection con= DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
return con;
}
/**
* 关闭数据库链接
* @param con
* @throws Exception
*/
public void closeCon(Connection con) throws Exception{
if (con!=null){
con.close();
}
}
/**
* 测试数据库链接
* @param args
*/
public static void main(String[] args){
DbUtil dbUtil = new DbUtil();
try {
dbUtil.getCon();
System.out.println("数据库连接成功");
} catch (Exception e) {
e.printStackTrace();
}
}
}
4、对应dao层查询数据库
public class AdminDao {
/**
* 管理员登录
* @param username
* @param password
* @return
* @throws Exception
*/
public String checkLogin(String username,String password)throws Exception{
String id=null;
String sql="select * from t_admin where userName='"+username+"' and password='"+password+"'";
Statement stat=null;
ResultSet rs = null;
Connection con=new DbUtil().getCon();
try {
stat=con.createStatement();
rs=stat.executeQuery(sql);
while (rs.next()) {
id=rs.getString("adminId");
}
} catch (SQLException ex) {}
return id;
}
}
public class ManagerDao {
public String checkLogin(String username,String password) throws Exception{
String id=null;
String sql="select * from t_manager where userName='"+username+"' and password='"+password+"'";
Statement stat=null;
ResultSet rs = null;
Connection con=new DbUtil().getCon();
try {
stat=con.createStatement();
rs=stat.executeQuery(sql);
while (rs.next()) {
id=rs.getString("manId");
}
} catch (SQLException ex) {}
return id;
}
}
public class StudentDao {
public String checkLogin(String username,String password) throws Exception{
String id=null;
String sql="select * from t_student where userName='"+username+"' and password='"+password+"'";
Statement stat=null;
ResultSet rs = null;
Connection con=new DbUtil().getCon();
try {
stat=con.createStatement();
rs=stat.executeQuery(sql);
while (rs.next()) {
id=rs.getString("stuId");
}
} catch (SQLException ex) {}
return id;
}
}
*
5、action业务处理
*
package cn.sushe.action;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import cn.sushe.dao.AdminDao;
import cn.sushe.dao.ManagerDao;
import cn.sushe.dao.StudentDao;
import cn.sushe.model.Admin;
import cn.sushe.model.Manager;
import cn.sushe.model.Student;
import cn.sushe.utils.DbUtil;
import cn.sushe.utils.StringUtil;
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String userType;
private String username;
private String password;
private String msg;
private Admin admin;
private Manager manager;
private Student student;
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
DbUtil dbUtil = new DbUtil();
AdminDao adminDao = new AdminDao();
ManagerDao managerDao = new ManagerDao();
StudentDao studentDao = new StudentDao();
@Override
public String execute() throws Exception {
if (StringUtil.isEmpty(username)||StringUtil.isEmpty(password)) {
msg="用户名或密码为空!";
return ERROR;
}
if ("admin".equals(userType)) {
if (null==adminDao.checkLogin(username, password)) {
msg="用户名或者密码错误";
return ERROR;
} else {
String adminId=adminDao.checkLogin(username, password);
//创建session
HttpSession session = ServletActionContext.getRequest().getSession();
session.setAttribute("adminId", adminId);
session.setAttribute("userType", "系统管理员");
return SUCCESS;
}
} else if ("manager".equals(userType)){
if (null==managerDao.checkLogin(username, password)) {
msg="用户名或者密码错误";
return ERROR;
} else {
String manId=managerDao.checkLogin(username, password);
//创建session
HttpSession session = ServletActionContext.getRequest().getSession();
session.setAttribute("manId", manId);
session.setAttribute("userType", "宿舍管理员");
return SUCCESS;
}
} else if ("student".equals(userType)) {
if (null==studentDao.checkLogin(username, password)) {
msg="用户名或者密码错误";
return ERROR;
} else {
String stuId=studentDao.checkLogin(username, password);
//创建session
HttpSession session = ServletActionContext.getRequest().getSession();
session.setAttribute("stuId", stuId);
session.setAttribute("userType", "学生");
return "success2";
}
}else {
msg="身份类型错误!";
return ERROR;
}
}
}
6、struts.xml页面跳转配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="user" namespace="/" extends="struts-default">
<action name="login" class="cn.sushe.action.LoginAction">
<result name="error">/login.jsp</result>
<result name="success" type="redirect">/index.jsp</result>
<result name="success2" type="redirect">/main.jsp</result>
</action>
</package>
</struts>
- 其实很简单,但之前没搞懂的时候觉得无从下手,所以留个大概思路以便以后能很方便的想起来。
- 代码只是工具,编程思想很重要
- 新手有什么不懂的,可以留言,我也是新手,不过交流使人进入,你正好需要,我正好拥有