[Original] Writing Custom TAG WEB Application (JBuilderx Version)

xiaoxiao2021-03-06  41

Author: bluesky35 (blue) development environment: Windows2000 JBuilderX Tomcat4.0.6 Introduction: Hello everybody, I'm bluesky35 (blue), although very busy recently, but still did not forget to write a few original articles up and share with everyone, I hope you Can support me. This article is about custom TAG parsing. I believe everyone has used Struts's custom label library. Write You can display a Text box. When I first use Struts, I feel very magical. How is IE to identify this label and analyze it into a text box? Let's uncover the mystery of the custom label together. 1. Newly build a project in JBuilderx Taken myTaglib.2. Next, let's make a servlet, call the JSP file containing custom TAG through the servlet. Create a package called MyServlet.SRC in the Project Root Catalog, then select Menu File-> New-> Web -> Servlet Creating a servlet called MyServlet, Package Select MyServlet.src, Web Module's Web Module named WebModule1, as shown below: Modify MyServlet, code as follows: package myservlet.src;

Import javax.servlet. *; import javax.servlet.http. *; import java.io. *; import java.util. *;

Public class myservlet extends httpservlet {private static final string content_type = "text / html; charSet = shift_jis";

// Initialize Global Variables Public Void Init () THROWS ServleTexception {}

// Process the HTTP Get request public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType (CONTENT_TYPE); String scope = request.getParameter ( "txtscope"); request.getRequestDispatcher ( "/ jsp / bluesky .jsp "). Forward (Request, Response);

// Clean Up Resources Public Void Destroy () {}} is OK, the servlet is configured, let's take a look at the current environment, as shown below: Take a break, let's go to formally enter the custom TAG writing. 4. Right-click Module Directory to select New-> Directory, build a directory named JSP, then right-click the JSP directory Select New-> JSP new JSP file, the content is as follows: <% @ Page ContentType = "text / html CHARSET = GB2312 "%> <% @ Taglib prefix =" bluesky "URI =" Bluesky-taglib "%> bluesky </ title> <body bgcolor =" # fff "> <form method = "post"> <br> <br> <bluesky: text color = "# ff0000" blueskytext = "I am bluesky" name = "bluesky" /> </ form> </ body> </ html> perhaps you It has been seen, <bluesky: text color = "# ff0000" blueskytext = "i am bluesky" name = "bluesky" /> is the box we want to resolve, Bluesky: Text is our custom Tag, Color, BlueskyText and Name is our custom Property, we will parse in the code below. Ok, let's continue ^ - ^. 5. In order to parse the TAG in Bluesky.JSP, we must first define Property about TAG, right-click Module Directory Select New-> Directory, create a directory named TLDS, then right-click the TLDS directory Select New-> file, create a new TYPE to the TLD type, named myTLD TLD file, defined the information of the TAG-related Property, the content is as follows: < XML Version = "1.0" encoding = "gb2312"?> <! doctype taglib public "- // sun microsystems, Inc.//dtd jsp tag library 1.1 // en" "http://java.sun.com/ J2EE / DTDS / WEB-JSPTAGL Ibrary_1_1.dtd "></p> <p><taglib> <Tlibversion> 1.0 </ tlibversion> <jspversion> 1.1 </ jspition> <shortname> Bluesky Library </ ShortName></p> <p><! - text -> <tag> <name> text </ name> <tagclass> mytag.src.text </ tagclass> <bodycontent> jsp </ bodycontent> <info> my text tag </ info> < attribute> <name> color </ name> <required> false </ required> <rtexprvalue> true </ rtexprvalue> </ attribute> <attribute> <name> blueskytext </ name> <required> false </ required> < RTEXPRVALUE> TRUE </ RTEXPRVALUE> </ attribute> <attribute> <name> name </ name> <request> false </ required> <RTEXPRVALUE> TRUE </ RTEXPRVALUE> </ attribute> </ tag> </ taglib> Color, BlueskyText and Name have been defined inside. 6. Modify WebModule1 / Module Directory / Web-INF / Web.xml, add the following: <taglib> <taglib-uri> bluesky-taglib </ taglib-uri> < Taglib-location> / taglib-location> </ taglib> to this, the entire contents of Web.xml are as follows: <? XML Version = "1.0" encoding = "UTF-8"?> <! Doctype web-app public "- // sun microsystems, incaps//dtd web application 2.2 // en" "http://java.sun.com /j2ee/dtds/web-app_2_2.dtd "> <web-app> <display-name> Weservlet <servlet-name> MyServlet </ servlet-name> MyServlet </ servlet-name> <servlet-class> MyServlet .src.myservlet </ servlet-class> </ servlet> <servlet-mapping> <servlet-name> MyServlet </ servlet-name> <url-pattern> / myservlet </ url-pattern> </ servlet-mapping> <taglib> <</p> <p>Taglib-uri> bluesky-taglib </ taglib-uri> <taglib-location> /ts/mytld.tld </ taglib-location> </ taglib> </ web-app> well, our definition work has been completed , Take a break ^ - ^, then we have to write code for these definitions, which is to parse the code of Color, BlueskyText, and Name. 7. Create a package called MyTag.src in the project root directory, right-click MyTag .src Select New-> Class, establish a class named text, we write the parsing code inside, text.java content is as follows: package mytag.src; import javax.servlet.jsp.jspexception; import javax.servlet.jsp.jspexception; import javax.servlet.jsp.jspexception; import javax.servlet.jsp.jspexception; import javax.servlet.jsp.jspexce .JspWriter; import javax.servlet.jsp.tagext.TagSupport; import javax.servlet.jsp.tagext.TryCatchFinally; public class text extends TagSupport implements TryCatchFinally {private String color = ""; private String text = ""; private String name = "" "" "" "" "" "; Public int dostarttag () throws jspexception {return skip_body;}</p> <p>Public void Docatch (throwable arg0) throws throwable {}</p> <p>Public int doendtag () THROWS JSPEXCE = pageContext.get (); html = "<input type = /" text / " " name = / "" this.name "/" " " Value = / "" this.text "/" " " style = / "background-color:" this.color "/" /> "; out.print (html);} catch (java.io .IoException e) {E.PrintStackTrace ();} returnval_page;}</p> <p>Public void Dofinally () {}</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-70437.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="70437" 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.046</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 = 'Y6IWDz7KrxTrreCnv5WPkc72R6LGEywD_2BaYumDxAVPk_2B9X0gBB_2FwU_2FNuoiLGEgU4zx0cEob4w7r2xxO1FJ9vwA_3D_3D'; 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>