java web freemaker_javaweb框架--自定义标签与freemaker结合

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

45a37725c408c3f989ec7256bd157485.png

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

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

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

4、建立jsp页面,导入标签()

5、jsp中使用( )

6、效果

03fcc55aaecf425a84a0e9ca63a364d8.png,以后使用很方便,如果需要修改直接改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 block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.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 set

public 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文件开始------------------------------------------

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">

JSTL tagTest core library

myTag

1.1

myf

/muyunfei

pageTag

page

tag.mytag.page.PageTag

JSP

curpage

true 

false  

action

true 

true  

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

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

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'">

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'">

 页/共页
 第 

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'">

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'">

共有记录${curpage}条

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

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

HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

My JSP 'myftag.jsp' starting page

自定义控件使用: 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值