首页 技术 正文
技术 2022年11月17日
0 收藏 418 点赞 3,742 浏览 5464 个字

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

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

javaweb框架–自定义标签与freemaker结合

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

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

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

4、建立jsp页面,导入标签(<https://www.shuzhiduo.com/A/kPzO9G7w5x/%25@taglib prefix=”myf” uri=”/muyunfei”%>)

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

6、效果javaweb框架–自定义标签与freemaker结合,以后使用很方便,如果需要修改直接改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. <https://www.shuzhiduo.com/A/kPzO9G7w5x/%25@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页面     结束——————————————

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291