Recursive display of tree structure (tree-like BBS) in JSP page __js

Source: Internet
Author: User

1. Custom Label class:

javax.servlet.jsp.tagext.bodytagsupport--Import this Package

public class Subnodestag extends Bodytagsupport {

/**
*
*/
Private static final long serialversionuid = 1396571704883585147L;

Private Bbsmessage bbsmessage;

Private String Proposalid;

private int level = 0;

private static final String Date_format = "Yyyy/mm/dd HH:mm:ss";

/**
* @param bbsmessage
* The Bbsmessage to set
*/
public void Setbbsmessage (final bbsmessage bbsmessage) {
This.bbsmessage = Bbsmessage;
}

/**
* @param proposalid
* The Proposalid to set
*/
public void Setproposalid (final String proposalid) {
This.proposalid = Proposalid;
}

/**
* {@inheritDoc}
*/
@Override
public int Doendtag () throws Jspexception {
This.getsubnodes (Bbsmessage, proposalid);
return eval_page;
}

/**
* Get the child node for each layer of message
* @param bbsmessage Bbsmessage
* @param proposalid String
*/
private void Getsubnodes (final bbsmessage bbsmessage, final String proposalid) {
To get the first-level child node.
list<bbsmessage> subnodes = Bbsmessage.getsubnodes ();
level++;
if (subnodes.size () > 0) {
Traversing child nodes
for (Bbsmessage m:subnodes) {
try {
Pagecontext.getout (). Print ("<tr><td>");
for (int i = 0; I < level; i++) {
Pagecontext.getout (). Print ("");
}
Pagecontext.getout (). Print ("<a href=/" bulletinboard.do?proposalid=);
Pagecontext.getout (). print (Proposalid);
Pagecontext.getout (). Print ("&messageid=");
Pagecontext.getout (). Print (M.getmessageid ());
Pagecontext.getout (). Print ("/>");
Pagecontext.getout (). Print (Tagutils.converthtml (M.gettitle ()));
Pagecontext.getout (). Print ("</a>");
Pagecontext.getout (). Print ("亅");
Pagecontext.getout (). Print (Tagutils.converthtml (M.getdisplayusername ()));
Pagecontext.getout (). Print ("亅");
Pagecontext.getout (). Print (New SimpleDateFormat (Date_format). FORMAT (M.getcreateddatetime ());
Pagecontext.getout (). Print ("</td></tr>");
This.getsubnodes (M, proposalid);
level--;
catch (IOException IoE) {
Ioe.printstacktrace ();
}
}
}
}
}

This completes the definition of a custom label

2. Configuration:

In the. tld file, configure the following:

<tag>
<description>
Display the subnodes.
</description>
<name>subNodes</name>
<tag-class>xxx.products.sms.sourcing.web.tag.SubNodesTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<description>BbsMessageModel</description>
<name>bbsMessage</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>ProposalId</description>
<name>proposalId</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>

attribute indicates the parameters required for the label to be used.

3. Complete initialization of the bbsmessage (take to all child nodes):

Private list<bbsmessage> Getsubnodes (String Parentmessageid) throws Dataconnectexception, Datapropertyexception, DataAccessException {
Remove the first layer of child nodes in the DB layer
list<bbsmessage> subnodes = Bbsmessagedao.getbbsmessagebyparentmessageid (Parentmessageid);
if (subnodes.size () > 0) {
traversing the child nodes;
Bbsmessage.setsubnodes (Getsubnodes (Bbsmessage.getmessageid ()));
}
}
return subnodes;
}

4. Refer to the JSP page:

<table>
<c:if test= "${not Empty Bbsmessage.subnodes}" >
<nbs:subnodes bbsmessage= "${bbsmessage}"
Proposalid= "${proposalid}" ></nbs:subNodes>
</c:if>
</table>

This enables you to implement a tree-like message body in a page, similar to the capabilities of Windows Explorer, and to enable the display of messages.

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.