SPRINGJDBC one-to-many relationships, and Java recursion, the implementation of JSP recursion

Source: Internet
Author: User

MAVEN compiles the SPRINGMVC+SPRING+SPRINGJDBC framework.

To implement a feature that is under a folder, may display n folders, and each folder may display N folders ....

Front Effect:

The methods in the controller are as follows:

@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";
}

The Pojo class is as follows:

Package Org.sakaiproject.courseware.pojo;

Import Java.util.Date;
Import java.util.List;

/**
* Description: Courseware Information
* @author Promise
* date:2015-08-04
*/
public class Cwinfo {
/** PRIMARY KEY */
Private Integer Cwid;
/** Courseware Name */
Private String Cwname;
/** Courseware Path */
Private String Cwpath;
/** Creator */
Private String CreateUser;
/** Creation Time */
Private Date Createtime;
/** Modify Time */
Private Date UpdateTime;
/** Modify People */
Private String UpdateUser;
/**-Parent Number */
Private Integer ParentID;
/** Tool Number */
Private String ToolID;
/** Courseware Description */
Private String cwcontent;
/** hidden 0 Hidden 1 Display */
Private Integer Ishidden;
/** contains a collection of Cwinfo objects */
Private list<cwinfo> Cwinfo; The point is I added a property that holds the parentid of the sub-courseware information equal to the Cwid collection of the parent courseware information
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;
}
}

In DAO, I use SPRINGJDBC to manipulate the database as follows:

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); This implements the recursive method of the query
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 {//springjdb C to implement the class, query with the

@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;
}
}

Front JSP page, I use a JSP page, nested another JSP page, as follows:

<%@ 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"/>//focus on this page is included, this page is called coursecontent.jsp, this is a recursive JSP page
</c:if>
</c:forEach>
</div>

The main JSP pages are as follows:

<%@ 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" >
&LT;TD headers= "checkboxes" >
${list.cwname}
</td>
</tr>
<c:if test= "${not Empty List.cwinfo}" >
<c:import url= "coursecontent.jsp"/>//focus is on the recursive JSP method called JSP here
</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>

SPRINGJDBC one-to-many relationships, and Java recursion, the implementation of JSP recursion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.