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

页面中调用

 

posted @ 2017-08-01 19:24  jojoworld  阅读(183)  评论(0编辑  收藏  举报