频道栏目
首页 > 资讯 > Jsp > 正文

jsp中自定义Taglib详解介绍

17-11-20        来源:[db:作者]  
收藏   我要投稿
jsp taglib自定义标签自定义jsp标签可以实现Tag接口或者继承TagSupport类,由于TagSupport已经实现了Tag接口,为了方便就直接继承TagSupport了
import javax.servlet.ServletContext;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;

import org.springframework.web.context.WebApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;

public abstract class AbstractTag extends TagSupport {

private static final long serialVersionUID = 3234375809496738889L;

protected WebApplicationContext spring;

@Override

public int doStartTag() throws JspException {

ServletContext servletContext = pageContext.getServletContext();

spring = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

// 根据名字自动装配这个类

spring.getAutowireCapableBeanFactory().autowireBeanProperties(this,

AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);

return super.doStartTag();

}

@Override

public int doEndTag() throws JspException {

doTag();

return super.doEndTag();

}

protected abstract String doTag();

}

这里一般我们只要复写doStartTag()和doEndTag()方法就行了,doStartTag()是在进入标签时会执行的方法,doEndTag()是在结束标签时会执行的方法。

doStartTag()返回值有:

EVAL_BODY_INCLUDE:把Body读入存在的输出流中

SKIP_BODY:忽略对Body的处理

doEndTag()返回值有:

EVAL_PAGE:继续处理页面

SKIP_PAGE:忽略对余下页面的处理

在我的项目里面Bean都是由spring根据name自动注入的。考虑到会定义多个标签,所以在这里还抽象了一层,后面所有要再定义的标签只要都继承AbstractTag这个类,并实现doTag()这个方法就可以了。

列:

public class SecurityTag extends AbstractTag {

private static final long serialVersionUID = -4459796109014389209L;

private DeptService deptService;

private String type;

@Override

protected String doTag() {

System.out.println(type);

deptService.delete(4l);

return null;

}

public void setDeptService(DeptService deptService) {

this.deptService = deptService;

}

public void setType(String type) {

this.type = type;

}

}

在SecurityTag的doTag()中写自己的逻辑就行了,我这里只是做个简单是测试。

到这里,标签类写好了,下面我们还需要加一个xxx.tld的文件放置到src目录下面

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"

version="2.0">


Security Tag Library

1.0

d

/security

    

st    
common.commons.tags.SecurityTag

JSP

       

type       
true       
String

最后我们在页面上引入这个标签

前缀为d<%@ taglib uri="/security" prefix="d" %>

调用

标签自定义函数

上面都弄好了,再自定义函数就比较简单了,贴代码

public class Functions {

private static DeptService deptService;

public static String test(Integer age){

deptService.delete(6l);

return String.valueOf(age);

}

public static void setDeptService(DeptService deptService) {

Functions.deptService = deptService;

}

}

里面要调用的方法都必须是静态的,里面如果需要用到服务接口,那么也必须声明为静态,并加上set方法,然后由spring定义bean去注入

xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

最后在前面的tld文件的tag标签下面加入下面的东西就可以在页面上直接使用了

test

common.commons.tags.Functions

String test(Integer)

使用

${d:test(234) }

相关TAG标签
上一篇:ai格式用什么软件打开?都有那些打开方式以及如何打开的方法!
下一篇:12306余票查询、价格查询 php实践
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站