文件结束[置顶] javaweb框架--自定义标签与freemaker结合
題記:寫這篇博客要主是加深自己對文件結束的認識和總結實現算法時的一些驗經和訓教,如果有錯誤請指出,萬分感謝。
????
很有效但是不不知道怎么說,寫個例子,總之方便多了,并且容易管理,重復利用強
????
????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類開始------------------------------------------
?
public class PageTag implements Tag {public PageContext pagecontex;public JspWriter out;//自定義屬性,當前頁private String curpage;//自定義屬性,跳轉路徑private String action;//設置頁面內容public void setPageContext(PageContext pc) {pagecontex = pc;out = pc.getOut();//再次方法中不能獲得屬性值}//結束@SuppressWarnings("unchecked")public int doEndTag() throws JspException {/*freemarker生成模板...開始*/Configuration cfg = new Configuration();//指定freemarker模板位置cfg.setServletContextForTemplateLoading( pagecontex.getServletContext(), "WEB-INF/templates");try {Map root = new HashMap();root.put("curpage", curpage);root.put("action", action);root.put("path",pagecontex.getServletContext().getContextPath());//得到模板Template templ = cfg.getTemplate("page.ftl");//輸出模板templ.process(root, out);} catch (TemplateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} /*freemarker生成模板...結束*/return 0;}//開始public int doStartTag() throws JspException {return 0;}public Tag getParent() {return null;}//釋放控件public void release() {}public void setParent(Tag t) {}//-----------get setpublic String getCurpage() {return curpage;}public void setCurpage(String curpage) {this.curpage = curpage;}public String getAction() {return action;}public void setAction(String action) {this.action = action;}}????---------------------------------tag類結束------------------------------------------
?
????---------------------------------tld文件開始------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?><taglib xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"version="2.1"><description>JSTL tagTest core library</description><display-name>myTag</display-name><tlib-version>1.1</tlib-version><short-name>myf</short-name><!-- 用來引入時的名字--><uri>/muyunfei</uri><!-- 用來引入時的地址--><tag><description>pageTag<!--描述 --></description><name>page</name><!--標簽的名字--><tag-class>tag.mytag.page.PageTag</tag-class><!-- 對應的java類,要寫全--><body-content>JSP</body-content><attribute><!-- 屬性,可以多個--><name>curpage</name><!-- 自己在java文件中定義的私有變量 --><required>true</required> <!-- 標簽是不是必須該屬性 --><rtexprvalue>false</rtexprvalue> <!-- 是不是支撐表達式 --></attribute><attribute><name>action</name><!-- 自己在java文件中定義的私有變量 --><required>true</required> <!-- 標簽是不是必須該屬性 --><rtexprvalue>true</rtexprvalue> <!-- 是不是支撐表達式 --></attribute></tag></taglib> 每日一道理生活中受傷難免,失敗跌倒并不可怕,可怕的是因此而一蹶不振,失去了對人生的追求與遠大的理想。沒有一個人的前進道路是平平穩穩的,就算是河中穿梭航行的船只也難免顛簸,生活中所遇上的坎坷磨難不是偶爾給予的為難,而是必然所經受的磨練。
????---------------------------------tld文件結束------------------------------------------
?
????---------------------------------freemaker文件*.ftl(page.ftl)???? 開始------------------------------------------
<div class="grid-outPagerImg" οnclick="endpage()" style="float:right;padding-top: 0px"><img alt="最后頁" border="0" src="${path}/images/last.png" style="cursor:hand;" οnmοuseοut="this.src='${path}/images/last.png'" οnmοuseοver="this.src='${path}/images/lasth.png'"></img> </div> <div class="grid-inPagerImg " οnclick="next()" style="float:right;padding-top: 1px"> <img alt="后一頁" border="0" src="${path}/images/next.png" style="cursor:hand;" οnmοuseοut="this.src='${path}/images/next.png'" οnmοuseοver="this.src='${path}/images/nexth.png'"></img> </div> <div class="grid-pagerText" style="float:right;padding-top: 2px">?頁/共<label id="totilepage"></label>頁</div> <input type="text" id="curpage" style="width: 20px;float:right"/> <div class="grid-pagerText" style="float:right;padding-top: 2px">?第?</div> <div class="grid-inPagerImg " οnclick="javascript:alert('${action}?curpage=${curpage}')"" style="float:right;padding-top: 1px"> <img alt="前一頁" border="0" src="${path}/images/prev.png" style="cursor:hand;" οnmοuseοut="this.src='${path}/images/prev.png'" οnmοuseοver="this.src='${path}/images/prevh.png'"></img> </div><div class="grid-outPagerImg" οnclick="javascript:alert('${action}?curpage=${curpage}')" style="float:right;padding-top: 0px"><img alt="第一頁" border="0" src="${path}/images/first.png" style="cursor:hand;" οnmοuseοut="this.src='${path}/images/first.png'" οnmοuseοver="this.src='${path}/images/firsth.png'"></img> </div><div class="grid-fnCreatePagerInnerHtml" id="ajaxtablefnCreatePagerInnerHtml"> <div class="grid-allNumberImg grid-pagerText" style="color:#09f;width:85px;float:right;padding-top: 2px"> 共有記錄<label id="totilerecode">${curpage}</label>條</div></div>????---------------------------------freemaker文件*.ftl(page.ftl)???? 結束------------------------------------------
?
????---------------------------------jsp頁面???? 開始------------------------------------------
?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="myf" uri="/muyunfei"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><title>My JSP 'myftag.jsp' starting page</title></head><body>自定義控件使用: <br><myf:page action="/ftlhelloword" curpage="1"></myf:page></body> </html>????
????---------------------------------jsp頁面???? 結束------------------------------------------
?
文章結束給大家分享下程序員的一些笑話語錄: 面試官:熟悉哪種語言
應聘者:JAVA
面試官:知道什么叫類么
應聘者:我這人實在,工作努力,不知道什么叫累
面試官:知道什么是包?
應聘者:我這人實在 平常不帶包 也不用公司準備了
面試官:知道什么是接口嗎?
應聘者:我這個人工作認真。從來不找借口偷懶
面試官:知道什么是繼承么
應聘者:我是孤兒沒什么可以繼承的
面試官:知道什么叫對象么?
應聘者:知道,不過我工作努力,上進心強,暫時還沒有打算找對象。
面試官:知道多態么?
應聘者:知道,我很保守的。我認為讓心愛的女人為了自已一時的快樂去墮胎是不道德的行為!請問這和C#有什么關系??
總結
以上是生活随笔為你收集整理的文件结束[置顶] javaweb框架--自定义标签与freemaker结合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: balanced binary sear
- 下一篇: IT人士有哪些保健建议