日期:2014-05-20 浏览次数:21015 次
Simpleframework是基于后处理后处理模式技术实现的Web应用级开源框架。采用过滤器工作机制和申明式组件元数据定义,所以备整装零用的应 用模式,不仅可以支持Web应用的全程开发装,也可以在其他框架资源和应用的基础上仅引用其部分组件,本例介绍通过FreeMarker代替jsp做页面 展示,并在FreeMarker模板文件输出Simpleframework的树组件。
FreeMarker是开源的模板框架。对于它的介绍网上已经很多了。详情可参考
主页:http://www.freemarker.org/
现在我们就开始FreeMarker版的Hello World并实现输出Simpleframework树组件。
1.去这里下载 最新的FreeMarker.我下载的是Download freemarker-2.3.16
2.解压你下载的freemarker-2.3.16。
3.新建一web工程。我是Eclipse下做的。把freemarker-2.3.16/lib目录下的
jar文件复制到你建的web工程的lib目录下。
4.在WEB-INF目录下建一文件夹templates,然后在这个文件夹里面新建一个模板文件test.ftl
<html>
<meta http - equiv ="Content-Type" content="text/ 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
您会看到:
?
完整的工程 点击下载
?