Simpleframework组件与FreeMarker 整合例子html charset gbk /head? title你的自定义模板/title
Simpleframework组件与FreeMarker 整合例子
html ; charset = gbk " />
<head>
? <title>你的自定义模板</title>
</head>
?<body>
????? <h1> freemarker 输出的内容<h1>?
????? ${message}
????? :<font color=red>${ username }</font>
?????? <h1> sSimple 输出的内容<h1><br>?
?? <table>
??? <tr>
?????? <td width="50%" valign ="top">
?????? <div> 可以拖动的普通树 </div>
?????? <div id="demoTree1" style="padding: 8px; border: 5px solid # ddd ;"> </div>
?????? </td>
?????? <td width="50%" valign ="top">
?????? <div> 动态装载的树,可以多选 </div>
?????? <div id="demoTree2" style="padding: 8px; border: 5px solid # ddd ;"> </div>
?????? </td>
??? </tr>
? </table>???
?</body>
</html>
5.建一个HelloFreeMarkerServlet:
packagedemo;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.util.HashMap;
importjava.util.Map;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
?
importfreemarker.template.Configuration;
importfreemarker.template.Template;
importfreemarker.template.TemplateException;
?
@SuppressWarnings("serial")
publicclassHelloFreeMarkerServlet extendsHttpServlet
{
??? privateConfiguration cfg= null;
?
??? @Override
??? publicvoidinit() throwsServletException
??? {
??????? // FreeMarker程序入口点
??????? cfg= newConfiguration();
??????? // 初始化 Servlet 上下文(第二参数为ServletContext的相对路径,null代表当前网站根路径,相对路径)
??????? cfg.setServletContextForTemplateLoading(this.getServletContext(), null);??????
??? }
??? @SuppressWarnings("unchecked")
??? @Override
??? protectedvoiddoPost(HttpServletRequest req, HttpServletResponse resp)
??????????? throwsServletException, IOException
??? {
??????? // 模板文件会根据key,读取value
??????? Map root = new HashMap ();
??????? root.put("message", "hello!");
??????? root.put("username", "SSimpleframe and freemarker lover.");
?? ?????Template t = cfg.getTemplate("/templates/test.ftl");
??????? resp.setCharacterEncoding("utf-8");
??????? PrintWriter out = resp.getWriter();
??????? try
??????? {
??????????? t.process(root, out);
??????? } catch(TemplateException e)
??????? {
??????????? e.printStackTrace();
??????? }
??? }
?
??? @Override
??? protectedvoiddoGet(HttpServletRequest req, HttpServletResponse resp)
??????????? throwsServletException, IOException
??? {
??????? this.doPost(req, resp);
??? }
?
}
?
6.web.xml中的相应配置:
?
<?xmlversion="1.0" encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
??? xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
??? xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
??? id="WebApp_ID" version="2.5">
??? <display-name> Simpleframework </display-name>
??? <welcome-file-list>
?????? <welcome-file>index.html</welcome-file>
?????? <welcome-file>index.htm</welcome-file>
?????? <welcome-file>index.jsp</welcome-file>
?????? <welcome-file>default.html</welcome-file>
?????? <welcome-file>default.htm</welcome-file>
?????? <welcome-file>default.jsp</welcome-file>
??? </welcome-file-list>
?
??? <servlet>
??????? <servlet-name>testServlet</servlet-name>
??????? <servlet-class>demo.HelloFreeMarkerServlet</servlet-class>
??? </servlet>
?
??? <servlet-mapping>
??????? <servlet-name>testServlet</servlet-name>
??????? <url-pattern>/hello.do</url-pattern>
??? </servlet-mapping>
?
??? <filter>
?????? <filter-name>actionFilter</filter-name>
??? ??? <filter-class>net.Simpleframework.web.page.PageFilter</filter-class>
??? </filter>
??? <filter-mapping>
?????? <filter-name>actionFilter</filter-name>
?????? <url-pattern>*. jsp </url-pattern>
??? </filter-mapping>??
??? <filter-mapping>
?????? <filter-name>actionFilter</filter-name>
?????? <url-pattern>*.do</url-pattern>
??? </filter-mapping>
??? <listener>
??? <listener-class>net.Simpleframework.web.page.PageEventAdapter</listener-class>
??? </listener>
</web-app>
?
7. 申明组件元数据定义,需要对应于拦截的hello, hello.xml 文件如下:
?
<?xmlversion="1.0" encoding="UTF-8"?>
<pagexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? xsi:noNamespaceSchemaLocation="/xsd/default/sSimple.xsd">
??? <responseCharacterEncoding> gbk </responseCharacterEncoding>
? <components>
??? <treename="demoTree1" containerId="demoTree1" handleClass="net.Simpleframework.example.MyTree"
????? width="240">
????? <treenodetext="Root" acceptdrop="true">
??????? <treenodetext="Node1" draggable="true" acceptdrop="true">
????????? <jsDblclickCallback>alert('Node1');</jsDblclickCallback>
??????? </treenode>
??????? <treenodetext="Node2" draggable="true" acceptdrop="true">
????????? <treenodetext="Node2_1" draggable="true" acceptdrop="true">
??????????? <jsDblclickCallback>alert('Node2_1');</jsDblclickCallback>
????????? </treenode>
????????? <treenodetext="Node2_2" draggable="true" acceptdrop="true">
????????? </treenode>
????????? <treenodetext="Node2_3" draggable="true" acceptdrop="true">
????????? </treenode>
??????? </treenode>
??????? <treenodetext="Node3" draggable="true" acceptdrop="true">
??????? </treenode>
??????? <treenodetext="Node4" draggable="true" acceptdrop="true">
??????? </treenode>
????? </treenode>
??? </tree>
?
??? <treename="demoTree2" containerId="demoTree2" checkboxes="true"
????? checkboxesThreeState="true" dynamicLoading="true" width="240">
????? <treenodetext="北京市"/>
????? <treenodetext="上海市"/>
?????? ***
??? </tree>
? </components>
</page>
?
8.启动浏览器访问: http://localhost:8080/demo/hello.do
您会看到:
?
完整的工程 点击下载
?
<p align="left"><head></p>
<p align="left">? <title>你的自定义模板</title></p>
<p align="left"></head></p>
<p align="left">?<body></p>
<p align="left">????? <h1> <span style="text-decoration: underline;">freemarker </span>输出的内容<h1>?</p>
<p align="left">????? ${message}</p>
<p align="left">????? :<font color=red>${ <span style="text-decoration: underline;">username </span>}</font></p>
<p align="left">?????? <h1> sSimple 输出的内容<h1><br>?</p>
<p align="left">?? <table></p>
<p align="left">??? <tr></p>
<p align="left">?????? <td width="50%" <span style="text-decoration: underline;">valign </span>="top"></p>
<p align="left">?????? <span style="text-decoration: underline;"><div> </span>可以拖动的普通树 <span style="text-decoration: underline;"></div> </span></p>
<p align="left">?????? <div id="demoTree1" style="padding: 8px; border: 5px solid # <span style="text-decoration: underline;">ddd </span>;"> <span style="text-decoration: underline;"></div> </span></p>
<p align="left">?????? </td></p>
<p align="left">?????? <td width="50%" <span style="text-decoration: underline;">valign </span>="top"></p>
<p align="left">?????? <span style="text-decoration: underline;"><div> </span>动态装载的树,可以多选 <span style="text-decoration: underline;"></div> </span></p>
<p align="left">?????? <div id="demoTree2" style="padding: 8px; border: 5px solid # <span style="text-decoration: underline;">ddd </span>;"> <span style="text-decoration: underline;"></div> </span></p>
<p align="left">?????? </td></p>
<p align="left">??? </tr></p>
<p align="left">? </table>???</p>
<p align="left">?</body></p>
<p align="left"><span style="text-decoration: underline;"></html> </span></p>
<p align="left">5.建一个HelloFreeMarkerServlet:</p>
<p align="left"><strong>package</strong>demo;</p>
<p align="left"><strong>import</strong>java.io.IOException;</p>
<p align="left"><strong>import</strong>java.io.PrintWriter;</p>
<p align="left"><strong>import</strong>java.util.HashMap;</p>
<p align="left"><strong>import</strong>java.util.Map;</p>
<p align="left"><strong>import</strong>javax.servlet.ServletException;</p>
<p align="left"><strong>import</strong>javax.servlet.http.HttpServlet;</p>
<p align="left"><strong>import</strong>javax.servlet.http.HttpServletRequest;</p>
<p align="left"><strong>import</strong>javax.servlet.http.HttpServletResponse;</p>
<p align="left">?</p>
<p align="left"><strong>import</strong>freemarker.template.Configuration;</p>
<p align="left"><strong>import</strong>freemarker.template.Template;</p>
<p align="left"><strong>import</strong>freemarker.template.TemplateException;</p>
<p align="left">?</p>
<p align="left">@SuppressWarnings("serial")</p>
<p align="left"><strong>public</strong><strong>class</strong>HelloFreeMarkerServlet <strong>extends</strong>HttpServlet</p>
<p align="left">{</p>
<p align="left">??? <strong>private</strong>Configuration cfg= <strong>null</strong>;</p>
<p align="left">?</p>
<p align="left">??? @Override</p>
<p align="left">??? <strong>public</strong><strong>void</strong>init() <strong>throws</strong>ServletException</p>
<p align="left">??? {</p>
<p align="left">??????? // FreeMarker程序入口点</p>
<p align="left">??????? cfg= <strong>new</strong>Configuration();</p>
<p align="left">??????? // 初始化 <span style="text-decoration: underline;">Servlet </span>上下文(第二参数为ServletContext的相对路径,null代表当前网站根路径,相对路径)</p>
<p align="left">??????? cfg.setServletContextForTemplateLoading(<strong>this</strong>.getServletContext(), <strong>null</strong>);??????</p>
<p align="left">??? }</p>
<p align="left">??? @SuppressWarnings("unchecked")</p>
<p align="left">??? @Override</p>
<p align="left">??? <strong>protected</strong><strong>void</strong>doPost(HttpServletRequest req, HttpServletResponse resp)</p>
<p align="left">??????????? <strong>throws</strong>ServletException, IOException</p>
<p align="left">??? {</p>
<p align="left">??????? // 模板文件会根据key,读取value</p>
<p align="left">??????? <span style="text-decoration: underline;">Map </span>root = <strong>new</strong> <span style="text-decoration: underline;">HashMap </span>();</p>
<p align="left">??????? root.put("message", "hello!");</p>
<p align="left">??????? root.put("username", "SSimpleframe and freemarker lover.");</p>
<p align="left">?? ?????Template t = cfg.getTemplate("/templates/test.ftl");</p>
<p align="left">??????? resp.setCharacterEncoding("utf-8");</p>
<p align="left">??????? PrintWriter out = resp.getWriter();</p>
<p align="left">??????? <strong>try</strong></p>
<p align="left">??????? {</p>
<p align="left">??????????? t.process(root, out);</p>
<p align="left">??????? } <strong>catch</strong>(TemplateException e)</p>
<p align="left">??????? {</p>
<p align="left">??????????? e.printStackTrace();</p>
<p align="left">??????? }</p>
<p align="left">??? }</p>
<p align="left">?</p>
<p align="left">??? @Override</p>
<p align="left">??? <strong>protected</strong><strong>void</strong>doGet(HttpServletRequest req, HttpServletResponse resp)</p>
<p align="left">??????????? <strong>throws</strong>ServletException, IOException</p>
<p align="left">??? {</p>
<p align="left">??????? <strong>this</strong>.doPost(req, resp);</p>
<p align="left">??? }</p>
<p align="left">?</p>
<p align="left">}</p>
<p align="left">?</p>
<p align="left">6.web.xml中的相应配置:</p>
<p align="left">?</p>
<p align="left"><?xmlversion=<em>"1.0"</em> encoding=<em>"UTF-8"</em>?></p>
<p align="left"><web-appxmlns:xsi=<em><span>"<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>"</span></em> xmlns=<em><span>"<a href="http://java.sun.com/xml/ns/javaee">http://java.sun.com/xml/ns/javaee</a>"</span></em></p>
<p align="left">??? xmlns:web=<em><span>"<a href="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd</a>"</span></em></p>
<p align="left">??? xsi:schemaLocation=<em><span>"<a href="http://java.sun.com/xml/ns/javaee">http://java.sun.com/xml/ns/javaee</a> <a href="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd</a>"</span></em></p>
<p align="left">??? id=<em>"WebApp_ID"</em> version=<em>"2.5"</em>></p>
<p align="left">??? <display-name> <span style="text-decoration: underline;">Simpleframework </span></display-name></p>
<p align="left">??? <welcome-file-list></p>
<p align="left">?????? <welcome-file>index.html</welcome-file></p>
<p align="left">?????? <welcome-file>index.htm</welcome-file></p>
<p align="left">?????? <welcome-file>index.jsp</welcome-file></p>
<p align="left">?????? <welcome-file>default.html</welcome-file></p>
<p align="left">?????? <welcome-file>default.htm</welcome-file></p>
<p align="left">?????? <welcome-file>default.jsp</welcome-file></p>
<p align="left">??? </welcome-file-list></p>
<p align="left">?</p>
<p align="left">??? <servlet></p>
<p align="left">??????? <servlet-name>testServlet</servlet-name></p>
<p align="left">??????? <servlet-class>demo.HelloFreeMarkerServlet</servlet-class></p>
<p align="left">??? </servlet></p>
<p align="left">?</p>
<p align="left">??? <servlet-mapping></p>
<p align="left">??????? <servlet-name>testServlet</servlet-name></p>
<p align="left">??????? <url-pattern>/hello.do</url-pattern></p>
<p align="left">??? </servlet-mapping></p>
<p align="left">?</p>
<p align="left">??? <filter></p>
<p align="left">?????? <filter-name>actionFilter</filter-name></p>
<p align="left">??? ??? <filter-class>net.Simpleframework.web.page.PageFilter</filter-class></p>
<p align="left">??? </filter></p>
<p align="left">??? <filter-mapping></p>
<p align="left">?????? <filter-name>actionFilter</filter-name></p>
<p align="left">?????? <url-pattern>*. <span style="text-decoration: underline;">jsp </span></url-pattern></p>
<p align="left">??? </filter-mapping>??</p>
<p align="left">??? <filter-mapping></p>
<p align="left">?????? <filter-name>actionFilter</filter-name></p>
<p align="left">?????? <url-pattern>*.do</url-pattern></p>
<p align="left">??? </filter-mapping></p>
<p align="left">??? <listener></p>
<p align="left">??? <listener-class>net.Simpleframework.web.page.PageEventAdapter</listener-class></p>
<p align="left">??? </listener></p>
<p align="left"></web-app></p>
<p align="left">?</p>
<p align="left">7. 申明组件元数据定义,需要对应于拦截的hello, hello.xml 文件如下:</p>
<p align="left">?</p>
<p align="left"><?xmlversion=<em>"1.0"</em> encoding=<em>"UTF-8"</em>?></p>
<p align="left"><pagexmlns:xsi=<em><span>"<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>"</span></em></p>
<p align="left">? xsi:noNamespaceSchemaLocation=<em>"/xsd/default/sSimple.xsd"</em>></p>
<p align="left">??? <responseCharacterEncoding> <span style="text-decoration: underline;">gbk </span></responseCharacterEncoding></p>
<p align="left">? <components></p>
<p style="margin-left: 55pt;" align="left">??? <treename=<em>"demoTree1"</em> containerId=<em>"demoTree1"</em> handleClass=<em>"net.Simpleframework.example.MyTree"</em></p>
<p align="left">????? width=<em>"240"</em>></p>
<p align="left">????? <treenodetext=<em>"Root"</em> acceptdrop=<em>"true"</em>></p>
<p align="left">??????? <treenodetext=<em>"Node1"</em> draggable=<em>"true"</em> acceptdrop=<em>"true"</em>></p>
<p align="left">????????? <jsDblclickCallback>alert('Node1');</jsDblclickCallback></p>
<p align="left">??????? </treenode></p>
<p align="left">??????? <treenodetext=<em>"Node2"</em> draggable=<em>"true"</em> acceptdrop=<em>"true"</em>></p>
<p align="left">????????? <treenodetext=<em>"Node2_1"</em> draggable=<em>"true"</em> acceptdrop=<em>"true"</em>></p>
<p align="left">??????????? <jsDblclickCallback>alert('Node2_1');</jsDblclickCallback></p>
<p align="left">????????? </treenode></p>
<p align="left">????????? <treenodetext=<em>"Node2_2"</em> draggable=<em>"true"</em> acceptdrop=<em>"true"</em>></p>
<p align="left">????????? </treenode></p>
<p align="left">????????? <treenodetext=<em>"Node2_3"</em> draggable=<em>"true"</em> acceptdrop=<em>"true"</em>></p>
<p align="left">????????? </treenode></p>
<p align="left">??????? </treenode></p>
<p align="left">??????? <treenodetext=<em>"Node3"</em> draggable=<em>"true"</em> acceptdrop=<em>"true"</em>></p>
<p align="left">??????? </treenode></p>
<p align="left">??????? <treenodetext=<em>"Node4"</em> draggable=<em>"true"</em> acceptdrop=<em>"true"</em>></p>
<p align="left">??????? </treenode></p>
<p align="left">????? </treenode></p>
<p align="left">??? </tree></p>
<p align="left">?</p>
<p align="left">??? <treename=<em>"demoTree2"</em> containerId=<em>"demoTree2"</em> checkboxes=<em>"true"</em></p>
<p align="left">????? checkboxesThreeState=<em>"true"</em> dynamicLoading=<em>"true"</em> width=<em>"240"</em>></p>
<p align="left">????? <treenodetext=<em>"</em><em>北京市</em><em>"</em>/></p>
<p align="left">????? <treenodetext=<em>"</em><em>上海市</em><em>"</em>/></p>
<p align="left">?????? ***</p>
<p align="left">??? </tree></p>
<p align="left">? </components></p>
<p align="left"></page></p>
<p align="left">?</p>
<p align="left">8.启动浏览器访问: <span style="text-decoration: underline;"><span><a href="http://localhost:8080/demo/hello.do">http://localhost:8080/demo/hello.do</a> </span></span></p>
<p align="left"><br>您会看到:</p>
<p><img style="padding: 1px; border: 1px solid #999999;" src="http://simpleframework.net/%24resource/default/filePager/jsp/dl.jsp?__file_beanId=1483841273_1967688702&__file_Id=2653&job=sys_account_normal" alt="Simpleframework组件与FreeMarker 调整例子"></p>
<p>?</p>
<p>完整的工程 <span style="color: #ff0000;"><span style="font-size: 28px;"><a href="http://simpleframework.googlecode.com/files/Simple-FreeMarker.zip">点击下载</a></span></span></p>
</div>
<p>?</p>
</div>
<p><br><a href="http://www.iteye.com/topic/842350">http://www.iteye.com/topic/842350</a></p>
<p><a href="http://www.iteye.com/topic/842348">http://www.iteye.com/topic/842348</a></p>
<p>?lz重复发帖了。</p> 2 楼 allbin1983 2010-12-15 这是为了发到论坛上,操作失误。