很有用但是不不知道怎么说,写个例子,总之方便多了,并且容易管理,重复利用强
1、自定一个类,实现 javax.servlet.jsp.tagext.Tag;(PageTag.java)
2、建立一个tld文件(myfTag.tld)
3、建立一个freemaker文件*.ftl(page.ftl)
4、建立jsp页面,导入标签()
5、jsp中使用( )
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 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页面 结束------------------------------------------