Servlet + Freemarker 入门练习_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Servlet + Freemarker 入门练习

Servlet + Freemarker 入门练习

 2013/12/18 19:09:12  AILIKES  程序员俱乐部  我要评论(0)
  • 摘要:建立web项目,拷贝freemarker的jar文件到类路径,其他步骤略。首先配置web.xml,添加freemarker解析的Servlet配置,内容如下Xml代码<servlet><servlet-name>freemarker</servlet-name><servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class><init-param>
  • 标签:Servlet

建立web项目,拷贝freemarker的jar文件到类路径,其他步骤略。

首先配置web.xml,添加freemarker解析Servlet配置,内容如下

?

Xml代码??class="star" alt="收藏代码" src="/Upload/Images/2013121819/40B102E0EF997EA6.png">
    monospace;">
  1. <servlet>??
  2. ????<servlet-name>freemarker</servlet-name>??
  3. ????<servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>??
  4. ??<init-param>????
  5. ??????<param-name>TemplatePath</param-name>????
  6. ??????<param-value>/</param-value>????
  7. ??</init-param>???
  8. ??<init-param>????
  9. ??????<param-name>NoCache</param-name>????
  10. ??????<param-value>true</param-value>????
  11. ??</init-param>????
  12. ????<init-param>??
  13. ????????<param-name>ContentType</param-name>??
  14. ????????<param-value>text/html;charset=UTF-8</param-value>??
  15. ????</init-param>??
  16. ????<init-param>??
  17. ????????<param-name>template_update_delay</param-name>??
  18. ????????<param-value>0</param-value>??
  19. ????</init-param>??
  20. ????<init-param>??
  21. ????????<param-name>default_encoding</param-name>??
  22. ????????<param-value>UTF-8</param-value>??
  23. ????</init-param>??
  24. ??<init-param>????
  25. ??????<param-name>number_format</param-name>????
  26. ??????<param-value>0.##########</param-value>????
  27. ??</init-param>????
  28. </servlet>??
  29. <servlet-mapping>??
  30. ????<servlet-name>freemarker</servlet-name>??
  31. ????<url-pattern>*.ftl</url-pattern>??
  32. </servlet-mapping>??

?

?

?

添加一个测试用的Servlet,代码如下:

Java代码??收藏代码
  1. package?test.servlet;??
  2. ??
  3. import?java.io.IOException;??
  4. import?java.util.ArrayList;??
  5. import?java.util.Date;??
  6. import?java.util.HashMap;??
  7. import?java.util.HashSet;??
  8. import?java.util.List;??
  9. import?java.util.Map;??
  10. import?java.util.Set;??
  11. ??
  12. import?javax.servlet.ServletException;??
  13. import?javax.servlet.http.HttpServlet;??
  14. import?javax.servlet.http.HttpServletRequest;??
  15. import?javax.servlet.http.HttpServletResponse;??
  16. ??
  17. public?class?TestFreemarker?extends?HttpServlet?{??
  18. ??
  19. ????private?static?final?long?serialVersionUID?=?1L;??
  20. ??
  21. ????@Override??
  22. ????protected?void?service(HttpServletRequest?req,?HttpServletResponse?resp)??
  23. ????????????throws?ServletException,?IOException?{??
  24. ????????String?strValue?=?"this?is?String";??
  25. ????????Date?myDate??=?new?Date();??
  26. ????????Object[]?objectArray?=?new?Object[]{1,"str",1.2};??
  27. ??????????
  28. ????????Set<Object>?setData?=?new?HashSet<Object>();??
  29. ????????setData.add("dataValue1");??
  30. ????????setData.add("dataValue2");??
  31. ????????setData.add("dataValue3");??
  32. ??????????
  33. ????????List<String>?strList?=?new?ArrayList<String>();??
  34. ????????strList.add("字符串1");??
  35. ????????strList.add("字符串2");??
  36. ????????strList.add("字符串3");??
  37. ??????????
  38. ????????Map<String,Object>?map?=?new?HashMap<String,?Object>();??
  39. ????????map.put("key1",?"value1");??
  40. ????????map.put("key2",?"value2");??
  41. ????????map.put("key3",?"value3");??
  42. ??????????
  43. ????????req.setAttribute("strValue",?strValue);??
  44. ????????req.setAttribute("objectArray",?objectArray);??
  45. ????????req.setAttribute("strList",?strList);??
  46. ????????req.setAttribute("map",?map);??
  47. ????????req.setAttribute("myDate",?myDate);??
  48. ????????req.setAttribute("setData",?setData);??
  49. ????????req.getRequestDispatcher("/WEB-INF/view/freemarker/test.ftl").forward(req,?resp);??
  50. ????}??
  51. }??

?

配置此Servlet到web.xml中,增加如下内容到web.xml文件中:

Xml代码??收藏代码
  1. <servlet>??
  2. ????<servlet-name>test</servlet-name>??
  3. ????<servlet-class>test.servlet.TestFreemarker</servlet-class>??
  4. </servlet>??
  5. <servlet-mapping>??
  6. ????<servlet-name>test</servlet-name>??
  7. ????<url-pattern>/test</url-pattern>??
  8. </servlet-mapping>??

?

在/WEB-INF/view/freemarker目录下建立文件test.ftl,内容如下:

Html代码??收藏代码
  1. <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">??
  2. <html?xmlns="http://www.w3.org/1999/xhtml"?xml:lang="zh-CN"?dir="ltr">??
  3. <head>??
  4. ?<title>test!</title>??
  5. ?<meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8"/>??
  6. </head>??
  7. <body>??
  8. ????<div>??
  9. ????????直接显示字符串<br?/>??
  10. ????????String:${strValue}<br/>?????
  11. ????????<hr/>??
  12. ????</div>??
  13. ??????
  14. ????<div>??
  15. ????显示日期??
  16. ????date:${myDate?date}<br/>??
  17. ????<hr/>??
  18. ????显示时间??
  19. ????time:${myDate?time}<br/>??
  20. ????<hr/>??
  21. ????显示时间戳??
  22. ????datetime:${myDate?datetime}<br/>??
  23. ????<hr/>???
  24. ????</div>??
  25. ??????
  26. ????<div>??
  27. ????访问对象数组??
  28. ????ObjectArray:??
  29. ????${objectArray[0]},${objectArray[1]},${objectArray[2]}??
  30. ????<br?/>??
  31. ????<hr/>???????
  32. ????</div>??
  33. ??????
  34. ????<div>??
  35. ????遍历Set中的元素??
  36. ????SetData:<br?/>??
  37. ????<#list?setData?as?value>??
  38. ????????${value}??
  39. ????</#list>??
  40. ????<hr/>???
  41. ????</div>??
  42. ??????
  43. ????<div>??
  44. ????遍历List中的元素??
  45. ????strList:<br/>??
  46. ????<#list?strList?as?strValue?>??
  47. ????<#if?strValue_index?==?0>??
  48. ????????${strValue}?is?first?element??
  49. ????<#elseif?!strValue_has_next>??
  50. ????????${strValue}?is?last?element??
  51. ????<#else>??
  52. ????????${strValue}??
  53. ????</#if>??
  54. ????<br?/>??
  55. ????</#list>??
  56. ????<hr/>???
  57. ????</div>??
  58. ??????
  59. ????<div>??
  60. ????获取map中的值??
  61. ????<br/>??
  62. ????map:??
  63. ????key1=${map.key1},key2=${map.key2},key3=${map.key3}??
  64. ????<hr/>???????
  65. ????</div>??
  66. ??????
  67. ??????
  68. ????<#include?"foot.html">??
  69. </body>?????
  70. </html>????

?

模板中包含的foot.html,在和test.ftl相同的目录下,文件内容如下

Html代码??收藏代码
  1. <div?style="margin-top:?50px;text-align:?center;font-size:?12px">??
  2. <hr?/>??
  3. this?is?foot??
  4. </div>??

?

运行此项目访问/test路径,输出内容如下:

Html代码??收藏代码
  1. <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">??
  2. <html?xmlns="http://www.w3.org/1999/xhtml"?xml:lang="zh-CN"?dir="ltr">??
  3. <head>??
  4. ?<title>test!</title>??
  5. ?<meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8"/>??
  6. </head>??
  7. <body>??
  8. ????<div>??
  9. ????????直接显示字符串<br?/>??
  10. ????????String:this?is?String<br/>??????
  11. ????????<hr/>??
  12. ??
  13. ????</div>??
  14. ??????
  15. ????<div>??
  16. ????显示日期??
  17. ????date:2011-3-18<br/>??
  18. ????<hr/>??
  19. ????显示时间??
  20. ????time:17:21:44<br/>??
  21. ????<hr/>??
  22. ????显示时间戳??
  23. ????datetime:2011-3-18?17:21:44<br/>??
  24. ??
  25. ????<hr/>???
  26. ????</div>??
  27. ??????
  28. ????<div>??
  29. ????访问对象数组??
  30. ????ObjectArray:??
  31. ????1,str,1.2??
  32. ????<br?/>??
  33. ????<hr/>???????
  34. ????</div>??
  35. ??????
  36. ????<div>??
  37. ????遍历Set中的元素??
  38. ????SetData:<br?/>??
  39. ????????dataValue2??
  40. ????????dataValue1??
  41. ????????dataValue3??
  42. ????<hr/>???
  43. ????</div>??
  44. ??
  45. ??????
  46. ????<div>??
  47. ????遍历List中的元素??
  48. ????strList:<br/>??
  49. ????????字符串1?is?first?element??
  50. ????<br?/>??
  51. ????????字符串2??
  52. ????<br?/>??
  53. ????????字符串3?is?last?element??
  54. ????<br?/>??
  55. ????<hr/>???
  56. ????</div>??
  57. ??
  58. ??????
  59. ????<div>??
  60. ????获取map中的值??
  61. ????<br/>??
  62. ????map:??
  63. ????key1=value1,key2=value2,key3=value3??
  64. ????<hr/>???????
  65. ????</div>??
  66. ??????
  67. ??????
  68. <div?style="margin-top:?50px;text-align:?center;font-size:?12px">??
  69. <hr?/>??
  70. this?is?foot??
  71. </div></body>?????
  72. </html>??????????

?

?

freemarker的list指令,不能遍历如Object[]类型的对象数组。但可以遍历List和Set

发表评论
用户名: 匿名