Struts principles and practices (8)

xiaoxiao2021-03-06  15

Struts Principles and Practice (8) Author: Zhang Mei Luo Kuaibo dispatch time: 2004.11.05

In the previous article, the JavaScript Implementation Class Decoction menu is introduced. This article continues to introduce an example of a tree menu using existing JavaScript code to form a tree menu. Everyone knows that the tree menu has a wide range of uses in the app. There are many ways to implement the tree type menu. This article describes the understanding of understanding, similar to the method of the last article: is to save the node of the tree menu in the database table (of course, in the actual project, node Information is often not placed in a single table. For example: in a permission management system, this information may be placed in the user table, role table, menu, etc., as long as you try to get the results of the query and below The content given is similar to the content. As long as some database knowledge is not difficult, the detailed implementation details are beyond the theme of this article, not here to say). By the data access object, it is placed in a collection object from the database, and the collection object is passed to the client, and then use a existing JavaScript code - DTREE (a free JavaScript program) to operate the collection The data. After the big direction is determined, we will make it specific to implement it. According to DTREE requirements, let's build a database table to store the node information of the tree, named functions, the structure is as follows:

ID field: varchar 10 primary key - node identification code

PID field: varchar 10 not null - parent node identification code

Name field: varchar 20 not null

URL field: VARCHAR 50 NOT

NULL - This field is stored at the time of clicking this node, such as a URL of a page),

In order not to make this article too long, no corresponding page is given,

You can enter an alphabet such as: a to enable this example.

Title field: VARCHAR 20

Target Field: VARCHAR 10

Icon Field: VARCHAR 20

Iconopen field: VARCHAR 20

Opened field: char 1 Enter some records in the table to supply later:

0, -1, my permissions, JavaScript: void (0);

00, 0, user management, javascript: void (0);

0001,00, create new users;

0002,00, delete users;

01, 0, article management, JavaScript: void (0);

0101, 01, add new articles;

0102, 01, modify articles;

0103, 01, delete articles; here, the database preparation work will take a paragraph. The next job We are still in the MYSTRUTS project previously introduced. Write a named: Functionsform's ActionForm, which is as follows:

Package Entity;

Import org.apache.struts.Apache.struts.action. *;

Import javax.servlet.http. *;

Public class functionsform extends actionform {

PRIVATE STRING ICON;

Private string iConopen;

Private string id;

PRIVATE STRING NAME;

PRIVATE STRING OPENED;

PRIVATE STRING PID;

PRIVATE STRING TARGET;

PRIVATE STRING TITLE;

Private string URL;

Public string geticon () {

Return icon;

}

Public void seticon (String icon) {

THIS.ICON = icon;

}

Public String geticonopen () {

Return Iconopen;

}

Public void seticonopen (String iconopen) {

this.iconopen = iconopen;

}

Public string getId () {

Return ID;

}

Public void setid (String ID) {

THIS.ID = ID;

}

Public string getname () {

Return Name;

}

Public void setname (String name) {

THIS.NAME = Name;

}

Public string getopet () {

Return OPENED;

}

Public void setpened (string opened) {

THIS.OPENED = OPENED;

}

Public string getpid () {

Return PID;

}

Public void setpid (string pid) {

THIS.PID = PID;

}

Public string gettarget () {

Return Target;

}

Public void setTarget (String Target) {

THIS.TARGET = Target;

}

Public string gettitle () {

Return Title;

}

Public void setTitle (String Title) {

THIS.TITLE = Title;

}

Public string geturl () {

Return URL;

}

Public void seturl (String URL) {

this.url = URL;

}

} Because the data of our tree node is stored in the database table, then you want to make a data access object class, the name is: functionsDao.java, the code is as follows:

Package DB;

Import java.sql. *;

Import java.util. *;

Import Entity.Functionsform;

Public class functionsdao {

Private static connection con = NULL;

Public functionsdao (connect con) {

THIS.CON = con;

}

Public static collectiment findtree () {

PreparedStatement PS = NULL;

ResultSet RS = NULL;

ArrayList List = New ArrayList ();

String SQL = "SELECT * from functions";

Try {

IF (con?isclosed ()) {

Throw New IllegalStateException ("Error.unexpected");

}

PS = con.preparestatement (SQL);

RS = ps.executeQuery ();

While (rs.next ()) {

Functionsform Functionsform = New functionsform (); functionsform.setid (rs.getstring ("id"));

Functionsform.SetPid (Rs.getstring ("PID"));

Functionsform.SetName (Rs.getstring ("name");

Functionsform.SetURL (RS.GetString ("URL"));

Functionsform.Settitle (RS.GetString ("Title");

Functionsform.SetTarget (RS.GetString ("Target");

Functionsform.Seticon (rs.getstring ("icon"));

Functionsform.Seticonopen ("iconopen");

Functionsform.Setopened (Rs.getstring ("OPENED"));

List.add (functionsform);

}

Return List;

}

Catch (SQLException E) {

E.PrintStackTrace ();

Throw new runtimeException ("error.unexpected");

}

Finally {

Try {

IF (PS! = NULL)

ps.close ();

IF (rs! = null)

Rs.close ();

} catch (sqlexception e) {

E.PrintStackTrace ();

Throw new runtimeException ("error.unexpected");

}

}

}

} This is worth noting that some programs we have seen in the past, such as: Some ASP programs often simply use the recursive call method to find each node of the tree. This is still some useful, but this processing method also has a fatal weakness, which is repeated database query, and more applications for some nodes, the impact on application performance is very large. So sometimes it is difficult to accept; and in most cases in the actual application, the depth of the tree is often limited, such as: the tree used for accounting accounts is generally up to six layers. Another example: the case of the web function menu, the principle of web design is: reaching the final destination, the number of mouse clicks is not more than three times. Therefore, consider the efficiency of the query when the table actually designed the storage tree structure. To determine the maximum depth of the tree, try to optimize the query statement, reduce the number of queries, to improve the performance of the application while reducing the load load. This example corresponds to the name of the Action, which is as follows:

Package action;

Import Entity. *;

Import org.apache.struts.Apache.struts.action. *;

Import javax.servlet.http. *;

Import javax.sql.datasource;

Import java.sql.connection;

Import java.sql.sqlexception;

Import java.util.collection;

Import db.functionsdao;

Public class functionSaction extends action {

Public ActionForward Execute (ActionMApping ActionMApping, Actionform Actionform, HttpservletRequest HttpServletRequest, HttpServletResponse HttpservletResponse)

{

DataSource DataSource;

CONNECTION CNN = NULL;

ActionerroS Errors = new actionerrors ();

Try {

DataSource = GetDataSource (HttpServletRequest, "a");

CNN = DataSource.getConnection ();

Functionsdao functionsdao = new functionsdao (cnn);

Collection col = functionsdao.findtree ();

HTTPSERVLETREQUEST.SetAttribute ("treelist", col);

Return ActionMapping.FindForward ("Success");

}

Catch (throwable e) {

E.PrintStackTrace ();

// throw new runtimeException ("Failed to connect to Database");

Actionerror Error = New ActionError (E.getMessage ());

Errors.Add (ActionerRors.global_Error, Error);

}

Finally {

Try {

IF (CNN! = NULL)

CNN.Close ();

}

Catch (SQLException E) {

Throw new runtimeException (E.GetMessage ());

}

}

SaveerRors (httpservletRequest, Errors);

Return ActionMapping.FindForward ("fail");

}

} Add the following in the Struts-Config.xml file:

Type = "action.functions" VALIDATE = "false">

For the corresponding configuration

We also provide a JSP page that displays the error message, which is as follows:

<% @ Page ContentType = "Text / HTML; Charset = UTF-8"%>

<% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>

</p> <p>Genericerror</p> <p></ title></p> <p><link href = "css / mycss.css" rel = "stylesheet" Type = "text / css"></p> <p></ hEAD></p> <p><body bgcolor = "# ffffff"></p> <p><html: errors /></p> <p></ body></p> <p></ html> Let's take a look at the page code we show the tree menu. It can be seen from the configuration that the name of the page is TestDtree.jsp, the code is as follows:</p> <p><% @ Page ContentType = "Text / HTML; Charset = UTF-8"%></p> <p><% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%></p> <p><% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%></p> <p><% @ Taglib URI = "/ Web-INF / STRUTS-LOGIC.TLD" prefix = "logic"%></p> <p><html></p> <p><HEAD></p> <p><Title></p> <p>Testdtree</p> <p></ title></p> <p><link rel = "stylesheet" href = "css / dtree.css" type = "text / css" /></p> <p></ hEAD></p> <p><body bgcolor = "# eeeeee"></p> <p><body leftmargin = "0" TopMargin = "0"> <Table Width = "180"></p> <p><TR> <TD Height = "300" VALIGN = "TOP" nowrap></p> <p><script type = "text / javascript" src = "js / dtree.js"> </ script></p> <p><script type = 'text / javascript'></p> <p>Tree = New DTREE ('Tree');</p> <p>Tree.config.folderlinks = false;</p> <p>Tree.config.useCookies = false;</p> <p><logic: orthrate id = "functionsform" name = "treelist" scope = "request"</p> <p>TYPE = "Entity.FunctionsForm"></p> <p>Tree.Add ("<bean: write name =" /> "/>", "<bean: writename =" "/>", "<bean: write name =" Functionsform "Property =" "</p> <p>Property = "Name" /> "," <bean: write name = "functionsform"</p> <p>Property = "URL" /> "," <bean: write name = "functionsform"</p> <p>Property = "Title" /> "," <bean: write name = "functionsform"</p> <p>Property = "Target" /> "," <bean: write name = "functionsform" Property = "iCON" /> ");</p> <p></ logic: Iterate></p> <p>Document.write (TREE);</p> <p></ script></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ body></p> <p></ html> From <script src = "js / dtree.js" type = "text / javascript"> </ script> can be seen, we have to build a directory named JS in the mystruts directory and download DTREE file DTREE.JS is placed in this directory. Then, in the mystruts directory, a directory named IMG and named CSS is built, and the icon and stacked style form files used in DTREE are separately in the corresponding directory. For the use of DTREE, please refer to its documentation, such as: API.html. The author is here to thank the DTREE's author to provide us with such a clear JavaScript program! Now, you can compile this example program. After compiling, enter: http://127.0.0.1: 8080 / mystruts / functions can see the running effect. The effect is:</p> <p>Note: DTREE download address is: http://www.destroydrop.com/javascripts/tree/</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-47377.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="47377" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.043</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'MZ3qTZl4EKSMlIYP3hmL9vQG5dzQpX7RBW3DXEXVG5RJbizlZj7bBYLTdAW6prAx_2FTL_2BewK9dOS4X4xy'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>