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类开始------------------------------------------

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

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

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

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

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

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

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

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

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

  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. <body>
  9. 自定义控件使用: <br>
  10. <myf:page action="/ftlhelloword" curpage="1"></myf:page>
  11. </body>
  12. </html>

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

最新文章

  1. 《学技术练英语》PPT分享
  2. C#基础-关于用json给控制台程序传值的坑
  3. 转载:《TypeScript 中文入门教程》 2、枚举
  4. 【原】web移动端常用知识点笔记
  5. (转)Linux下安装Matlab2014及破解
  6. hadoop2.0 和1.0的区别
  7. js unix时间戳转换
  8. JS中关于 一个关于计时器功能效果的实现
  9. Pull解析-解析xml文件
  10. GridView 自定义表头
  11. Toy Storage
  12. hdoj 2057
  13. poj 3020Antenna Placement
  14. bzoj1643 [Usaco2007 Oct]Bessie&#39;s Secret Pasture 贝茜的秘密草坪
  15. Flask web开发 简单介绍
  16. HttpClient文件上传下载
  17. 分布式文件系统之MogileFS工作原理及实现过程
  18. 一个经典的PHP验证码类分享
  19. eclipse工具按键翻译
  20. stack源码

热门文章

  1. layer.open({}) 子页面传参并调用父页面的方法
  2. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 网格系统实例:手机、平板电脑、台式电脑
  3. 【JS 移动端】获取设置页面大小
  4. LPWAN
  5. EBCDIK,EBCDIC,ASCII,shift JIS間の変換
  6. Mysql 连接时的 option 含义
  7. redhat7.6 httpd配置php模块
  8. Web--Utils
  9. nginx 的磁盘IO优化
  10. windows 以太坊开发框架Truffle环境搭建