Java uses JSP Tag Files & JSP EL functions to create your own page template

Source: Internet
Author: User
Tags tld

1. Brief description:
After JSP 2.0, you no longer need to drastically define a bunch of tagsupport or bodytagsupport, using JSP Tag files technology to implement powerful page template technology. Here, combining project development, the application of tag files technology is briefly introduced. For detailed tutorials and information, please refer to Java EE Tutorial, which has detailed e-text information.
Http://docs.oracle.com/javaee/5/tutorial/doc/bnama.html
2. Definition Template:/web-inf/tags/subview.tag

  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
  3. <!--
  4. <%@ tag language= "java" pageencoding= "UTF-8" iselignored= "false"%>
  5. <%@ taglib prefix= "tags" tagdir= "/web-inf/tags/"%>
  6. <%@ attribute name= "id" required= "true"%>
  7. <%@ attribute name= "title" Required= "false"%>
  8. <%@ attribute Name= "Headstyle" required= "false" fragment= "true"%>
  9. <%@ attribute Name= "Headscript" required= "false" fragment= "true"%>
  10. <%@ attribute name= "Body" required= "false" fragment= "true"%>
  11. <%@ attribute Name= "Footscript" required= "false" fragment= "true"%>
  12. -
  13. <tags:header/>
  14. <!--custom CSS section--
  15. <jsp:invoke fragment= "Headstyle"/>
  16. <!--Custom JS section--
  17. <jsp:invoke fragment= "Headscript"/>
  18. <body class= "Pbody" id= "approveapply" >
  19. <div class= "Mainhd" >
  20. <p class= "Maintit" >${title}</p>
  21. </div>
  22. <div id= "__body__" >
  23. <!--custom Body--
  24. <jsp:invoke fragment= "Body"/>
  25. </div>
  26. <jsp:invoke fragment= "Footscript"/>
  27. <div id= "__footer__" >
  28. <tags:footer version= "${version}"/>
  29. </div>
  30. </body>

Copy Code

Where <tags:header> is the head template, <tags:footer> is the bottom template, the small template is then integrated into the large <tags:subView> template
2. Inherit the template, implement the specific page:/view/deploy/approveapply.jsp

  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%>
  3. <%@ taglib prefix= "tags" tagdir= "/web-inf/tags/"%>
  4. <%@ taglib prefix= "app" uri= "Http://risecloud.com/app"%>
  5. <tags:subview id= "approveapply" title= "Audit Request" >
  6. <jsp:attribute name= "Body" >
  7. <div id= "Maingrid" ></div>
  8. </jsp:attribute>
  9. <jsp:attribute name= "Footscript" >
  10. <!--overlay page style class--
  11. <script type= "Text/javascript" src= "${pagecontext.request.contextpath}/js/deploy/approveapplygrid.js?${app: Pageversion ()} "></script>
  12. <script type= "Text/javascript" src= "${pagecontext.request.contextpath}/js/deploy/approveapplydialog.js?${app: Pageversion ()} "></script>
  13. <script type= "Text/javascript" >
  14. <! [cdata[
  15. $ (function () {
  16. var Grid = Approveapplygrid.create ({
  17. Selector: ' #mainGrid ',
  18. URL:WINDOW.GETCCTXURL ('/deploy/approveapply!listgameapply '),
  19. Agree:function (ret) {
  20. Alert (' Agree ')
  21. },
  22. Deny:function (ret, rec) {
  23. Alert (' Agree ')
  24. }
  25. });
  26. });
  27. ]]>
  28. </script>
  29. </jsp:attribute>
  30. </tags:subView>

Copy Code

which
1. ID, title, body, Footscript, etc. are in the template area of <tabs:subView>, where each one is replaced.
2. ${app:pageversion ()} is a custom JSP EL Function that forces the user's browser to update the cache when a new version is released. Specifically defined as/WEB-INF/APP.TLD,

  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <taglib xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee web-jsptaglibrary_2_1.xsd" xmlns= "/HTTP/ Java.sun.com/xml/ns/javaee "xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "version=" 2.1 ">
  3. <tlib-version>1.0</tlib-version>
  4. <jsp-version>2.1</jsp-version>
  5. <short-name>app</short-name>
  6. <uri>http://risecloud.com/app</uri>;
  7. <display-name>app</display-name>
  8. <function>
  9. <name>pageVersion</name>
  10. <function-class>com.yy.game.risecloud.taglib.RiseCloundFunctionLib</function-class>
  11. <function-signature>java.lang.string pageversion () </function-signature>
  12. </function>
  13. </taglib>

Copy Code

Note that El function must be static
3. Concrete Effect: The red box outside the chart is a template, the red box is the Inheritance Template page implementation, both unified and convenient. Isn't that what you're dreaming of?

Attention:
1. JSP Tag files are placed in the/web-inf/tags/directory by default and are referenced directly using the file name. If other places, you must use the <jsp-config> declaration in Web. Xml!
2. The TLD of the JSP EL functions is placed under/web-inf/by default, and if other places must use <jsp-config> declaration in Web. Xml!
If they are placed in the default location, they will be loaded automatically when the Servlet container is started. No need to configure any more! For specific use, you can refer to Java EE Tutorial

Java uses JSP Tag Files & JSP EL functions to create your own page template

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.