---
title: "在 SpringMVC 环境下使用 SimpleFramework"
author: "allbin1983"
date: "2010-12-25T00:34:02Z"
tags: []
categories: ["企业架构"]
slug: "springmvc-simpleframework"
draft: false
lastmod: "2010-12-25T00:34:02Z"
id: 851565
url: "https://allbin1983.iteye.com/blog/851565"
---

本例主要探讨如何整合SimpleFramework 与Spring Web MVC 框架，通过实例演示SimpleFramework的树组件（Tree）与服务器端的Spring Web MVC 框架进行整合的过程。

**前提条件**

本教程假定读者已经熟悉Spring Web MVC，并能配置相应的环境。但SimpleFramework相关知识并无特别要求，文章里会细致讲解相关的Simple 的配置过程。

**系统要求**

教程中的示例所用的工具和环境如下：

- JDK 1.6.0+
- Spring Framework 2.5.5 及其依赖项
- Tomcat 6.0.14
- eclipse 3.4.1
- SimpleFramework V3 及其依赖

示例用到了代码工程我已经上传,[点击下载](http://simpleframework.googlecode.com/files/Simple-Spring-demo.rar)。

下载后导入eclipse 发布到tomcat，就可以把例子跑起来，如果有疑问，请跟帖。

1\. 第一步，在web.xml配置simple与spring的Filter。web.xml 如下：

 

&lt;?xmlversion=*"1.0"* encoding=*"UTF-8"*?&gt;

&lt;web-appversion=*"2.4"*

    xmlns=*"[http://java.sun.com/xml/ns/j2ee](http://java.sun.com/xml/ns/j2ee)"*

    xmlns:xsi=*"[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"*

    xsi:schemaLocation=*"[http://java.sun.com/xml/ns/j2ee](http://java.sun.com/xml/ns/j2ee)*

    *[http://java.sun.com/xml/ns/j2ee/web-app\_2\_4.xsd](http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd)"*&gt;

    &lt;welcome-file-list&gt;

    &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;

    &lt;/welcome-file-list&gt;

 

    &lt;!-- 配置dispatchAction --&gt;

    &lt;servlet&gt;

       &lt;servlet-name&gt;spring&lt;/servlet-name&gt;

    &lt;servlet-class&gt; org.springframework.web.servlet.DispatcherServlet &lt;/servlet-class&gt;

    &lt;/servlet&gt;

    &lt;servlet-mapping&gt;

       &lt;servlet-name&gt;spring&lt;/servlet-name&gt;

       &lt;url-pattern&gt;\*.spring&lt;/url-pattern&gt;

    &lt;/servlet-mapping&gt;

 

    &lt;!--配置 simpleframework   --&gt;

    &lt;filter&gt;

       &lt;filter-name&gt;actionFilter&lt;/filter-name&gt;

        &lt;filter-class&gt;net.simpleframework.web.page.PageFilter&lt;/filter-class&gt;

    &lt;/filter&gt;

 

    &lt;filter-mapping&gt;

       &lt;filter-name&gt;actionFilter&lt;/filter-name&gt;

       &lt;url-pattern&gt;\*. jsp &lt;/url-pattern&gt;

    &lt;/filter-mapping&gt;

 

    &lt;filter-mapping&gt;

       &lt;filter-name&gt;actionFilter&lt;/filter-name&gt;

       &lt;url-pattern&gt;\*.spring&lt;/url-pattern&gt;

    &lt;/filter-mapping&gt;

 

    &lt;listener&gt;

    &lt;listener-class&gt;net.simpleframework.web.page.PageEventAdapter&lt;/listener-class&gt;

    &lt;/listener&gt;

 

&lt;/web-app&gt;

2.     第二步，编写Spring测试代码

**package**com.spring.mvc.test;

 

**import**java.io.IOException;

 

**import**javax.servlet.ServletContext;

**import**javax.servlet.ServletException;

**import**javax.servlet.http.HttpServletRequest;

**import**javax.servlet.http.HttpServletResponse;

 

**import**org.apache.struts.tiles.ComponentContext;

**import**org.springframework.web.servlet.ModelAndView;

**import**org.springframework.web.servlet.mvc.Controller;

 

/\**

 * Spring MVC  Demo

 * **@author** andy

 * **@from**   simpleframework

 * **@date**   2010-11-24

 \*/

**publicclass**HelloController **implements**Controller,

       org.apache.struts.tiles.Controller {

 

    String jsp= "";

    //注入  要转到的 jsp 名称

    **publicvoid**setJsp(String jsp) {

       **this**.jsp= jsp;

    }

 

 

    **public**ModelAndView handleRequest(

           HttpServletRequest request,

           HttpServletResponse response) **throws**Exception {

       String txtWord  = request.getParameter("txtWord");

       //相当于

       /\**

        * request.setAttribute("txtWord", txtWord);

        * request.getRequestDispatcher( jsp ).forward(request, response);

        \*/

       **returnnew**ModelAndView(jsp,"txtWord",txtWord);

    }

 

    **publicvoid**execute(ComponentContext arg0, HttpServletRequest arg1,

           HttpServletResponse arg2, ServletContext arg3) **throws**Exception {

       // **TODO**Auto-generated method stub

 

    }

 

    **publicvoid**perform(ComponentContext arg0, HttpServletRequest arg1,

           HttpServletResponse arg2, ServletContext arg3)

           **throws**ServletException, IOException {

       // **TODO**Auto-generated method stub

 

    }

 

}

3.      第三步，编写展示jsp 代码：index.jsp 与对应的index.xml  (输入页面)

 

&lt;%@page language=*"java"* import=*"java.util.\*"* pageEncoding=*"UTF-8"*%&gt;

&lt;!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

    &lt;title&gt;Spring MVC Demo&lt;/title&gt;

    &lt;metahttp-equiv=*"pragma"* content=*"no-cache"*&gt;

    &lt;metahttp-equiv=*"cache-control"* content=*"no-cache"*&gt;

    &lt;metahttp-equiv=*"expires"* content=*"0"*&gt;   

    &lt;metahttp-equiv=*"keywords"* content=*"keyword1,keyword2,keyword3"*&gt;

    &lt;metahttp-equiv=*"description"* content=*"This is my page"*&gt;

  &lt;/head&gt;

  &lt;body&gt;

  &lt;formaction=*"hello.spring"*&gt;

    &lt;h2&gt;Spring MVC  DEMO&lt;/h2&gt;

    &lt;div&gt;

    Please  Enter a  word &lt;inputtype=*"text"* name=*"txtWord"* /&gt;

    &lt;br/&gt;

    &nbsp;&lt;inputtype=*"submit"*&gt;

    &lt;/div&gt;

  &lt;/form&gt;

 

   &lt; h1 &gt; simple 输出的内容 &lt; h1 &gt; &lt;br&gt; 

   &lt; table &gt;

 

    &lt;tr&gt;

       &lt;tdwidth=*"50%"* valign=*"top"*&gt;

       &lt;div&gt;可以拖动的普通树&lt;/div&gt;

       &lt;divid=*"demoTree1"* style="padding: *8px*; border: *5px solid #ddd*;"&gt;&lt;/div&gt;

       &lt;/td&gt;

       &lt;tdwidth=*"50%"* valign=*"top"*&gt;

       &lt;div&gt;动态装载的树，可以多选&lt;/div&gt;

       &lt;divid=*"demoTree2"* style="padding: *8px*; border: *5px solid #ddd*;"&gt;&lt;/div&gt;

       &lt;/td&gt;

    &lt;/tr&gt;

 

  &lt;/table&gt;   

 

 

  &lt;/body&gt;

&lt;/html&gt;

 

Index.xml

&lt;?xmlversion=*"1.0"* encoding=*"UTF-8"*?&gt;

&lt;pagexmlns:xsi=*"[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"*

  xsi:noNamespaceSchemaLocation=*"/xsd/default/simple.xsd"*&gt;

    &lt;responseCharacterEncoding&gt; gbk &lt;/responseCharacterEncoding&gt;

  &lt;components&gt;

    &lt;treename=*"demoTree1"* containerId=*"demoTree1"* handleClass=*"net.simpleframework.example.MyTree"*

      width=*"240"*&gt;

      &lt;treenodetext=*"Root"* acceptdrop=*"true"*&gt;

         (省略树节点，本例是直接读取文件，下次单独介绍simple树使用)

      &lt;/treenode&gt;

   &lt;/components&gt;

&lt;/page&gt;

运行效果如下图：

![](http://simpleframework.net/%24resource/default/filePager/jsp/dl.jsp?__file_beanId=1483841273_1967688702&__file_Id=3936&job=sys_account_normal)

![](http://simpleframework.net/%24resource/default/filePager/jsp/dl.jsp?__file_beanId=1483841273_1967688702&__file_Id=3937&job=sys_account_normal)

 

&lt;script&gt;&lt;/script&gt;