zoukankan      html  css  js  c++  java
  • javaweb框架--自定义标签与freemaker结合

    http://blog.csdn.net/myfmyfmyfmyf/article/details/8960299

    很有用但是不不知道怎么说,写个例子,总之方便多了,并且容易管理,重复利用强

    1、自定一个类,实现 javax.servlet.jsp.tagext.Tag;(PageTag.java)

    2、建立一个tld文件(myfTag.tld)

    3、建立一个freemaker文件*.ftl(page.ftl)

    4、建立jsp页面,导入标签(<%@taglib prefix="myf" uri="/muyunfei"%>)

    5、jsp中使用( <myf:page action="/ftlhelloword" curpage="1"></myf:page>)

    6、效果,以后使用很方便,如果需要修改直接改freemaker就可以

    ---------------------------------tag类开始------------------------------------------

    [java] view plaincopy
     
    1. public class PageTag  implements Tag {  
    2.   
    3.     public PageContext pagecontex;  
    4.     public JspWriter out;  
    5.       
    6.     //自定义属性,当前页  
    7.     private String curpage;  
    8.       
    9.     //自定义属性,跳转路径  
    10.     private String action;  
    11.       
    12.     //设置页面内容  
    13.     public void setPageContext(PageContext pc) {  
    14.         pagecontex = pc;  
    15.         out = pc.getOut();  
    16.         //再次方法中不能获取属性值  
    17.     }  
    18.       
    19.     //结束  
    20.     @SuppressWarnings("unchecked")  
    21.     public int doEndTag() throws JspException {  
    22.   
    23. /*freemarker生成模板...开始*/  
    24.          Configuration cfg = new Configuration();  
    25.          //指定freemarker模板位置  
    26.          cfg.setServletContextForTemplateLoading( pagecontex.getServletContext(), "WEB-INF/templates");  
    27.   
    28.             try {  
    29.                 Map root = new HashMap();  
    30.                 root.put("curpage", curpage);  
    31.                 root.put("action", action);  
    32.                 root.put("path",pagecontex.getServletContext().getContextPath());  
    33.                 //得到模板  
    34.                 Template templ = cfg.getTemplate("page.ftl");  
    35.                 //输出模板  
    36.                 templ.process(root, out);  
    37.             } catch (TemplateException e) {  
    38.                 // TODO Auto-generated catch block  
    39.                 e.printStackTrace();  
    40.             } catch (IOException e) {  
    41.                 // TODO Auto-generated catch block  
    42.                 e.printStackTrace();  
    43.             }  
    44. /*freemarker生成模板...结束*/  
    45.          return 0;  
    46.               
    47.     }  
    48.   
    49.     //开始  
    50.     public int doStartTag() throws JspException {  
    51.         return 0;  
    52.     }  
    53.   
    54.     public Tag getParent() {  
    55.         return null;  
    56.     }  
    57.   
    58.     //释放控件  
    59.     public void release() {  
    60.           
    61.     }  
    62.     public void setParent(Tag t) {  
    63.           
    64.     }  
    65.   
    66.     //-----------get set  
    67.       
    68.     public String getCurpage() {  
    69.         return curpage;  
    70.     }  
    71.   
    72.     public void setCurpage(String curpage) {  
    73.         this.curpage = curpage;  
    74.     }  
    75.   
    76.     public String getAction() {  
    77.         return action;  
    78.     }  
    79.   
    80.     public void setAction(String action) {  
    81.         this.action = action;  
    82.     }  
    83.   
    84. }  

    ---------------------------------tag类结束------------------------------------------

    ---------------------------------tld文件开始------------------------------------------

    [html] view plaincopy
     
    1. <?xml version="1.0" encoding="UTF-8" ?>  
    2.   
    3. <taglib xmlns="http://java.sun.com/xml/ns/javaee"  
    4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"  
    6.     version="2.1">  
    7.       
    8.   <description>JSTL tagTest core library</description>  
    9.   <display-name>myTag</display-name>  
    10.   <tlib-version>1.1</tlib-version>  
    11.   <short-name>myf</short-name><!-- 用来引入时的名字-->  
    12.   <uri>/muyunfei</uri><!-- 用来引入时的地址-->  
    13.    
    14.   
    15.   <tag>  
    16.     <description>  
    17.         pageTag<!--描述 -->  
    18.     </description>  
    19.     <name>page</name><!--标签的名字-->  
    20.     <tag-class>tag.mytag.page.PageTag</tag-class><!-- 对应的java类,要写全-->  
    21.     <body-content>JSP</body-content>  
    22.       
    23.     <attribute><!-- 属性,可以多个-->  
    24.         <name>curpage</name><!-- 自己在java文件中定义的私有变量 -->  
    25.         <required>true</required<!-- 标签是否必须该属性 -->  
    26.         <rtexprvalue>false</rtexprvalue>  <!-- 是否支持表达式 -->  
    27.     </attribute>  
    28.     <attribute>  
    29.         <name>action</name><!-- 自己在java文件中定义的私有变量 -->  
    30.         <required>true</required<!-- 标签是否必须该属性 -->  
    31.         <rtexprvalue>true</rtexprvalue>  <!-- 是否支持表达式 -->  
    32.     </attribute>  
    33.   </tag>  
    34.   
    35. </taglib>  

    ---------------------------------tld文件结束------------------------------------------

    ---------------------------------freemaker文件*.ftl(page.ftl)     开始------------------------------------------

    [plain] view plaincopy
     
    1. <div class="grid-outPagerImg"  onclick="endpage()"  style="float:right;padding-top: 0px">  
    2.   <img  alt="最后页" border="0"   
    3.     src="${path}/images/last.png"   
    4.       
    5.     style="cursor:hand;" onmouseout="this.src='${path}/images/last.png'"   
    6.     onmouseover="this.src='${path}/images/lasth.png'">  
    7.     </img>  
    8. </div>      
    9. <div class="grid-inPagerImg "  onclick="next()" style="float:right;padding-top: 1px">   
    10.       <img  alt="后一页" border="0"   
    11.     src="${path}/images/next.png"   
    12.       
    13.     style="cursor:hand;" onmouseout="this.src='${path}/images/next.png'"   
    14.     onmouseover="this.src='${path}/images/nexth.png'">  
    15.     </img>  
    16. </div>  
    17. <div class="grid-pagerText" style="float:right;padding-top: 2px"> 页/共<label id="totilepage"></label>页</div>  
    18. <input type="text"  id="curpage"  style=" 20px;float:right"/>  
    19. <div class="grid-pagerText" style="float:right;padding-top: 2px"> 第 </div>  
    20. <div class="grid-inPagerImg " onclick="javascript:alert('${action}?curpage=${curpage}')"" style="float:right;padding-top: 1px">     
    21.       <img  alt="前一页" border="0"   
    22.     src="${path}/images/prev.png"   
    23.        
    24.     style="cursor:hand;" onmouseout="this.src='${path}/images/prev.png'"   
    25.     onmouseover="this.src='${path}/images/prevh.png'">  
    26.     </img>  
    27. </div>  
    28.  <div class="grid-outPagerImg"  onclick="javascript:alert('${action}?curpage=${curpage}')" style="float:right;padding-top: 0px">  
    29.     <img  alt="第一页" border="0"   
    30.     src="${path}/images/first.png"   
    31.        
    32.     style="cursor:hand;" onmouseout="this.src='${path}/images/first.png'"   
    33.     onmouseover="this.src='${path}/images/firsth.png'">  
    34.     </img>          
    35.  </div>  
    36.  <div class="grid-fnCreatePagerInnerHtml" id="ajaxtablefnCreatePagerInnerHtml">                             
    37.     <div class="grid-allNumberImg grid-pagerText" style="color:#09f;85px;float:right;padding-top: 2px">   
    38.         共有记录<label id="totilerecode">${curpage}</label>条  
    39.     </div>  
    40.   
    41. </div>  

    ---------------------------------freemaker文件*.ftl(page.ftl)     结束------------------------------------------

    ---------------------------------jsp页面     开始------------------------------------------

    [html] view plaincopy
     
    1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
    2. <%@taglib prefix="myf" uri="/muyunfei"%>  
    3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
    4. <html>  
    5.   <head>  
    6.     <title>My JSP 'myftag.jsp' starting page</title>  
    7.   </head>  
    8.     
    9.   <body>  
    10.     自定义控件使用: <br>  
    11.     <myf:page action="/ftlhelloword" curpage="1"></myf:page>  
    12.   </body>  
    13. </html>  

    ---------------------------------jsp页面     结束------------------------------------------

  • 相关阅读:
    tomcat使用redis存储共享session
    dubbo专题」dubbo其实很简单,就是一个远程服务调用的框架(1)
    SpringBoot Test集成测试
    IntelliJ Idea设置护眼浅绿色背景方法
    IDEA使用有道翻译插件
    IDEA Translation插件中有道智云(有道翻译)应用ID,密钥申请教程
    利用grep-console插件使Intellij idea显示多颜色调试日志
    mybatis 整合spring之mapperLocations配置的问题
    idea将maven项目打包成war包的方式,以及使用war包
    maven 常用命令
  • 原文地址:https://www.cnblogs.com/u0mo5/p/4162460.html
Copyright © 2011-2022 走看看