火星文 技术研习社

Noname Cat, Keep Thinking
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

FreeMarker 应用:动态决定超链接默认焦点

Posted on 2006-12-08 23:01  剑廿三  阅读(1620)  评论(0编辑  收藏  举报
1. org.stephencat.test.InitializeServlet 片段

import freemarker.template.*;

public class InitializeServlet extends HttpServlet {

    
/**
     * Initialization of the servlet. <br>
     *
     * 
@throws ServletException if an error occure
     
*/
    
public void init(ServletConfig config) throws ServletException {
        
// Put your code here
        String root = config.getServletContext().getRealPath("/");
        
//System.out.println(root);
        
//PropertyConfigurator.configure("E:/log/newslog.properties");
        
        Configuration templateConfig 
= new Configuration();
        File templateDirectory 
= null;
        
        templateDirectory 
= new File(root + "templates");
        System.out.println(
"Setting template dir to: " + templateDirectory.getAbsolutePath());
        
if(templateDirectory.exists() && templateDirectory.isDirectory()){
            
try{
                templateConfig.setDirectoryForTemplateLoading(templateDirectory);
            }
catch(Exception ex){
                System.out.println(ex.getMessage());
            }
        }
else{
            System.out.println(
"template dir not exists!");
            
        }
        templateConfig.setDefaultEncoding(
"utf-8");
        
try{
            templateConfig.setSetting(Configuration.CACHE_STORAGE_KEY, 
"strong:20, soft:250");
        }
catch(Exception ex){
            System.out.println(
"cannot set cache for template engine.");
            System.out.println(ex.getMessage());
        }
        
        config.getServletContext().setAttribute(
"templateConfig", templateConfig);
    }

2. WebRoot/templates/test.html 模板


testing complex integrated function 
<br>

<#-- 自动脚本: 根据 iTV 平台获得超链接 -->
<#function getLink id>
    
<#if platform = "ut">
        
<#-- 
            
需要手工修改: 定义各功能链接的序号
            listSize: 明细表当前页的条目数 
        --
>

        
<#assign focusID 
        {
        
"china":listSize?number+0, 
        "oversea":listSize?number+1, 
        "coffee":listSize?number+2, 
        "other":listSize?number+3,
        "prev":listSize?number+4,
        "next":listSize?number+5,
        "back":listSize?number+6

        }
>
        
<#-- 自动脚本: 取所有栏目链接的 id 名称 -->
        
<#assign focusSet=focusID?keys>
        
        
<#-- 自动脚本: 获得并返回对应的链接序号 -->
        
<#if focusSet?seq_contains(id)>
            
<#return focusID[id]>
        
<#else>
            
<#return id>
        
</#if>
    
<#else>
        
<#-- 自动脚本: 直接返回链接的 id 名称 -->
        
<#return "\""+id+"\"">
    
</#if>
</#function>
<body bgcolor="white">
<id="A100" href="a">A100</a><br>
<id="A101" href="a">A101</a><br>
<id="A102" href="a">A102</a><br>
<id="china" href="a">china</a><br>
<id="oversea" href="a">oversea</a><br>
<id="coffee" href="a">coffee</a><br>
<id="other" href="a">other</a><br>
<id="prev" href="a">prev</a><br>
<id="next" href="a">next</a><br>
<id="back" href="a">back</a><br>
<script>
function setFocus(id)
{
    document.links[id].focus();
    
//document.getElementById(id).focus();
}
</script>
<script>
setFocus(${getLink(currentFocus)});
</script>
</body>

3. 实测 index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="freemarker.template.*" %>
<%@ page import="java.io.*" %>
<%

Configuration cfg 
= (Configuration)this.getServletContext().getAttribute("templateConfig");
Template temp 
= cfg.getTemplate("test.html");

/* Create a data model */
Map root 
= new HashMap();


// test hash 

int listSize = 3;

root.put(
"listSize", String.valueOf(listSize));

String currentFocus 
= "china";

root.put(
"platform""ut");
root.put(
"currentFocus", currentFocus);

StringWriter writer 
= new StringWriter();
temp.process(root, writer);

out.println(writer.toString());
        

%>


4. 输出 HTML 结果





testing complex integrated function 
<br>

<body bgcolor="white">
<id="A100" href="a">A100</a><br>
<id="A101" href="a">A101</a><br>
<id="A102" href="a">A102</a><br>
<id="china" href="a">china</a><br>
<id="oversea" href="a">oversea</a><br>
<id="coffee" href="a">coffee</a><br>
<id="other" href="a">other</a><br>
<id="prev" href="a">prev</a><br>
<id="next" href="a">next</a><br>
<id="back" href="a">back</a><br>
<script>
function setFocus(id)
{
    document.links[id].focus();
    
//document.getElementById(id).focus();
}
</script>
<script>
setFocus(3
);
</script>
</body>

小结
1)模板稍显臃肿,可考虑进一步封装
2)使用 MRU Cache 后性能确有改善
3)可结合 log4j 跟踪使用