|
本例主要探讨如何整合SimpleFramework 与Spring Web MVC 框架,通过实例演示SimpleFramework的树组件(Tree)与服务器端的Spring Web MVC 框架进行整合的过程。 前提条件 本教程假定读者已经熟悉Spring Web MVC,并能配置相应的环境。但SimpleFramework相关知识并无特别要求,文章里会细致讲解相关的Simple 的配置过程。 系统要求 教程中的示例所用的工具和环境如下:
示例用到了代码工程我已经上传,点击下载。 下载后导入eclipse 发布到tomcat,就可以把例子跑起来,如果有疑问,请跟帖。 1. 第一步,在web.xml配置simple与spring的Filter。web.xml 如下:
<?xmlversion="1.0" encoding="UTF-8"?> <web-appversion="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
<!-- 配置dispatchAction --> <servlet> <servlet-name>spring</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.spring</url-pattern> </servlet-mapping>
<!--配置 simpleframework --> <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>*.spring</url-pattern> </filter-mapping>
<listener> <listener-class>net.simpleframework.web.page.PageEventAdapter</listener-class> </listener>
</web-app> 2. 第二步,编写Spring测试代码 packagecom.spring.mvc.test;
importjava.io.IOException;
importjavax.servlet.ServletContext; importjavax.servlet.ServletException; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse;
importorg.apache.struts.tiles.ComponentContext; importorg.springframework.web.servlet.ModelAndView; importorg.springframework.web.servlet.mvc.Controller;
/** * Spring MVC Demo * @author andy * @from simpleframework * @date 2010-11-24 */ publicclassHelloController implementsController, org.apache.struts.tiles.Controller {
String jsp= ""; //注入 要转到的 jsp 名称 publicvoidsetJsp(String jsp) { this.jsp= jsp; }
publicModelAndView handleRequest( HttpServletRequest request, HttpServletResponse response) throwsException { String txtWord = request.getParameter("txtWord"); //相当于 /** * request.setAttribute("txtWord", txtWord); * request.getRequestDispatcher( jsp ).forward(request, response); */ returnnewModelAndView(jsp,"txtWord",txtWord); }
publicvoidexecute(ComponentContext arg0, HttpServletRequest arg1, HttpServletResponse arg2, ServletContext arg3) throwsException { // TODOAuto-generated method stub
}
publicvoidperform(ComponentContext arg0, HttpServletRequest arg1, HttpServletResponse arg2, ServletContext arg3) throwsServletException, IOException { // TODOAuto-generated method stub
}
} 3. 第三步,编写展示jsp 代码:index.jsp 与对应的index.xml (输入页面)
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Spring MVC Demo</title> <metahttp-equiv="pragma" content="no-cache"> <metahttp-equiv="cache-control" content="no-cache"> <metahttp-equiv="expires" content="0"> <metahttp-equiv="keywords" content="keyword1,keyword2,keyword3"> <metahttp-equiv="description" content="This is my page"> </head> <body> <formaction="hello.spring"> <h2>Spring MVC DEMO</h2> <div> Please Enter a word <inputtype="text" name="txtWord" /> <br/> <inputtype="submit"> </div> </form>
< h1 > simple 输出的内容 < h1 > <br> < table >
<tr> <tdwidth="50%" valign="top"> <div>可以拖动的普通树</div> <divid="demoTree1" style="padding: 8px; border: 5px solid #ddd;"></div> </td> <tdwidth="50%" valign="top"> <div>动态装载的树,可以多选</div> <divid="demoTree2" style="padding: 8px; border: 5px solid #ddd;"></div> </td> </tr>
</table>
</body> </html>
Index.xml <?xmlversion="1.0" encoding="UTF-8"?> <pagexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="/xsd/default/simple.xsd"> <responseCharacterEncoding> gbk </responseCharacterEncoding> <components> <treename="demoTree1" containerId="demoTree1" handleClass="net.simpleframework.example.MyTree" width="240"> <treenodetext="Root" acceptdrop="true"> (省略树节点,本例是直接读取文件,下次单独介绍simple树使用) </treenode> </components> </page> 运行效果如下图:
|