maven编译,springMVC+spring+springJDBC框架。
要实现的功能是一个文件夹下,可能显示n个文件夹,每个文件夹下又可能显示n个文件夹。。。。
前台效果:
controller中的方法如下:
@RequestMapping(value="/index",method=RequestMethod.GET)
public String getIndex(HttpServletRequest request,@RequestParam(required=false) Integer isHidden){
if(isHidden==null) isHidden = 1;//1 showHidden
request.setAttribute("list", cwInfoService.getAllCwInfo(isHidden));
return "/jsp/coursewareSelectAll";
}
pojo类如下:
package org.sakaiproject.courseware.pojo;
import java.util.Date;
import java.util.List;
/**
* 描述:课件信息
* @author Promise
* Date:2015-08-04
*/
public class CwInfo {
/**主键*/
private Integer cwId;
/**课件名称*/
private String cwName;
/**课件路径*/
private String cwPath;
/**创建人*/
private String createUser;
/**创建时间*/
private Date createTime;
/**修改时间*/
private Date updateTime;
/**修改人*/
private String updateUser;
/**父级编号*/
private Integer parentId;
/**工具编号*/
private String toolId;
/**课件描述*/
private String cwContent;
/**是否隐藏 0隐藏 1显示*/
private Integer isHidden;
/**包含有cwinfo对象的集合*/
private List<CwInfo> cwInfo; //重点是我添加了一个属性,用于存放子课件信息的parentId等于父课件信息的cwId用的集合
public List<CwInfo> getCwInfo() {
return cwInfo;
}
public void setCwInfo(List<CwInfo> cwInfo) {
this.cwInfo = cwInfo;
}
public Integer getIsHidden() {
return isHidden;
}
public void setIsHidden(Integer isHidden) {
this.isHidden = isHidden;
}
public Integer getCwId() {
return cwId;
}
public void setCwId(Integer cwId) {
this.cwId = cwId;
}
public String getCwName() {
return cwName;
}
public void setCwName(String cwName) {
this.cwName = cwName;
}
public String getCwPath() {
return cwPath;
}
public void setCwPath(String cwPath) {
this.cwPath = cwPath;
}
public String getCreateUser() {
return createUser;
}
public void setCreateUser(String createUser) {
this.createUser = createUser;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public String getUpdateUser() {
return updateUser;
}
public void setUpdateUser(String updateUser) {
this.updateUser = updateUser;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public String getToolId() {
return toolId;
}
public void setToolId(String toolId) {
this.toolId = toolId;
}
public String getCwContent() {
return cwContent;
}
public void setCwContent(String cwContent) {
this.cwContent = cwContent;
}
}
dao中,我用springJDBC操作数据库如下:
package org.sakaiproject.courseware.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.sakaiproject.courseware.dao.CwInfoDao;
import org.sakaiproject.courseware.pojo.CwInfo;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
public class CwInfoDaoImpl implements CwInfoDao {
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@SuppressWarnings("unchecked")
public List<CwInfo> findCwInfo(Integer cwId,Integer isHidden){
String sql = "select * from cw_info where parent_id="+cwId;
if(isHidden==1) sql=sql+" and is_hidden="+isHidden;
List<CwInfo> list = jdbcTemplate.query(sql, new cwMapper());
for (int i = 0; i < list.size(); i++) {
List<CwInfo> cwList = this.findCwInfo(list.get(i).getCwId(),isHidden); //这里实现了查询的递归方法
list.get(i).setCwInfo(cwList);
}
return list;
}
@Override
@SuppressWarnings("unchecked")
public List<CwInfo> getAllCwInfo(Integer isHidden){
String sql = "select * from cw_info where parent_id=0";
if(isHidden==1) sql=sql+" and is_hidden="+isHidden;
List<CwInfo> countList = jdbcTemplate.query(sql,new cwMapper());
for (int i = 0; i < countList.size(); i++) {
List<CwInfo> cwList = this.findCwInfo(countList.get(i).getCwId(),isHidden);
countList.get(i).setCwInfo(cwList);
}
return countList;
}
class cwMapper implements RowMapper { //springJDBC要实现的类,查询用的
@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
CwInfo cw = new CwInfo();
cw.setCreateTime(rs.getDate("create_time"));
cw.setCreateUser(rs.getString("create_user"));
cw.setCwContent(rs.getString("cw_content"));
cw.setCwId(rs.getInt("cw_id"));
cw.setCwName(rs.getString("cw_name"));
cw.setCwPath(rs.getString("cw_path"));
cw.setParentId(rs.getInt("parent_id"));
cw.setToolId(rs.getString("tool_id"));
cw.setUpdateTime(rs.getDate("update_time"));
cw.setUpdateUser(rs.getString("update_user"));
cw.setIsHidden(rs.getInt("is_hidden"));
return cw;
}
}
前台jsp页面,我用的是一个jsp页面,嵌套另一个jsp页面,如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<div name="spreadContent">
<c:forEach items="${list.cwInfo }" var="list">
<tr class="odd">
<td headers="expansion">
${list.cwName }
</td>
</tr>
<c:if test="${not empty list.cwInfo }">
<c:import url="courseContent.jsp" /> //重点是这里包含本页面,本页面就叫做courseContent.jsp,这是递归jsp页面
</c:if>
</c:forEach>
</div>
主要的jsp页面如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<body>
<table>
<div id="content-content">
<c:if test="${not empty list }">
<c:forEach items="${list }" var="list">
<tr class="odd">
<td headers="checkboxes">
${list.cwName }
</td>
</tr>
<c:if test="${not empty list.cwInfo }">
<c:import url="courseContent.jsp" /> //重点是这里调用了jsp的递归jsp方法
</c:if>
</c:forEach>
</c:if>
</div>
</c:forEach>
</c:if>
</div>
</table>
</form>
</div>
</body>
<script type="text/javascript" src="/library/js/jquery/jquery-1.9.1.min.js">
</script>
</html>