博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSP自定义tag
阅读量:4584 次
发布时间:2019-06-09

本文共 3720 字,大约阅读时间需要 12 分钟。

前端需要调用后端的配置,想起velocity-tools。然而jsp的话,目前只能想到tag和EL表达式了。

 

Tag相当好写,jsp2.0提供了简化写法

编写一个java类:

public class HelloWorldTag extends SimpleTagSupport {    public void doTag() throws JspException, IOException{        JspWriter out = getJspContext().getOut();        out.println("Hello Custom Tag!");    }}

然后编写对应tld:

1.0
Example TLD
hello
com.test.demo.HelloWorldTag
empty

然后就可以在页面上使用了:

<%@ taglib prefix="ex" uri="/WEB-INF/hello.tld" %>

 

上述是没有body的tag,如果想要输出body的内容:

新写一个java类:

public class BodyTag extends SimpleTagSupport {    StringWriter sw = new StringWriter();    public void doTag() throws JspException, IOException{        getJspBody().invoke(sw);        JspWriter out = getJspContext().getOut();        out.println(sw.toString());    }}

在原来tld文件里面追加一个tag:

body
com.test.demo.BodyTag
scriptless

在页面上:

This is message body.

 

如果想要在tag上追加参数:

public class StandardTag extends SimpleTagSupport {    private String message;    public void setMessage(String message) {        this.message = message;    }    StringWriter sw = new StringWriter();    public void doTag() throws JspException, IOException{        JspWriter out = getJspContext().getOut();        if (message!=null){            //from filed            out.println(message);        }else{            //from body            getJspBody().invoke(sw);            out.println(sw.toString());        }    }}

在tld中添加一个新tag:

msg
com.test.demo.StandardTag
scriptless
message
false
java.lang.String

在页面上使用:

---------------
if message==null , then show body.

 

 

如果想要使用传参,使用EL表达:

在java类中添加一个static方法:

public static String hello(String name){        return "Welcome: " + name;}

然后在tld中添加:

welcome
com.test.demo.StandardTag
java.lang.String hello(java.lang.String)
${ex:welcome('Ryan')}

然后页面上调用:

${ex:welcome('Leslie')}

 

在spring mvc 中,有个很好用的tag支持类RequestContextAwareTag,下面做一个简单的使用:

public abstract class BaseTag extends RequestContextAwareTag {    private static final EPCLogger LOGGER = EPCLogger.getLogger(BaseTag.class);    private static final long serialVersionUID = -6258930875039222435L;    private BeanUtils beanUtils = new BeanUtils();    private HandlebarUtils handlebarUtils = new HandlebarUtils();    public abstract Object getModel();    public BaseTag() {    }    public void renderHandlebarsView(String templateLocation) {        try {            Template e = this.handlebarUtils.compile(templateLocation);            e.apply(this.getModel(), this.pageContext.getOut());        } catch (Exception var3) {            LOGGER.error(UITagsSystemEvent.BASE_TAG_ERROR, "Error occurred while rendering handlebars view.", var3);        }    }    public Object getBean(String beanName) {        Validate.notNull(beanName);        return this.beanUtils.getBean(this.getRequestContext(), beanName);    }}
public class BeanUtils {    public BeanUtils() {    }    public Object getBean(RequestContext requestContext, String beanName) {        return requestContext.getWebApplicationContext().getBean(beanName);    }}

 

 

reference:

http://www.runoob.com/jsp/jsp-custom-tags.html

https://www.ibm.com/developerworks/cn/java/j-lo-jsp2tag/

 

转载于:https://www.cnblogs.com/woshimrf/p/5779918.html

你可能感兴趣的文章
SQL Server 各版本安装包分享
查看>>
.net项目移植后的虚拟目录的配置问题
查看>>
JSP页面中引入另一个JSP页面
查看>>
Android笔记——活动的生命周期
查看>>
springmvc使用包装的pojo接收商品信息的查询条件
查看>>
【Linux】【Services】【Configuration】puppet
查看>>
poj 1002:487-3279(水题,提高题 / hash)
查看>>
RAC环境上搭建DG
查看>>
OS X Mountain Lion高手进阶
查看>>
精通CSS:高级Web标准解决方案(第2版)(Amazon第一CSS畅销书全新改版)
查看>>
初识电流环
查看>>
MySQL每天自动增加分区
查看>>
在线生成坐标值,方便布局使用
查看>>
ab测试工具的使用
查看>>
RTL基本知识:编译命令指定隐性线网类型
查看>>
java中BigDecimal在金融行业中的使用
查看>>
66.Plus One
查看>>
爬虫——小结
查看>>
sticky bit
查看>>
sqlserver 中 where 条件和 join 条件的作用比较
查看>>