1. TypeDao.java
public Type select(int id) throws SQLException{ QueryRunner r = new QueryRunner(DBUtil.getDataSource()); String sql = "select * from type where id = ?"; return r.query(sql, new BeanHandler<Type>(Type.class),id); }
2. TypeService.java
public Type select(int id){ Type t = null; try { t = tDao.select(id); } catch (SQLException e) { e.printStackTrace(); } return t; }
3. GoodsListServlet.java
private TypeService tService = new TypeService(); Type t = null; if(id != 0) { t = tService.select(id); } request.setAttribute("t", t);
4. goods_list.jsp
<h2><c:choose><c:when test="${empty t }">全部系列</c:when><c:otherwise>${t.name }</c:otherwise></c:choose></h2>
效果