taglib block

Posted 马杏争1994

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了taglib block相关的知识,希望对你有一定的参考价值。

新建文件

package com.augmentum.oes.taglib;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.context.ApplicationContext;

import com.augmentum.oes.common.BlockAbstract;
import com.augmentum.oes.util.SpringUtil;

public class BlockTaglib extends TagSupport{

    private static final long serialVersionUID = -1485358775043610324L;
    private String name;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int doStartTag() throws JspException {
        return SKIP_BODY;
    }
    @Override
    public int doEndTag() throws JspException {
        ApplicationContext ctx = SpringUtil.getApplicationContext();
        BlockAbstract block = (BlockAbstract) ctx.getBean(name);

        String content = block.dispalyBlock(pageContext);

        JspWriter out = pageContext.getOut();

        try {
            out.println(content);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return EVAL_PAGE;  
    }

    @Override
    public void release() {
        super.release();
    }
}

  新建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>oes 1.0 </description> 
    <display-name>mxz</display-name>  
    <tlib-version>1.0</tlib-version>  
    <short-name>block</short-name> 
    <uri>http://www.oes.com/tag/mxz</uri> 
    <tag>             
        <name>display</name>
        <tag-class>com.augmentum.oes.taglib.tagl</tag-class>
        <body-content>empty</body-content> 
        <attribute>
            <name>name</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag> 
    
 </taglib>  

在页面中引入

<%@taglib prefix="block" uri="http://www.oes.com/tag/mxz" %>

 

 

对于block页面分块

公共类

技术分享
package com.augmentum.oes.common;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;

public abstract class BlockAbstract {

    public String template;

    public String getTemplate() {
        return template;
    }

    public void setTemplate(String template) {
        this.template = template;
    }

    public String dispalyBlock(PageContext pagecontext) {
        execute(pagecontext);
        HttpServletRequest request = (HttpServletRequest) pagecontext.getRequest();
        Writer body = new StringWriter();
        try {
            if (template != null && !template.trim().equals("")) {
                pagecontext.pushBody(body);
                pagecontext.include(template);
                pagecontext.popBody();
                return body.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                body.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    abstract protected void execute(PageContext pagecontext) ;


}
BlockAbstract

要实现的传数据类

技术分享
package com.augmentum.oes.block;

import javax.servlet.jsp.PageContext;

import com.augmentum.oes.common.BlockAbstract;

public class QuestionInfoBlock extends BlockAbstract{

    @Override
    protected void execute(PageContext pagecontext) {
        //get data;
    }


}
QuestionInfoBlock

通过spring获得

技术分享
    <bean id="questionInfoBlock" class="com.augmentum.oes.block.QuestionInfoBlock">
        <property name="template" value="/WEB-INF/jsp/questions/question_list.jsp"></property>
    //这里可以注入其他需要的service
    </bean>
applicationcontext.xml

页面中调用

 

以上是关于taglib block的主要内容,如果未能解决你的问题,请参考以下文章

iOS学习之代码块(Block)

ios block和delegate的区别

Sphinx - 在代码块片段中使用省略号 (...)

Objective-C Block与函数指针比较

Taglib 锐利不编辑评级

使用 TagLib 从 MP3 文件中提取专辑封面 - 有没有更好的方法编写此代码?