JSP tag file that either outputs its body or returns it in a variable

前端 未结 3 1362
一生所求
一生所求 2021-02-20 00:59

I have a custom tag in a \".tag\" file that computes and outputs a value. Because I cannot post the code here, let\'s assume a simple example.

Content of file mytag.tag:

3条回答
  •  伪装坚强ぢ
    2021-02-20 01:54

    Ran into this same issue and found a way to this without a "hard-coded" variable name that has to match between .jsp and .tag.

    <%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core"%><%@ 
    taglib prefix="s"       uri="http://www.springframework.org/tags" %>
    
    
    
    
    
    
    
        
            ${pageContext.request.setAttribute(var, someOutput)}
        
        
            ${someOutput}
        
    
    

    This tag can be used in two ways:

    <%-- Option 1: renders the output of the tag directly to the page --%>
    
    
    <%-- Option 2: stores the output of the tag in variable called "result" and lets the caller render the output on his own --%>
    
    
    

提交回复
热议问题