1. org.stephencat.test.InitializeServlet 片段
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">
<a id="A100" href="a">A100</a><br>
<a id="A101" href="a">A101</a><br>
<a id="A102" href="a">A102</a><br>
<a id="china" href="a">china</a><br>
<a id="oversea" href="a">oversea</a><br>
<a id="coffee" href="a">coffee</a><br>
<a id="other" href="a">other</a><br>
<a id="prev" href="a">prev</a><br>
<a id="next" href="a">next</a><br>
<a 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
4. 输出 HTML 结果
testing complex integrated function
<br>
<body bgcolor="white">
<a id="A100" href="a">A100</a><br>
<a id="A101" href="a">A101</a><br>
<a id="A102" href="a">A102</a><br>
<a id="china" href="a">china</a><br>
<a id="oversea" href="a">oversea</a><br>
<a id="coffee" href="a">coffee</a><br>
<a id="other" href="a">other</a><br>
<a id="prev" href="a">prev</a><br>
<a id="next" href="a">next</a><br>
<a 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 跟踪使用
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);
}
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
![](/Images/dot.gif)
<#-- 自动脚本: 根据 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">
<a id="A100" href="a">A100</a><br>
<a id="A101" href="a">A101</a><br>
<a id="A102" href="a">A102</a><br>
<a id="china" href="a">china</a><br>
<a id="oversea" href="a">oversea</a><br>
<a id="coffee" href="a">coffee</a><br>
<a id="other" href="a">other</a><br>
<a id="prev" href="a">prev</a><br>
<a id="next" href="a">next</a><br>
<a 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![](/Images/dot.gif)
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());
%>
<%@ 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
![](/Images/dot.gif)
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
![](/Images/dot.gif)
<body bgcolor="white">
<a id="A100" href="a">A100</a><br>
<a id="A101" href="a">A101</a><br>
<a id="A102" href="a">A102</a><br>
<a id="china" href="a">china</a><br>
<a id="oversea" href="a">oversea</a><br>
<a id="coffee" href="a">coffee</a><br>
<a id="other" href="a">other</a><br>
<a id="prev" href="a">prev</a><br>
<a id="next" href="a">next</a><br>
<a 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 跟踪使用