標籤:
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>
springJDBC一對多關聯性,以及Java遞迴,jsp遞迴的實現