Chinaunix首页 | 论坛 | 博客
  • 博客访问: 522402
  • 博文数量: 151
  • 博客积分: 7010
  • 博客等级: 少将
  • 技术积分: 1405
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-22 14:32
文章分类

全部博文(151)

文章存档

2011年(1)

2010年(23)

2009年(1)

2008年(126)

我的朋友

分类: 系统运维

2008-09-03 17:40:54

Let's show the JSP file firt:

   <%@ page contentType="text/html" %>
   <%@ taglib prefix="c" uri="" %>
   <%@ taglib prefix="my" tagdir="/WEB-INF/tags/mytags" %>
  
   <%-- Create test data --%>
  
     This is just a lot of text that the browser will format to
     fit the browser window. Attempts to add HTML elements
     are dealt with by conversion to character entities.
     [code]
     This part I want the browser to leave alone, so that
     all my indentations are left intact:
  
       public class Foo {
         public String getBar() {
           return bar;
         }
       }
     [/code]
     And then some regular text again.
  

  
    
       Online Forum
    
    
      

Online Forum


       Here's a formatted message:
      


        
           ${message}
        

      


    
  
  

The tag file(htmlFormat.tag):

   <%@ tag body-content="scriptless" %>
   <%@ taglib prefix="fn" uri="" %>
   <%@ taglib prefix="c" uri="" %>
  
   <%-- Capture the body evaluation result in a variable --%>
  
  
   <%-- Convert special characters to character entities --%>
  
  
   <%-- Replace "[code]/[/code]" with "
/
" --%>
        value="${fn:replace(escapedBody, '[code]', '
')}" />
        value="${fn:replace(convBody, '[/code]', '
')}" />
  
   <%-- Output the result --%>
   ${convBody}


The result:


The JSP file first saves test data in a varialbe named message.In a real application,the text would likely come from a database or some other external source.It then processes the text with the costom action.

Note that the tag directive in the tag file sets the body-content attribute to scriptless.This means that the pape author is allowed to put template text,standard action and custom actions ,in the body but not scripting elements.

In the tag file,there's a standard action which can only used in tag files.It evaluates the body of the custom action element,meaning that all action elements in the body are called and output they produce is mixed with the template text.The result is saved in a variable,using the var attribute to name the variable.

In the example showed above,the {message} is set to the variable  'bodyRes' of the standard action .The rest code of the tag file is to deal with the content of the 'bodyRes'. 
阅读(546) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~