自定义MVC增删查改

自定义MVC增删查改

首先我要讲一个Demo,也是我接下来要来介绍的,就是跟据老师id或班级id找到该某同学所在班级和所在哪个老师所带。话不多说上代码。
首先我们要建立3个实体类,Student类,Teacher类,Class类

Student:
package com.wangshaoyang.entity;
public class Student {
	private int sid;
	private String sname;
	private String shobby;
	private int tid;
	private int cid;
	
	public int getTid() {
		return tid;
	}
	public void setTid(int tid) {
		this.tid = tid;
	}
	public int getCid() {
		return cid;
	}
	public void setCid(int cid) {
		this.cid = cid;
	}
	public void setSid(int sid) {
		this.sid = sid;
	}
	public int getSid() {
		return sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getShobby() {
		return shobby;
	}
	public void setShobby(String shobby) {
		this.shobby = shobby;
	}
	public Student() {
		super();
	}
	public Student(int sid, String sname, String shobby, int tid, int cid) {
		super();
		this.sid = sid;
		this.sname = sname;
		this.shobby = shobby;
		this.tid = tid;
		this.cid = cid;
	}
	@Override
	public String toString() {
		return "Student [sid=" + sid + ", sname=" + sname + ", shobby=" + shobby + ", t=" + tid + ", c=" + cid + "]";
	}
}
Teacher:
package com.wangshaoyang.entity;

public class Teacher {
	
	private int tid;
	private String tname;
	public int getTid() {
		return tid;
	}
	public void setTid(int tid) {
		this.tid = tid;
	}
	public String getTname() {
		return tname;
	}
	public void setTname(String tname) {
		this.tname = tname;
	}
	public Teacher() {}
	public Teacher(int tid, String tname) {
		super();
		this.tid = tid;
		this.tname = tname;
	}
}
Class
package com.wangshaoyang.entity;

public class Class {
	
	private int cid;
	private String cname;
	public int getCid() {
		return cid;
	}
	public void setCid(int cid) {
		this.cid = cid;
	}
	public String getCname() {
		return cname;
	}
	public void setCname(String cname) {
		this.cname = cname;
	}
	public Class() {}
	public Class(int cid, String cname) {
		super();
		this.cid = cid;
		this.cname = cname;
	}
}
UTIL(连接数据库和工具类)

连接数据库

package com.wangshaoyang.util;

import java.io.InputStream;
import java.security.KeyStore.ProtectionParameter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 提供了一组获得或关闭数据库对象的方法
 * 
 */
public class DBAccess {
	private static String driver;
	private static String url;
	private static String user;
	private static String password;

	static {// 静态块执行一次,加载 驱动一次
		try {
			InputStream is = DBAccess.class
					.getResourceAsStream("config.properties");

			Properties properties = new Properties();
			properties.load(is);

			driver = properties.getProperty("driver");
			url = properties.getProperty("url");
			user = properties.getProperty("user");
			password = properties.getProperty("pwd");

			Class.forName(driver);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 获得数据连接对象
	 * 
	 * @return
	 */
	public static Connection getConnection() {
		try {
			Connection conn = DriverManager.getConnection(url, user, password);
			return conn;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public static void close(ResultSet rs) {
		if (null != rs) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}

	public static void close(PreparedStatement ps) {
		if (null != ps) {
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}

	public static void close(Connection conn) {
		if (null != conn) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}

	public static void close(Connection con, PreparedStatement ps, ResultSet rs) {
		close(rs);
		close(ps);
		close(con);
	}

	public static boolean isOracle() {
		return "oracle.jdbc.driver.OracleDriver".equals(driver);
	}

	public static boolean isSQLServer() {
		return "com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver);
	}
	
	public static boolean isMysql() {
		return "com.mysql.cj.jdbc.Driver".equals(driver);
	}

	public static void main(String[] args) {
		Connection conn = DBAccess.getConnection();
		DBAccess.close(conn);
		System.out.println("isOracle:" + isOracle());
		System.out.println("isSQLServer:" + isSQLServer());
		System.out.println("isMysql:" + isMysql());
		System.out.println("数据库连接(关闭)成功");
	}
}
config.properties配置文件
#oracle9i
#driver=oracle.jdbc.driver.OracleDriver
#url=jdbc:oracle:thin:@localhost:1521:ora9
#user=test
#pwd=test


#sql2005
#driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
#url=jdbc:sqlserver://localhost:1423;DatabaseName=test
#user=sa
#pwd=sa

#sql2000
#driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
#url=jdbc:microsoft:sqlserver://localhost:1433;databaseName=unit6DB
#user=sa
#pwd=888888

#mysql5
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost/db_0608?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
user=root
pwd=123

通用的dao方法

BaseDao
package com.wangshaoyang.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class BaseDao<T> {
	
	public static interface CallBack<E>{
		//遍历ResultSet结果集
		public List<E> forEach(ResultSet rs) throws SQLException, Exception;
	}

	/**
	 * 分页查询
	 * @param sql 普通的sql
	 * @param pageBean 
	 * @return
	 */
	public List<T> executeQuery(String sql,PageBean pageBean,CallBack<T> callBack){
		Connection conn=null;
		PreparedStatement stmt=null;
		ResultSet rs=null;
		try {
			conn=DBAccess.getConnection();
			//判断是否分页
			if(null!=pageBean&&pageBean.isPagination()) {
				//第一次查询总记录数
				String countSql=this.getCountSql(sql);
				stmt=conn.prepareStatement(countSql);
				rs=stmt.executeQuery();
				if(rs.next()) {
					Object obj = rs.getObject(1);
					pageBean.setTotal(Integer.parseInt(obj.toString()));
				}
				//DBHelper.close(conn, stmt, rs);
				//第二次满足条件的分页数据集
				sql=this.getPagerSql(sql, pageBean);
			}
			stmt=conn.prepareStatement(sql);
			rs=stmt.executeQuery();
			//处理结果集
			return callBack.forEach(rs);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBAccess.close(conn, stmt, rs);
		}
		return null;
	}
	
	/**
	 * 将普通的Sql语句转换成查总记录数的Sql语句
	 * 例如:sql=="select * from t_book where bookname='123'"
	 * @param sql
	 * @return
	 */
	private String getCountSql(String sql) {
		return "select count(1) from ("+sql+") t1";
	}
	/**
	 * 将普通Sql语句转换成查询分页的Sql语句
	 * 例如:sql=="select * from t_book where bookname='123'"
	 * @param sql
	 * @param pageBean
	 * @return
	 */
	private String getPagerSql(String sql,PageBean pageBean) {
		return sql+" Limit "+pageBean.getStartIndex()+","+
					pageBean.getRows();
	}
}
工具类 CommonUtils
package com.wangshaoyang.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommonUtils {
	
	/**
	 * 根据ResultSet数据集,利用反射机制动态赋值并返回List<T>
	 * @param rs ResultSet数据集
	 * @param clazz 实体类对象
	 * @return 返回List实体集合
	 * @throws Exception
	 */
	public static <T> List<T> toList(ResultSet rs,Class<T> clazz) throws Exception{
		//定义实体集合
		List<T> lst=new ArrayList<T>();
		try {
			//获取ResultSet的metadata列信息
			ResultSetMetaData metaData = rs.getMetaData();
			//获取对象属性集合
			Field[] fields=clazz.getDeclaredFields();
			//循环ResultSet
			while(rs.next()) {
				//反射机制实例化
				T obj = clazz.newInstance();
				for (int i = 0; i < metaData.getColumnCount(); i++) {
					//获取列名
					String columnName=metaData.getColumnLabel(i+1).toUpperCase();
					for (Field field : fields) {
						//判断属性名与列名是否相同
						if(field.getName().toUpperCase().equals(columnName)) {
							//获取属性对应的set方法名,方法名首字母大写
							String methodName="set"+field.getName().substring(0, 1).toUpperCase()+field.getName().substring(1);
							//获取属性对应的set方法
							Method method = obj.getClass().getDeclaredMethod(methodName, field.getType());
							//设置访问权限
							method.setAccessible(true);
							//执行set方法,将数据存储到对象中的相应属性中
							method.invoke(obj, rs.getObject(columnName));
							break;
						}
					}
				}
				lst.add(obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lst;
	}
	
	/**
	 * 多表查询时返回结果集,利用反射机制赋值 
	 * @param rs
	 * @return 返回List<Map<String,Object>>
	 * @throws Exception
	 */
	public static List<Map<String,Object>> toList(ResultSet rs) throws Exception{
		//定义实体集合
		List<Map<String,Object>> lst=new ArrayList<Map<String,Object>>();
		//获取ResultSet的metadata列信息	
		ResultSetMetaData metaData = rs.getMetaData();
		Map<String,Object> set=null;
		while(rs.next()) {
			set=new HashMap<String,Object>();
			for (int i = 0; i < metaData.getColumnCount(); i++) {
				String columnName=metaData.getColumnLabel(i+1);
				set.put(columnName, rs.getObject(columnName));
			}
			lst.add(set);
		}
		return lst;
	}
}
工具类判空 StringUtils
package com.wangshaoyang.util;

public class StringUtils {
	// 私有的构造方法,保护此类不能在外部实例化
	private StringUtils() {
	}

	/**
	 * 如果字符串等于null或去空格后等于"",则返回true,否则返回false
	 * 
	 * @param s
	 * @return
	 */
	public static boolean isBlank(String s) {
		boolean b = false;
		if (null == s || s.trim().equals("")) {
			b = true;
		}
		return b;
	}
	
	/**
	 * 如果字符串不等于null或去空格后不等于"",则返回true,否则返回false
	 * 
	 * @param 
	 * @return
	 */
	public static boolean isNotBlank(String s) {
		return !isBlank(s);
	}
}
分页工具类 PageBean
package com.wangshaoyang.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * 分页工具类
 *
 */
public class PageBean {

	private int page = 1;// 页码

	private int rows = 10;// 页大小

	private int total = 0;// 总记录数

	private boolean pagination = true;// 是否分页

	private String url;
	private Map<String, String[]> paramMap = new HashMap<>();

//	定义初始化方法,存入上一次请求的值
	public void setRequest(HttpServletRequest req) {
		this.setPage(req.getParameter("page"));
		this.setRows(req.getParameter("rows"));
		this.setPagination(req.getParameter("pagination"));
		// getRequestURL获取到浏览器请求的全路径
		this.setUrl(req.getRequestURL().toString());
		// getParameterMap可以获取到一次url请求所携带的所有参数
		this.setParamMap(req.getParameterMap());

	}

	public void setPagination(String pagination) {
		if (StringUtils.isNotBlank(pagination)) {
			this.setPagination(!"false".equals(pagination));
		}
	}

	public void setRows(String rows) {
		if (StringUtils.isNotBlank(rows))
			this.setRows(Integer.valueOf(rows));

	}

	public void setPage(String page) {
		if (StringUtils.isNotBlank(page)) {
			this.setPage(Integer.valueOf(page));
		}
	}

	public PageBean() {
		super();
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Map<String, String[]> getParamMap() {
		return paramMap;
	}

	public void setParamMap(Map<String, String[]> paramMap) {
		this.paramMap = paramMap;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public void setTotal(String total) {
		this.total = Integer.parseInt(total);
	}

	public boolean isPagination() {
		return pagination;
	}

	public void setPagination(boolean pagination) {
		this.pagination = pagination;
	}

	/**
	 * 获得起始记录的下标
	 * 
	 * @return
	 */
	public int getStartIndex() {
		return (this.page - 1) * this.rows;
	}

	@Override
	public String toString() {
		return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
	}
	/**
	 * 获取到总页数
	 * @return
	 */
	public int getMaxPage() {
		return this.total % this.rows == 0 ? 
				this.total / this.rows : 
					(this.total / this.rows) + 1;
	}
	/**
	 * 获取下一页页码
	 * @return
	 */
	public int getNextPage() {
		return this.page < this.getMaxPage() ? this.page+1 : this.page;
	}
	/**
	 * 获取上一页页码
	 * @return
	 */
	public int getPreviousPage() {
		return this.page > 1 ? this.page-1 : this.page;
	}

}
写一个接口IstuDao
package com.wangshaoyang.dao;

import java.util.List;

import com.wangshaoyang.entity.Class;
import com.wangshaoyang.entity.Student;
import com.wangshaoyang.entity.Teacher;


public interface IStuDao {
	/**
	 * 增加
	 * @param stu
	 * @return
	 */
	public int add(Student stu);
	
	
	public int getRows(String str);
	
	public int update(Student stu,int sid);
	
	public int del(int sid);
	
	/**
	 * 分页查
	 * @param pageIndex
	 * @param pageSize
	 * @param str
	 * @return
	 */
	public List<Student> getAll(int pageIndex,int pageSize,String str);
	/**
	 * 查单个
	 * @param sid
	 * @return
	 */
	public Student getBySid(int sid);
	
	/**
	 * 自动增加Sid
	 * @return
	 */
	public int getSid();
	
	/**
	 * 查出所有班级
	 * @return
	 */
	public List<Class> getCid();
	
	/**
	 * 查出所有教员
	 * @return
	 */
	public List<Teacher> getTid();
}
然后实现接口 StuDao
package com.wangshaoyang.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.wangshaoyang.entity.Class;
import com.wangshaoyang.entity.Student;
import com.wangshaoyang.entity.Teacher;
import com.wangshaoyang.util.BaseDao;
import com.wangshaoyang.util.CommonUtils;
import com.wangshaoyang.util.DBAccess;
import com.wangshaoyang.util.PageBean;
import com.wangshaoyang.util.StringUtils;

public class StuDao extends BaseDao<Student> implements IStuDao  {
	
	Connection con=null;
	PreparedStatement ps=null;
	ResultSet rs=null;
		
	/**
	 * 分页查询
	 * @param pageIndex
	 * @param pageSize
	 * @param str
	 * @param colName
	 * @return
	 */
	public List<Student> getAll(int pageIndex,int pageSize,String str){
		List<Student> ls=new ArrayList<>();
		try {
			con=DBAccess.getConnection();
			ps=con.prepareStatement("select * from tb_stu "+str+" limit "+(pageIndex-1)*pageSize+","+pageSize+"");
		 	ResultSet rs=ps.executeQuery();
			while(rs.next()) {
				Student stu=new Student();
				stu.setSid(rs.getInt(1));
				stu.setSname(rs.getString(2));
				stu.setShobby(rs.getString(3));
				stu.setTid(rs.getInt(4));
				stu.setCid(rs.getInt(5));
				ls.add(stu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBAccess.close(con, ps, rs);
		}
		return ls;
	}
	public List<Student> List(Student stu,PageBean pageBean) throws InstantiationException, IllegalAccessException, IllegalArgumentException, SQLException {
		String sql="select * from tb_stu where 1=1 ";		
		if(StringUtils.isBlank(stu.getSname())) {
			sql+=" and sname like '%"+stu.getSname()+"%'";
		}
			return super.executeQuery(sql, pageBean,new CallBack<Student>() {

				@Override
				public java.util.List<Student> forEach(ResultSet rs) throws Exception {
						return CommonUtils.toList(rs,Student.class);
				}
			});
	}
	
	/**
	 * 查出所有教员
	 * @return
	 */
	public List<Teacher> getTid(){
		List<Teacher> ls=new ArrayList<>();
		try {
			con=DBAccess.getConnection();
			ps=con.prepareStatement("select * from tb_teacher");
			rs=ps.executeQuery();
			while(rs.next()) {
				Teacher t=new Teacher();
				t.setTid(rs.getInt(1));
				t.setTname(rs.getString(2));
				ls.add(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBAccess.close(con, ps, rs);
		}
		return ls;
	}
	
	
	/**
	 * 查出所有班级
	 * @return
	 */
	public List<Class> getCid(){
		List<Class> ls=new ArrayList<>();
		try {
			con=DBAccess.getConnection();
			ps=con.prepareStatement("select * from tb_class");
			rs=ps.executeQuery();
			while(rs.next()) {
				Class c=new Class();
				c.setCid(rs.getInt(1));
				c.setCname(rs.getString(2));
				ls.add(c);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBAccess.close(con, ps, rs);
		}
		return ls;
	}
	
	@Override
	public int add(Student stu) {
		int n=0;
		try {
			con=DBAccess.getConnection();
			ps=con.prepareStatement("insert into tb_stu values(?,?,?,?,?)");
			ps.setInt(1, stu.getSid());
			ps.setString(2, stu.getSname());
			ps.setString(3, stu.getShobby());
			ps.setInt(4, stu.getTid());
			ps.setInt(5, stu.getCid());
			n=ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBAccess.close(con, ps, rs);
		}
		return n;
	}
	
	@Override
	public int update(Student stu, int sid) {
		int n=0;
		try {
			con=DBAccess.getConnection();
			ps=con.prepareStatement("update tb_stu set sid=?,sname=?,shobby=?,tid=?,cid=? where sid=?");
			ps.setInt(1, stu.getSid());
			ps.setString(2, stu.getSname());
			ps.setString(3, stu.getShobby());
			ps.setInt(4, stu.getTid());
			ps.setInt(5, stu.getCid());
			ps.setInt(6, sid);
			n=ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBAccess.close(con, ps, rs);
		}
		return n;
	}

	@Override
	public int del(int sid) {
		int n=0;
		try {
			con=DBAccess.getConnection();
			ps=con.prepareStatement("delete from tb_stu where sid="+sid);
			n=ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBAccess.close(con, ps, rs);
		}
		return n;
	}

	@Override
	public Student getBySid(int sid) {
		String sql="select * from tb_stu where sid=?";
		List<Student> list = super.executeQuery(sql, null, new CallBack<Student>() {

			@Override
			public java.util.List<Student> forEach(ResultSet rs) throws SQLException, Exception {
				return CommonUtils.toList(rs, Student.class);
			}
		});
		if(null!=list||0!=list.size()) {
			return list.get(0);
		}
		else {
			return null;
		}
	}

	@Override
	public int getSid() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	/**
	 * 获取最大行数
	 * @param str
	 * @return
	 */
	public int getRows(String str){
		int n = 0;
		try {
			con=DBAccess.getConnection();
			ps=con.prepareStatement("select count(*) from tb_stu "+str+"");
			rs = ps.executeQuery();
			if(rs.next()){
				n = rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			DBAccess.close(con, ps, rs);
		}
		return n;
	}
	
	public Class getC(int cid) {
		Class c=new Class();
		try {
			con=DBAccess.getConnection();
			ps=con.prepareStatement("select * from tb_class where cid="+cid);
			rs=ps.executeQuery();
			if(rs.next()) {
				c.setCid(rs.getInt(1));
				c.setCname(rs.getString(2));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBAccess.close(con, ps, rs);
		}
		return c;
	}
	
	public Teacher getT(int tid) {
		Teacher t=new Teacher();
		try {
			con=DBAccess.getConnection();
			ps=con.prepareStatement("select * from tb_teacher where tid="+tid);
			rs=ps.executeQuery();
			if(rs.next()) {
				t.setTid(rs.getInt(1));
				t.setTname(rs.getString(2));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBAccess.close(con, ps, rs);
		}
		return t;
	}
	public List<Student> getAllName(){
		List<Student> ls=new ArrayList<>();
		try {
			con=DBAccess.getConnection();
			ps=con.prepareStatement("select sname from tb_stu");
		 	ResultSet rs=ps.executeQuery();
			while(rs.next()) {
				Student stu=new Student();
				stu.setSname(rs.getString(1));
				ls.add(stu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBAccess.close(con, ps, rs);
		}
		return ls;
	}
}
业务逻辑层 接口IStuBiz
package com.wangshaoyang.Biz;

import java.util.List;

import com.wangshaoyang.entity.Class;
import com.wangshaoyang.entity.Student;
import com.wangshaoyang.entity.Teacher;

public interface IStuBiz {
	
	/**
	 * 增加
	 * @param stu
	 * @return
	 */
	public int add(Student stu);	
public int getRows(String str);
	
	public int update(Student stu,int sid);
	
	public int del(int sid);
	
	/**
	 * 分页查
	 * @param pageIndex
	 * @param pageSize
	 * @param str
	 * @return
	 */
	public List<Student> getAll(int pageIndex,int pageSize,String str);
	/**
	 * 查单个
	 * @param sid
	 * @return
	 */
	public Student getBySid(int sid);
	/**
	 * 自动增加Sid
	 * @return
	 */
	public int getSid();
	
	/**
	 * 查出所有班级
	 * @return
	 */
	public List<Class> getCid();
	/**
	 * 查出所有教员
	 * @return
	 */
	public List<Teacher> getTid();
}
StuBiz
package com.wangshaoyang.Biz;

import java.util.List;

import com.wangshaoyang.dao.IStuDao;
import com.wangshaoyang.dao.StuDao;
import com.wangshaoyang.entity.Class;
import com.wangshaoyang.entity.Student;
import com.wangshaoyang.entity.Teacher;


public abstract class StuBiz implements IStuBiz{
	
	IStuDao isd=new StuDao();

	@Override
	public int add(Student stu) {
		// TODO Auto-generated method stub
		return isd.add(stu);
	}

	@Override
	public int getRows(String str) {
		// TODO Auto-generated method stub
		return isd.getRows(str);
	}

	@Override
	public int update(Student stu, int sid) {
		// TODO Auto-generated method stub
		return isd.update(stu, sid);
	}

	@Override
	public int del(int sid) {
		// TODO Auto-generated method stub
		return isd.del(sid);
	}

	@Override
	public List<Student> getAll(int pageIndex, int pageSize, String str) {
		// TODO Auto-generated method stub
		return isd.getAll(pageIndex, pageSize, str);
	}

	@Override
	public Student getBySid(int sid) {
		// TODO Auto-generated method stub
		return isd.getBySid(sid);
	}

	@Override
	public int getSid() {
		// TODO Auto-generated method stub
		return isd.getSid();
	}

	@Override
	public List<Class> getCid() {
		// TODO Auto-generated method stub
		return isd.getCid();
	}

	@Override
	public List<Teacher> getTid() {
		// TODO Auto-generated method stub
		return isd.getTid();
	}
}
主控制器 DispatcherServlet
package com.wangshaoyang.framework;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;

import org.apache.commons.beanutils.BeanUtils;


/**
 * 主控制器
 * @author 86135
 *
 */
public class DispatcherServlet extends HttpServlet {

	private static final long serialVersionUID = -7094025920085803724L;
	
	private ConfigModel configModel = null;
	
	
	//初始化方法
	public void init() {
		
		try {
			//将原来的读取框架的默认配置文件转变成读取可配置路径的配置文件
			String xmlPath = this.getInitParameter("xmlPath");
			if(xmlPath == null || "".equals(xmlPath)) {
				configModel = ConfigModelFactory.build();
			}
			else {
				configModel = ConfigModelFactory.build(xmlPath);
				}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
		
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		init();//初始化
		String url = req.getRequestURI();//Mvc/xxx.action
		url = url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
		
		//根据路径获取action的模型
		ActionModel actionModel = configModel.pop(url);
		if(actionModel==null) {
			throw new RuntimeException("你没有配置对应的子控制器Action!!!");
		}
		try {
			//原来子控制器的来源是map集合,这样的话子控制器被写死再map容器中,代码不够灵活
			//现在将子控制器配置方式存放再config.xml中,未来可以通过改变config.xml中的内容
			//随意给中央控制器添加子控制器
			Action action = (Action)Class.forName(actionModel.getType()).newInstance();
			
//			调用模型驱动接口,获取所需要操作的实体类,然后将jsp传递过来的参数,封装到实体类中
			if(action instanceof ModelDriver) {
				ModelDriver modelDriver=(ModelDriver)action;
				Object model = modelDriver.getModel();
				//封装属性
				BeanUtils.populate(model, req.getParameterMap());
			}
			
//			每个子控制器,都需要对结果进行对应的处理,要么重定向,要么转发,代码重复量较大
//			针对于这一现象,将其交给配置文件来处理
//			调用了增强版的子控制器来处理业务逻辑
			String code = action.execute(req, resp);
			ForwardModel forwardModel = actionModel.pop(code);
			if(forwardModel ==null) {
				throw new RuntimeException("你没有配置对应的子控制器Action的处理方式Forward!!!");
			}
			String jspPath = forwardModel.getPath();
			if(forwardModel.isRedirect()) {
				resp.sendRedirect(req.getContextPath()+jspPath);//拼接上路径,相当于绝对路径
			}
			else {
				req.getRequestDispatcher(jspPath).forward(req, resp);
			}
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
}
子控制器ActionSupport
package com.wangshaoyang.framework;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

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


/**
 * 之前的Action只能处理一个实体类的业务
 * 
 *  现在这个是增加版的子控制器
 *  凡是这个实体类的操作,对应方法都可以写在当前增强版的子控制器来完成
 * @author 86135
 *
 */
public class ActionSupport implements Action {

	@Override
	public final String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String methodName = req.getParameter("methodName");
		String code=null;
		try {
			//获取到类对象,再获取方法
			Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
			method.setAccessible(true);
			//具体调用了你自己所写的子控制器的方法来处理浏览器请求
			code=(String)method.invoke(this, req,resp);
		} catch (NoSuchMethodException | SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return code;
	}
}
ActionModel
package com.wangshaoyang.framework;

import java.util.HashMap;
import java.util.Map;

public class ActionModel {
	
	private String path;
	private String type;
	private Map<String, ForwardModel> fMap=new HashMap<>();
	
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public void push(ForwardModel forwardmodel) {
		fMap.put(forwardmodel.getName(),forwardmodel);
	}
	public ForwardModel pop(String name) {
		return fMap.get(name);
	}
}
ConfigModel
package com.wangshaoyang.framework;

import java.util.HashMap;
import java.util.Map;

public class ConfigModel {
	private Map<String, ActionModel> acmap=new HashMap<>();
	
	public void push(ActionModel actionmodel) {
		acmap.put(actionmodel.getPath(), actionmodel);
	}	
	public ActionModel pop(String path) {
		return acmap.get(path);
	}
}
ConfigModelFactory
package com.wangshaoyang.framework;

import java.io.InputStream;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;import com.sun.corba.se.spi.orbutil.fsm.Action;

public class ConfigModelFactory {
	
	public static ConfigModel build() throws Exception {
		return build("config.xml");
	}

	public static ConfigModel build(String string) throws Exception {
		ConfigModel configmodel=new ConfigModel();
		InputStream in = ConfigModelFactory.class.getResourceAsStream("config.xml");
		SAXReader reader=new SAXReader();
		Document doc = reader.read(in);
		ActionModel actionmodel=null;
		ForwardModel forwardmodel=null;
		List<Element> actionEles = doc.selectNodes("/config/action");
		for (Element actionEle : actionEles) {
			actionmodel=new ActionModel();
			//接下来需要往actionModel中填充内容
			actionmodel.setPath(actionEle.attributeValue("path"));
			actionmodel.setType(actionEle.attributeValue("type"));
			//拿到foward
			List<Element> forword = actionEle.selectNodes("forward");
			for (Element forwardEle : forword) {
				forwardmodel=new ForwardModel();
				//往forwardModel中填充内容
				forwardmodel.setName(forwardEle.attributeValue("name"));
				forwardmodel.setPath(forwardEle.attributeValue("path"));
				forwardmodel.setRedirect(!"false".equals(forwardEle.attributeValue("redirect")));
				actionmodel.push(forwardmodel);
			}
			configmodel.push(actionmodel);//调用存值方法
		}
		return configmodel;
	}
}
ForwardModel
package com.wangshaoyang.framework;

public class ForwardModel {
	
	private String name;
	private String path;
	private boolean redirect;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public boolean isRedirect() {
		return redirect;
	}
	public void setRedirect(boolean redirect) {
		this.redirect = redirect;
	}
}
模型驱动接口 ModelDriver
package com.wangshaoyang.framework;

/**
 * 模型驱动接口
 * 	是用来处理jsp页面传递过来的参数,
 * 	将所有的参数自动封装到实体类T中
 * @author 86135
 *
 * @param <T>
 */
public interface ModelDriver<T> {
	T getModel();
}
复选框 CheckboxTag
package com.wangshaoyang.tag;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class CheckboxTag extends BodyTagSupport {
	
	private static final long serialVersionUID = 3056626706793185309L;
	
	
	private String textKey;
	private String textVal;
	private List<Object> checkedVal=new ArrayList<>();
	private List<Object> item=new ArrayList<>();
	
	public List<Object> getItem() {
		return item;
	}
	public void setItem(List<Object> item) {
		this.item = item;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	public String getTextKey() {
		return textKey;
	}
	public List<Object> getCheckedVal() {
		return checkedVal;
	}
	public void setCheckedVal(List<Object> checkedVal) {
		this.checkedVal = checkedVal;
	}
	public void setTextKey(String textKey) {
		this.textKey = textKey;
	}
	public String getTextVal() {
		return textVal;
	}
	public void setTextVal(String textVal) {
		this.textVal = textVal;
	}
	
	public CheckboxTag() {
		super();
	}
	
	public CheckboxTag(String textKey, String textVal, List<Object> checkedVal, List<Object> item) {
	super();
	this.textKey = textKey;
	this.textVal = textVal;
	this.checkedVal = checkedVal;
	this.item = item;
}
	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			out.print(toHTML());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}
	private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		StringBuffer sb=new StringBuffer();
		String value;
		String html;
		for (Object obj : item) {
			Field textKeyfield = obj.getClass().getDeclaredField(textKey);
			textKeyfield.setAccessible(true);
			value=(String)textKeyfield.get(obj);
			Field textValfield = obj.getClass().getDeclaredField(textVal);
			textValfield.setAccessible(true);
			html=(String)textValfield.get(obj);
			if(checkedVal.contains(value)) {
				sb.append("<input checked type='checkbox' value='"+value+"' />"+html+"");
			}
			else {
				sb.append("<input type='checkbox' value='"+value+"' />"+html+"");
			}
		}
		return sb.toString();
	}
}
分页 PageTag
package com.wangshaoyang.tag;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.wangshaoyang.util.PageBean;

public class PageTag extends BodyTagSupport {
	
	private static final long serialVersionUID = -8476740758233598045L;
	private PageBean pageBean;
	
	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			out.write(toHTML());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}

	public String toHTML() {
//	pageBean=new PageBean();
	StringBuffer sb=new StringBuffer();
	//下一次请求提交到后台的表单html代码拼接
	sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/bootstrap.min.css\"/>");
	sb.append("<script type=\"text/javascript\" src=\"js/jquery-3.3.1.js\"></script>");
	sb.append("<script type=\"text/javascript\" src=\"js/bootstrap.min.js\"></script>");
	sb.append("<form id='pageBeanForm' action='"+pageBean.getUrl()+"' method='post'>");
	Map<String, String[]> paramMap = pageBean.getParamMap();
	if(paramMap!=null&&paramMap.size()>0) {
		for(Entry<String, String[]> entry:paramMap.entrySet()) {
			if(!"page".equals(entry.getKey())) {
				for(String val:entry.getValue()) {
					sb.append("<input type='hidden' name='"+entry.getKey()+"'> value='"+val+"'");
				}
			}
		}
	}
	sb.append("<input type='hidden' name='page'>");
	sb.append("</form>");
	//分页条html代码拼接
	sb.append("<div style='text-align: right; font-size: 12px;'>");
	sb.append(" 每页"+pageBean.getRows()+"条,共"+pageBean.getTotal()+"条,第"+pageBean.getPage()+"页,共"+pageBean.getMaxPage()+"页&nbsp;&nbsp;");
	sb.append(" <a href='javascript:gotoPage(1)' class='btn btn-default'>首页</a>&nbsp;&nbsp;<a");
	sb.append(" href='javascript:gotoPage("+pageBean.getPreviousPage()+")' class='btn btn-default'>上一页</a>&nbsp;&nbsp;<a");
	sb.append(" href='javascript:gotoPage("+pageBean.getNextPage()+")' class='btn btn-default'>下一页</a>&nbsp;&nbsp;<a");
	sb.append(" href='javascript:gotoPage("+pageBean.getMaxPage()+")' class='btn btn-default'>尾页</a>&nbsp;&nbsp;<input type='text'");
	sb.append(" id='skipPage'");
	sb.append(" style='text-align: center; font-size: 12px; width: 50px;'>&nbsp;&nbsp;<a");
	sb.append(" href='javascript:skipPage()' class='btn btn-default'>Go</a>");
	sb.append(" </div>");
	//分页所需要调用的js代码
	sb.append("<script type='text/javascript'>");
	sb.append("		function gotoPage(page) {");
	sb.append("		  document.getElementById('pageBeanForm').page.value = page;");
	sb.append("		  document.getElementById('pageBeanForm').submit();");
	sb.append("		}");
	sb.append("		function skipPage() {");
	sb.append("		  var page = document.getElementById('skipPage').value;");
	sb.append("		  if(!page || isNaN(page) || parseInt(page)<1 || parseInt(page)>"+pageBean.getMaxPage()+"){");
	sb.append("		  alert('请输入1~N的数字');");
	sb.append("		  return;");
	sb.append("		}");
	sb.append("		gotoPage(page);");
	sb.append("	}");
	sb.append("</script>");
	
		return sb.toString();
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
}
下拉框 SelectTag
package com.wangshaoyang.tag;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
 * 1、值的传递  id、name
 * 2、数据源		items
 * 3、展示列与数据存储列与实体类的对应关系		textKey  textVal
 * 4、数据回显  selectedVal
 * 5、可能下拉框有默认值(头标签)  headerTextKey  headerTextVal
 */
public class SelectTag extends BodyTagSupport{
	
	private static final long serialVersionUID = -6022855282722803239L;
	
	private String id;
	private String name;
	private List<Object> items=new ArrayList<>();
	private String textKey;  //id=textKey
	private String textVal;
	private String selectedVal;
	private String headerTextKey;
	private String headerTextVal;

	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			try {
				out.print(toHTML());
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
	private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		StringBuffer sb=new StringBuffer();
		sb.append("<select id='"+id+"' name='"+name+"'>");//初始select
		//判断头标签不能为空
		if(!(headerTextKey==null||"".equals(headerTextVal)||headerTextVal==null||"".equals(headerTextVal))) {
			sb.append("<option selected value='"+headerTextKey+"'>"+headerTextVal+"</option>");
		}
		Integer value;
		String html;
		for (Object obj : items) {
			//利用反射获取类对象的属性,也就是我们下拉框的显示值和数据值
			Field textKeyField = obj.getClass().getDeclaredField(textKey);
			textKeyField.setAccessible(true);
			value=(Integer)textKeyField.get(obj);
			Field textValField = obj.getClass().getDeclaredField(textVal);
			textValField.setAccessible(true);
			html=(String)textValField.get(obj);
			//设置回显,设置的值和你下拉框的值相同就默认选中
			if(selectedVal.equals(value+"")) {
				sb.append("<option selected value='"+value+"'>"+html+"</option>");
			}
			else {
				sb.append("<option value='"+value+"'>"+html+"</option>");
			}
		}
		sb.append("</select>");
		return sb.toString();
	}

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<Object> getItems() {
		return items;
	}
	public void setItems(List<Object> items) {
		this.items = items;
	}
	public String getTextKey() {
		return textKey;
	}
	public void setTextKey(String textKey) {
		this.textKey = textKey;
	}
	public String getTextVal() {
		return textVal;
	}
	public void setTextVal(String textVal) {
		this.textVal = textVal;
	}
	public String getSelectedVal() {
		return selectedVal;
	}
	public void setSelectedVal(String selectedVal) {
		this.selectedVal = selectedVal;
	}
	public String getHeaderTextKey() {
		return headerTextKey;
	}
	public void setHeaderTextKey(String headerTextKey) {
		this.headerTextKey = headerTextKey;
	}
	public String getHeaderTextVal() {
		return headerTextVal;
	}
	public void setHeaderTextVal(String headerTextVal) {
		this.headerTextVal = headerTextVal;
	}
	public SelectTag() {
		super();
	}
}
Jsp界面 index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>主页面</title>

<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>

<script type="text/javascript">
	
	function myf(){
		var go=$("#go").val();
		location.href="index.do?pid="+go+"";
	}	
	
	
</script>

</head>
<body>
		<div align="center">
		<form action="index.do" method="post">
		姓名:<input type="text" name="name" />
			教师:<select name="tid">
					 <option value="k">请选择教师</option>
					 <c:forEach items="${lst }" var="t">
					 	<option value="${t.tid}">${t.tname}</option>
					 </c:forEach>
				</select>
			班级:<select name="cid">
					<option value="k">请选择班级</option>
					<c:forEach items="${lsc}" var="c">
						<option value="${c.cid}">${c.cname}</option>
					</c:forEach>
				</select>
			<div class="form-group">
				学生爱好:
				 <label class="checkbox-inline">
					<input type="checkbox" name="shobby" value="足球" />足球
				</label>
				<label class="checkbox-inline">
				<input type="checkbox" name="shobby" value="撩妹" />撩妹
				</label>
				<label class="checkbox-inline">
				<input type="checkbox" name="shobby" value="唱歌" />唱歌
				</label>
				<label class="checkbox-inline">
				<input type="checkbox" name="shobby" value="撩汉子" />撩汉子
				</label>
				<input type="submit" value="查询" class="btn btn-default" />
				<a style="float: right;" href="add.jsp" class="btn btn-default">增加</a>
			</div>
			<table border="1px" class="table table-hover">
				<tr align="center">
					<td>学生编号</td>
					<td>学生姓名</td>
					<td>学生的教师</td>
					<td>学生所在班级</td>
					<td>学生爱好</td>
					<td>操作</td>
				</tr>
				<c:if test="${empty myls }" >
					<jsp:forward page="index.do"></jsp:forward>
				</c:if>
				<c:forEach items="${myls }" var="ls">
				<tr align="center">
					<td>${ls.sid}</td>
					<td>${ls.sname}</td>
					<td>${ls.t.tname}</td>
					<td>${ls.c.cname}</td>
					<td>${ls.shobby}</td>
					<td><a onclick="return confirm('你确定要删除吗')" href="del.do?sid=${ls.sid}" class="btn btn-default">删除</a>&nbsp;&nbsp;<a href="pre.do?sid=${ls.sid}" class="btn btn-default">修改</a></td>
				</tr>
				</c:forEach>
			</table>
		</form>
		</div>		
			<ul class="pager">
				<li><a href="index.do?pid=1">首页(1)</a></li>
				<li><a href="index.do?pid=${pageIndex>1?pageIndex-1:1}">上一页</a></li>
				<li><input width="20" id="go" type="text" name="pid" /><a href="javascript:myf()">GO</a></li>
				<li><a href="index.do?pid=${pageIndex<max?pageIndex+1:max}">下一页</a></li>
				<li><a href="index.do?pid=${max}">末页(${max})</a></li>
			</ul>
</body>
</html>
增加add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>增加学生</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>

</head>
<body>
	<h1 align="center">增加学生</h1>
	<form action="add.do" method="post">
	<div align="center">
	姓名:<input type="text" name="sname" /><br/><br/>
	教师:<select name="tid">
					 <option value="null">请选择教员</option>
					 <option value="1">龙哥</option>
					 <option value="2">王哥</option>
					 <option value="3">松哥</option>					 	
			</select><br/><br/>
	班级:<select name="cid">
					 <option value="null">请选择班级</option>
				 <option value="1">1501</option>
					 <option value="2">1502</option>
					 <option value="3">1503</option>
					 <option value="4">1504</option>
					 <option value="5">1505</option>
					 <option value="6">1506</option>
		</select><br/><br/>
		<div class="form-group">
				学生爱好:
				 <label class="checkbox-inline">
					<input type="checkbox" name="shobby" value="足球" />足球
				</label>
				<label class="checkbox-inline">
				<input type="checkbox" name="shobby" value="撩妹" />撩妹
				</label>
				<label class="checkbox-inline">
				<input type="checkbox" name="shobby" value="唱歌" />唱歌
				</label>
				<label class="checkbox-inline">
				<input type="checkbox" name="shobby" value="撩汉子" />撩汉子
				</label>
			</div>
				<input type="submit" value="增加" class="btn btn-default" />
				<input type="reset" value="清空" class="btn btn-default" />
				<input type="button" onclick="location.href='index.jsp'" value="取消" class="btn btn-default" />
				</div>
	</form>	
</body>
</html>
修改 update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改学生</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript">
	
	$(function(){
		var checked='${s.shobby}';
		var ck=checked.split(",");
		$.each(ck,function(n,val){
			$(":checkbox[name='shobby'][value="+val+"]").prop("checked",true);
		});
	})
	
	function myf(){
		
	}
	
</script>

</head>
<body>
	<h1 align="center">修改学生</h1>
	<form action="update.do" method="post">
	<div align="center">
	学号:<input type="text" name="sid" value="${s.sid}" /><br/><br/>
	姓名:<input type="text" name="sname" value="${s.sname}" /><br/><br/>
	教员:<select name="tid">
					 <c:forEach items="${lst }" var="t">
						 <c:if test="${s.t.tid==t.tid}">
						 	<option value="${t.tid}">${t.tname}</option>
						 </c:if>
					 </c:forEach>	
					  <c:forEach items="${lst }" var="t">
						 <c:if test="${s.t.tid!=t.tid}">
						 	<option value="${t.tid}">${t.tname}</option>
						 </c:if>
					 </c:forEach>			 	
			</select><br/><br/>
	班级:<select name="cid">
					<c:forEach items="${lsc}" var="c">
					<c:if test="${s.c.cid==c.cid}">
						<option selected="selected" value="${c.cid}">${c.cname}</option>
					</c:if>
					</c:forEach>
					 <c:forEach items="${lsc }" var="c">
					<c:if test="${s.c.cid!=c.cid}">
					 	<option value="${c.cid}">${c.cname}</option>
					 </c:if>
					 </c:forEach>	
		</select><br/><br/>
		<div class="form-group">
				学生爱好:
				 <label class="checkbox-inline">
					<input type="checkbox" name="shobby" value="1" />足球
				</label>
				<label class="checkbox-inline">
					<input type="checkbox" name="shobby" value="2" />撩妹
				</label>
				<label class="checkbox-inline">
					<input type="checkbox" name="shobby" value="3" />唱歌
				</label>
				<label class="checkbox-inline">
					<input type="checkbox" name="shobby" value="4" />撩汉子
				</label>
			</div>
				<input type="hidden" value="${s.sid}" name="sid1" />
				<input type="submit" value="修改" class="btn btn-default" />
				<a class="btn btn-default" href="index.jsp" >取消</a>
				</div>
	</form>
</body>
</html>

今天就更新到这里
喜欢的可以关注我
不定时更新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

听晚风续过晚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值