第一步:工具下载

首先下载struts2

        下载地址:http://struts.apache.org/download.cgi#struts2315-SNAPSHOT

第二下载:CKEditor

        下载地址:http://ckeditor.com/download   (最新版本ckeditor 4.1.2)

第三下载:CKFinder

        下载地址:http://cksource.com/ckfinder/download  (最新版本ckfinder 2.3.1)

 

第二步:新建项目

      打开myeclipse -->File --> New -->Web Project

      项目名:ThreeGorgesSoulSystem

 

第三步:整合

      首先解压:struts-2.3.14.2-all.zip,然后拷贝最小安装包放在WEB-INF/lib下:

       在struts-2.3.14.2\apps打开struts2-blank.war,在WEB-INF\src\java下,拷贝struts.xml到classpath(src)下

       编辑WEB-INF\web.xml

 

  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app id="WebApp_ID" version="2.4"  
  3.     xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  5.   
  6.   <filter>  
  7.     <filter-name>struts2</filter-name>  
  8.     <!--   
  9.       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
  10.     -->  
  11.     <filter-class>com.orange.web.filter.FCKFilter</filter-class>  
  12.   </filter>  
  13.   <filter-mapping>  
  14.     <filter-name>struts2</filter-name>  
  15.     <url-pattern>/*</url-pattern>  
  16.   </filter-mapping>  
  17.   <welcome-file-list>  
  18.     <welcome-file>/index.jsp</welcome-file>  
  19.   </welcome-file-list>  
  20. </web-app></span>  

 

        解压ckeditor_ckeditor_4.1.2_full.zip,然后将ckeditor文件夹全部拷到WebRoot根目录下。

      修改ckeditor文件下的config.js文件,代码如下:

  1. <span style="font-size:18px;">/*  
  2. Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.  
  3. For licensing, see LICENSE.html or http://ckeditor.com/license  
  4. */  
  5.   
  6. CKEDITOR.editorConfig = function (config) {  
  7.     // Define changes to default configuration here. For example:  
  8.     // config.language = 'fr';  
  9.     // config.uiColor = '#AADC6E';  
  10.   
  11.     //CKFinder.SetupCKEditor(null, '../ckfinder/'); //注意ckfinder的路径对应实际放置的位置  
  12.     config.skin = 'v2'; // 皮肤  
  13.     //config.uiColor = '#FFF'; // 皮肤背景颜色  
  14.     //config.resize_enabled = false;   // 取消 “拖拽以改变尺寸”功能     
  15.     config.language = 'zh-cn'; //配置语言   
  16.     config.font_names = '宋体;楷体_GB2312;新宋体;黑体;隶书;幼圆;微软雅黑;Arial;Comic Sans MS;Courier New;Tahoma;Times New Roman;Verdana'; // 字体  
  17.     config.width = 1000; //宽度  
  18.     config.height = 450; //高度  
  19.   
  20.     //config.toolbar = "Basic";  // 基础工具栏  
  21.     config.toolbar = "Full";  // 全能工具栏   
  22.     // 自定义工具栏    
  23.     config.toolbar_Full =  
  24.     [  
  25.         ['Source', '-', 'Preview', '-', 'Templates'],  
  26.         ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Print', 'SpellChecker', 'Scayt'],  
  27.         ['Undo', 'Redo', '-', 'Find', 'Replace', '-', 'SelectAll', 'RemoveFormat'],  
  28.         ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],  
  29.         '/',  
  30.         ['Bold', 'Italic', 'Underline', 'Strike', '-', 'Subscript', 'Superscript'],  
  31.         ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote', 'CreateDiv'],  
  32.         ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],  
  33.         ['Link', 'Unlink', 'Anchor'],  
  34.           ['Image', 'Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak'],  
  35.         '/',  
  36.         ['Styles', 'Format', 'Font', 'FontSize'],  
  37.         ['TextColor', 'BGColor'],  
  38.         ['Maximize', 'ShowBlocks', '-', 'About']  
  39.     ];  
  40.   
  41.     config.toolbar_Basic =  
  42.     [  
  43.         ['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink', '-', 'About']  
  44.     ];  
  45.       
  46.     /*** 文件上传功能的配置-----------------------  
  47.      */  
  48.     config.filebrowserBrowseUrl = 'ckfinder/ckfinder.html';   
  49.     config.filebrowserImageBrowseUrl = 'ckfinder/ckfinder.html?type=Images';  
  50.     config.filebrowserFlashBrowseUrl = 'ckfinder/ckfinder.html?type=Flash';  
  51.     config.filebrowserUploadUrl = 'ckfinder/core/connector/java/connector.java?command=QuickUpload&type=Files';  
  52.     config.filebrowserImageUploadUrl = 'ckfinder/core/connector/java/connector.java?command=QuickUpload&type=Images';  
  53.     config.filebrowserFlashUploadUrl = 'ckfinder/core/connector/java/connector.java?command=QuickUpload&type=Flash';  
  54.     //---------------------------文件上传功能的配置结束--------  
  55. };</span>  

        接着解压:ckfinder_java_2.3.1.zip,在ckfinder目录下再解压CKFinderJava.war,将CKFinderJava下的ckfinder文件夹拷贝到WebRoot根目录下,目录结构如下图:

       接着把config.xml拷贝到WEB-INF/下

       配置config.xml文件:

  1. <span style="font-size:18px;"><config>  
  2.   
  3.     <!-- 开启上传功能,如果为false为提示  
  4.     :因为安全原因,文件不可浏览. 请联系系统管理员并检查CKFinder配置文件.   
  5.     并且默认你是是为false的 -->  
  6.     <enabled>true</enabled>  
  7.   
  8.     <!-- 文件存放的地址    /ckfinder/userfiles/   (自动创建) -->  
  9.     <baseURL>/ThreeGorgesSoulSystem/upload/</baseURL>  
  10.       
  11.       
  12.     <!--检查双扩展名   -->  
  13.     <checkDoubleExtension>true</checkDoubleExtension>  
  14.       
  15.     <!-- 在缩放后检查大小 -->  
  16.     <checkSizeAfterScaling>true</checkSizeAfterScaling>  
  17.       
  18.   
  19.     <!-- 配置路径以及格式 -->  
  20.     <types>  
  21.         <type name="Files">  
  22.             <url>%BASE_URL%files/</url>  
  23.             <directory>%BASE_DIR%files</directory>  
  24.             <maxSize>0</maxSize>  
  25.             <allowedExtensions>7z,aiff,asf,avi,bmp,csv,doc,docx,fla,flv,gif,gz,gzip,jpeg,jpg,mid,mov,mp3,mp4,mpc,mpeg,mpg,ods,odt,pdf,png,ppt,pptx,pxd,qt,ram,rar,rm,rmi,rmvb,rtf,sdc,sitd,swf,sxc,sxw,tar,tgz,tif,tiff,txt,vsd,wav,wma,wmv,xls,xlsx,zip  
  26.             </allowedExtensions>  
  27.             <deniedExtensions></deniedExtensions>  
  28.         </type>  
  29.         <type name="Images">  
  30.             <url>%BASE_URL%images/</url>  
  31.             <directory>%BASE_DIR%images</directory>  
  32.             <maxSize>0</maxSize>  
  33.             <allowedExtensions>bmp,gif,jpeg,jpg,png</allowedExtensions>  
  34.             <deniedExtensions></deniedExtensions>  
  35.         </type>  
  36.         <type name="Flash">  
  37.             <url>%BASE_URL%flash/</url>  
  38.             <directory>%BASE_DIR%flash</directory>  
  39.             <maxSize>0</maxSize>  
  40.             <allowedExtensions>swf,flv</allowedExtensions>  
  41.             <deniedExtensions></deniedExtensions>  
  42.         </type>  
  43.     </types>  
  44.       
  45.     <!--  可允许的操作  -->  
  46.     <accessControls>  
  47.         <accessControl>  
  48.             <role>*</role>  
  49.             <resourceType>*</resourceType>  
  50.             <folder>/</folder>  
  51.             <folderView>true</folderView>  
  52.             <folderCreate>true</folderCreate>  
  53.             <folderRename>true</folderRename>  
  54.             <folderDelete>true</folderDelete>  
  55.             <fileView>true</fileView>  
  56.             <fileUpload>true</fileUpload>  
  57.             <fileRename>true</fileRename>  
  58.             <fileDelete>true</fileDelete>  
  59.         </accessControl>  
  60.     </accessControls>  
  61.     <thumbs>  
  62.         <enabled>true</enabled>  
  63.         <url>%BASE_URL%_thumbs/</url>  
  64.         <directory>%BASE_DIR%_thumbs</directory>  
  65.         <directAccess>false</directAccess>  
  66.         <maxHeight>100</maxHeight>  
  67.         <maxWidth>100</maxWidth>  
  68.         <quality>80</quality>  
  69.     </thumbs>  
  70.       
  71.       
  72.     <!-- 图片浏览——图片缩略图 -->  
  73.     <plugins>  
  74.         <plugin>  
  75.             <name>imageresize</name>  
  76.             <class>com.ckfinder.connector.plugins.ImageResize</class>  
  77.             <params>  
  78.                 <param name="smallThumb" value="90x90"></param>  
  79.                 <param name="mediumThumb" value="120x120"></param>  
  80.                 <param name="largeThumb" value="180x180"></param>  
  81.             </params>  
  82.         </plugin>  
  83.         <plugin>  
  84.             <name>fileeditor</name>  
  85.             <class>com.ckfinder.connector.plugins.FileEditor</class>  
  86.             <params></params>  
  87.         </plugin>  
  88.     </plugins>  
  89.     <basePathBuilderImpl>com.ckfinder.connector.configuration.ConfigurationPathBuilder</basePathBuilderImpl>  
  90. </config>  
  91.     </span>  

 

      在配置web.xml文件中添加ckeditor+ckfinder配置信息

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app id="WebApp_ID" version="2.4"  
  3.     xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  5.   
  6.   <filter>  
  7.     <filter-name>struts2</filter-name>  
  8.     <!--   
  9.       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
  10.     -->  
  11.     <filter-class>com.orange.web.filter.FCKFilter</filter-class>  
  12.   </filter>  
  13.   <filter-mapping>  
  14.     <filter-name>struts2</filter-name>  
  15.     <url-pattern>/*</url-pattern>  
  16.   </filter-mapping>  
  17.   
  18.  <!-- =================ckeditor+ckfinder图片上传功能的配置=================== -->  
  19.    <servlet>  
  20.         <!-- servlet的命 名===注册名称,这个名称可以任意的        -->  
  21.         <servlet-name>ConnectorServlet</servlet-name>  
  22.         <!-- servlet 所在包  -->  
  23.         <servlet-class>com.ckfinder.connector.ConnectorServlet</servlet-class>  
  24.         <init-param>  
  25.             <!-- 初始化设置 -->  
  26.             <param-name>XMLConfig</param-name>  
  27.             <param-value>/WEB-INF/config.xml</param-value>  
  28.         </init-param>  
  29.         <init-param>  
  30.             <!-- 设置是否可以查看debug,在正常时候需要设置为false. -->  
  31.             <param-name>debug</param-name>  
  32.             <param-value>false</param-value>  
  33.         </init-param>  
  34.           
  35.         <!--   
  36.           
  37.         load-on-startup 元素在web应用启动的时候指定了  
  38.         servlet被加载的顺序,它的值必须是一个整数。  
  39.         如果它的值是一个负整数或是这个元素不存在,  
  40.         那么容器会在该servlet被调用的时候,加载这个servlet   
  41.         。如果值是正整数或零,容器在配置的时候就加载并初始化这个servlet,  
  42.         容器必须保证值小的先被加载。如果值相等,容器可以自动选择先加载谁。  
  43.   
  44.           
  45.         在此不是必须的  
  46.          -->  
  47.         <load-on-startup>1</load-on-startup>  
  48.     </servlet>  
  49.       
  50.       
  51.     <!-- 映射的相对路径,主要是在其他地方使用,比如js,页面访问  -->  
  52.     <servlet-mapping>  
  53.         <servlet-name>ConnectorServlet</servlet-name>  
  54.         <url-pattern>  
  55.             /ckfinder/core/connector/java/connector.java  
  56.         </url-pattern>  
  57.     </servlet-mapping>  
  58.   
  59. <!-- =================ckeditor+ckfinder图片上传功能的配置结束=================== -->  
  60.     
  61.   <welcome-file-list>  
  62.     <welcome-file>/index.jsp</welcome-file>  
  63.   </welcome-file-list>  
  64. </web-app>  

 

注:1.将ckfinder中的_samples文件夹直接删除

         2.如果jquery.min.js出错,是因为采用了压缩的jquery库,不被识别,在http://jquery.com/download/下载

       点击链接,把jQuery2.0.2中的代码直接替换项目中jquery.min.js中的代码

   

 

第四步:java代码和页面

action层中的代码:    

  1. package com.orange.web.action;  
  2. import java.sql.SQLException;  
  3. import com.orange.domain.Classify;  
  4. import com.orange.domain.Menu;  
  5. import com.orange.service.ClassifyService;  
  6. import com.orange.service.MenuService;  
  7. import com.orange.service.impl.ClassifyServiceImpl;  
  8. import com.orange.service.impl.MenuServiceImpl;  
  9.   
  10.   
  11. public class ClassifyAction {  
  12.     /**  
  13.      * menuId  
  14.      */  
  15.     private int menuId;  
  16.     /**  
  17.      * 分类信息  
  18.      */  
  19.     private Classify classify;  
  20.     /**  
  21.      * 获取文本域中的内容(ckeditor中textared内容)  
  22.      */  
  23.     private String editor1;  
  24.     /**  
  25.      * 简介  
  26.      */  
  27.     private Classify intro;  
  28.     /**  
  29.      * 文化  
  30.      */  
  31.     private Classify cultural;  
  32.     /**  
  33.      * 精神  
  34.      */  
  35.     private Classify spirit;  
  36.     /**  
  37.      * 旅游  
  38.      */  
  39.     private Classify tourism;  
  40.   
  41.     private ClassifyService classifyService = new ClassifyServiceImpl();  
  42.   
  43.     private MenuService menuService = new MenuServiceImpl();  
  44.   
  45.     public String listToMenuId() {  
  46.         try {  
  47.             classify = classifyService.listToMenuId(menuId);  
  48.         } catch (SQLException e) {  
  49.             // TODO Auto-generated catch block  
  50.             e.printStackTrace();  
  51.         }  
  52.         return "listToMenuId";  
  53.     }  
  54.     public String updateClassify(){  
  55.         Menu menu = new Menu();  
  56.         try {  
  57.             menu = menuService.listMenu(menuId);  
  58.             classify.setMenu(menu);  
  59.             classify.setClassifyContent(editor1);  
  60.             classifyService.updateClassify(classify);  
  61.         } catch (SQLException e) {  
  62.             // TODO Auto-generated catch block  
  63.             e.printStackTrace();  
  64.         }  
  65.         return initalize();  
  66.     }  
  67.     public String initalize() {  
  68.         try {  
  69.             intro = classifyService.listToMenuId(11000);  
  70.             cultural = classifyService.listToMenuId(21000);  
  71.             spirit = classifyService.listToMenuId(31000);  
  72.             tourism = classifyService.listToMenuId(41000);  
  73.               
  74.         } catch (SQLException e) {  
  75.             // TODO Auto-generated catch block  
  76.             e.printStackTrace();  
  77.         }  
  78.         return "threegorgessoul";  
  79.     }  
  80.     public int getMenuId() {  
  81.         return menuId;  
  82.     }  
  83.     public void setMenuId(int menuId) {  
  84.         this.menuId = menuId;  
  85.     }  
  86.     public Classify getClassify() {  
  87.         return classify;  
  88.     }  
  89.     public void setClassify(Classify classify) {  
  90.         this.classify = classify;  
  91.     }  
  92.     public String getEditor1() {  
  93.         return editor1;  
  94.     }  
  95.     public void setEditor1(String editor1) {  
  96.         this.editor1 = editor1;  
  97.     }  
  98.     public Classify getIntro() {  
  99.         return intro;  
  100.     }  
  101.     public void setIntro(Classify intro) {  
  102.         this.intro = intro;  
  103.     }  
  104.     public Classify getCultural() {  
  105.         return cultural;  
  106.     }  
  107.     public void setCultural(Classify cultural) {  
  108.         this.cultural = cultural;  
  109.     }  
  110.     public Classify getSpirit() {  
  111.         return spirit;  
  112.     }  
  113.     public void setSpirit(Classify spirit) {  
  114.         this.spirit = spirit;  
  115.     }  
  116.     public Classify getTourism() {  
  117.         return tourism;  
  118.     }  
  119.     public void setTourism(Classify tourism) {  
  120.         this.tourism = tourism;  
  121.     }  
  122.       
  123. }  

service层:

  1. package com.orange.service.impl;  
  2. import java.sql.SQLException;  
  3. import java.util.List;  
  4. import com.orange.dao.ClassifyDao;  
  5. import com.orange.dao.impl.ClassifyDaoImpl;  
  6. import com.orange.domain.Classify;  
  7. import com.orange.service.ClassifyService;  
  8.   
  9. public class ClassifyServiceImpl implements ClassifyService {  
  10.       
  11.     private ClassifyDao classifyDao = new ClassifyDaoImpl();  
  12.       
  13.     /**  
  14.      * 添加分类  
  15.      *   
  16.      * @param classify  
  17.      * @throws SQLException  
  18.      */  
  19.     public void addClassify(Classify classify) throws SQLException {  
  20.         classifyDao.addClassify(classify);  
  21.     }  
  22.   
  23.     /**  
  24.      * 修改评论  
  25.      *   
  26.      * @param classify  
  27.      * @throws SQLException  
  28.      */  
  29.     public void updateClassify(Classify classify) throws SQLException {  
  30.         classifyDao.updateClassify(classify);  
  31.     }  
  32.   
  33.     /**  
  34.      * 删除评论  
  35.      *   
  36.      * @param classify_Id  
  37.      * @throws SQLException  
  38.      */  
  39.     public void deleteClassify(int classifyId) throws SQLException {  
  40.         classifyDao.deleteClassify(classifyId);  
  41.     }  
  42.   
  43.     /**  
  44.      * 根据id查看评论  
  45.      *   
  46.      * @param classify_Id  
  47.      * @return  
  48.      * @throws SQLException  
  49.      */  
  50.     public Classify listClassify(int classifyId) throws SQLException {  
  51.         return classifyDao.listClassify(classifyId);  
  52.     }  
  53.     /**  
  54.      * 根据menuId查看评论  
  55.      *   
  56.      * @param classify_Id  
  57.      * @return  
  58.      * @throws SQLException  
  59.      */  
  60.     public Classify listToMenuId(int menuId) throws SQLException {  
  61.         return classifyDao.listToMenuId(menuId);  
  62.     }  
  63.   
  64.     /**  
  65.      * 查看所有评论信息  
  66.      *   
  67.      * @return  
  68.      * @throws SQLException  
  69.      */  
  70.     public List<Classify> listAllClassify() throws SQLException {  
  71.         return classifyDao.listAllClassify();  
  72.     }  
  73.   
  74.     /**  
  75.      * 查询从page条数据开始,总控查询rows条记录  
  76.      *   
  77.      * @param page  
  78.      * @param rows  
  79.      * @return  
  80.      * @throws SQLException  
  81.      */  
  82.     public List<Classify> listAllClassify(int page, int rows)  
  83.             throws SQLException {  
  84.         return classifyDao.listAllClassify(page, rows);  
  85.     }  
  86.   
  87.     /**  
  88.      * 查询数据库中得所有记录数据  
  89.      *   
  90.      * @return  
  91.      * @throws SQLException  
  92.      */  
  93.     public int getCount() throws SQLException {  
  94.         return classifyDao.getCount();  
  95.     }  
  96.   
  97. }  

dao层:

  1. package com.orange.dao.impl;  
  2. import java.sql.Connection;  
  3. import java.sql.PreparedStatement;  
  4. import java.sql.ResultSet;  
  5. import java.sql.SQLException;  
  6. import java.util.ArrayList;  
  7. import java.util.LinkedList;  
  8. import java.util.List;  
  9. import com.orange.common.DataBaseUtils;  
  10. import com.orange.dao.ClassifyDao;  
  11. import com.orange.domain.Classify;  
  12. import com.orange.domain.Menu;  
  13.   
  14. public class ClassifyDaoImpl implements ClassifyDao {  
  15.     Classify classify = new Classify();  
  16.   
  17.     public void addClassify(Classify classify) throws SQLException {  
  18.         String sql = "insert into t_classify(classify_title,classify_content,menu_id) values(?, ?, ?)";  
  19.         Object[] params = {  
  20.             classify.getClassifyTitle(),  
  21.             classify.getClassifyContent(),   
  22.             classify.getMenu().getMenuId()  
  23.         };  
  24.   
  25.         DataBaseUtils.executeUpdate(sql, params);  
  26.   
  27.     }  
  28.   
  29.     public void updateClassify(Classify classify) throws SQLException {  
  30.         String sql = "update t_classify set classify_title=?,classify_content=?,menu_id=? where classify_id = ?";  
  31.         Object[] params = {                   
  32.                 classify.getClassifyTitle(),  
  33.                 classify.getClassifyContent(),   
  34.                 classify.getMenu().getMenuId(),  
  35.                 classify.getClassifyId()   
  36.   
  37.         };  
  38.         DataBaseUtils.executeUpdate(sql, params);  
  39.   
  40.     }  
  41.   
  42.     public void deleteClassify(int classifyId) throws SQLException {  
  43.         String sql = "delete  from t_classify where classify_Id=?";  
  44.         Object[] params = { classifyId };  
  45.   
  46.         DataBaseUtils.executeUpdate(sql, params);  
  47.   
  48.     }  
  49.     public Classify listClassify(int classifyId) throws SQLException {  
  50.         Classify classify = null;  
  51.         Connection conn = null;  
  52.         PreparedStatement pst = null;  
  53.         ResultSet res = null;  
  54.   
  55.         try {  
  56.   
  57.             conn = DataBaseUtils.getConnection();  
  58.   
  59.             pst = conn.prepareStatement("SELECT  " +  
  60.                     "c.classify_id,c.classify_title,c.classify_content,c.menu_id, " +  
  61.                     "m.menu_id,m.menu_name,m.model_id " +  
  62.                     " FROM " +  
  63.                     "t_classify AS c , " +  
  64.                     "t_menu AS m " +  
  65.                     " WHERE " +  
  66.                     "c.menu_id =  m.menu_id AND " +  
  67.                     "c.classify_id =  ?");  
  68.   
  69.             pst.setInt(1, classifyId);  
  70.   
  71.             res = pst.executeQuery();  
  72.   
  73.             if (res.next()) {  
  74.   
  75.                 classify = new Classify();  
  76.                 classify.setClassifyId(res.getInt("classify_Id"));  
  77.                 classify.setClassifyTitle(res.getString("classify_title"));  
  78.                 classify.setClassifyContent(res.getString("classify_content"));  
  79.                 Menu menu = new Menu();  
  80.                 menu.setMenuId(res.getInt("menu_id"));  
  81.                 menu.setMenuName(res.getString("menu_name"));  
  82.                 menu.setModel(null);  
  83.                 classify.setMenu(menu);  
  84.             }  
  85.   
  86.         } finally {  
  87.             DataBaseUtils.close(conn, pst, res);  
  88.         }  
  89.   
  90.         return classify;  
  91.   
  92.     }  
  93.     /**  
  94.      * 根据menuId查看评论  
  95.      *   
  96.      * @param classify_Id  
  97.      * @return  
  98.      * @throws SQLException  
  99.      */  
  100.     public Classify listToMenuId(int menuId) throws SQLException {  
  101.         Classify classify = null;  
  102.         Connection conn = null;  
  103.         PreparedStatement pst = null;  
  104.         ResultSet res = null;  
  105.   
  106.         try {  
  107.   
  108.             conn = DataBaseUtils.getConnection();  
  109.   
  110.             pst = conn.prepareStatement("SELECT  " +  
  111.                     "c.classify_id,c.classify_title,c.classify_content,c.menu_id, " +  
  112.                     "m.menu_id,m.menu_name,m.model_id " +  
  113.                     " FROM " +  
  114.                     "t_classify AS c , " +  
  115.                     "t_menu AS m " +  
  116.                     " WHERE " +  
  117.                     "c.menu_id =  m.menu_id AND " +  
  118.                     "c.menu_id =  ?");  
  119.   
  120.             pst.setInt(1, menuId);  
  121.   
  122.             res = pst.executeQuery();  
  123.   
  124.             if (res.next()) {  
  125.   
  126.                 classify = new Classify();  
  127.                 classify.setClassifyId(res.getInt("classify_Id"));  
  128.                 classify.setClassifyTitle(res.getString("classify_title"));  
  129.                 classify.setClassifyContent(res.getString("classify_content"));  
  130.                 Menu menu = new Menu();  
  131.                 menu.setMenuId(res.getInt("menu_id"));  
  132.                 menu.setMenuName(res.getString("menu_name"));  
  133.                 menu.setModel(null);  
  134.                 classify.setMenu(menu);  
  135.             }  
  136.   
  137.         } finally {  
  138.             DataBaseUtils.close(conn, pst, res);  
  139.         }  
  140.   
  141.         return classify;  
  142.   
  143.     }  
  144.     public List<Classify> listAllClassify() throws SQLException {  
  145.   
  146.         List<Classify> classifys = new LinkedList<Classify>();  
  147.         Connection conn = null;  
  148.         PreparedStatement pst = null;  
  149.         ResultSet res = null;  
  150.   
  151.         try {  
  152.   
  153.             conn = DataBaseUtils.getConnection();  
  154.   
  155.             pst = conn  
  156.                     .prepareStatement("SELECT c.classify_id,c.classify_title,c.classify_content,c.menu_id,m.menu_id,m.menu_name,m.model_id FROMt_classify AS c ,t_menu AS m WHERE c.menu_id = m.model_id");  
  157.   
  158.             res = pst.executeQuery();  
  159.   
  160.             while (res.next()) {  
  161.                 classify = new Classify();  
  162.                 classify.setClassifyId(res.getInt("classify_Id"));  
  163.                 classify.setClassifyTitle(res.getString("classify_title"));  
  164.                 classify.setClassifyContent(res.getString("classify_content"));  
  165.                 Menu menu = new Menu();  
  166.                 menu.setMenuId(res.getInt("menu_id"));  
  167.                 menu.setMenuName(res.getString("menu_name"));  
  168.                 menu.setModel(null);  
  169.                 classify.setMenu(menu);  
  170.             }  
  171.   
  172.         } finally {  
  173.             DataBaseUtils.close(conn, pst, res);  
  174.         }  
  175.   
  176.         return classifys;  
  177.     }  
  178.   
  179.     public List<Classify> listAllClassify(int page, int rows)  
  180.             throws SQLException {  
  181.   
  182.         Connection conn = null;  
  183.         PreparedStatement pst = null;  
  184.         ResultSet res = null;  
  185.         List<Classify> classifys = new ArrayList<Classify>();  
  186.         try {  
  187.             // 连接数据库  
  188.             conn = DataBaseUtils.getConnection();  
  189.             // 创建sql执行器  
  190.             pst = conn  
  191.                     .prepareStatement("SELECT c.classify_id,c.classify_title,c.classify_content,c.menu_id,m.menu_id,m.menu_name,m.model_id FROMt_classify AS c ,t_menu AS m WHERE c.menu_id = m.model_id");  
  192.             pst.setInt(1, (page - 1) * rows);  
  193.             pst.setInt(2, rows);  
  194.             // 执行sql语句  
  195.             res = pst.executeQuery();  
  196.   
  197.             while (res.next()) {  
  198.   
  199.                 Classify classify = new Classify();  
  200.                 classify = new Classify();  
  201.                 classify.setClassifyId(res.getInt("classify_Id"));  
  202.                 classify.setClassifyTitle(res.getString("classify_title"));  
  203.                 classify.setClassifyContent(res.getString("classify_content"));  
  204.                 Menu menu = new Menu();  
  205.                 menu.setMenuId(res.getInt("menu_id"));  
  206.                 menu.setMenuName(res.getString("menu_name"));  
  207.                 menu.setModel(null);  
  208.                 classify.setMenu(menu);  
  209.                 classifys.add(classify);  
  210.             }  
  211.         } finally {  
  212.             DataBaseUtils.close(conn, pst, res);  
  213.         }  
  214.         return classifys;  
  215.     }  
  216.   
  217.     public int getCount() throws SQLException {  
  218.   
  219.         Connection conn = null;  
  220.         PreparedStatement pst = null;  
  221.         ResultSet res = null;  
  222.         int count = -1;  
  223.   
  224.         conn = DataBaseUtils.getConnection();  
  225.         pst = conn.prepareStatement("select count(0) as count from t_classify");  
  226.         res = pst.executeQuery();  
  227.   
  228.         res.next();  
  229.         count = res.getInt(1);  
  230.         return count;  
  231.     }  
  232.   
  233. }  

连接数据库信息:

  1. package com.orange.common;  
  2. import java.sql.Connection;  
  3. import java.sql.DriverManager;  
  4. import java.sql.PreparedStatement;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7.   
  8. public class DataBaseUtils {  
  9.   
  10.     public static Connection getConnection() throws SQLException {  
  11.         try {  
  12.           
  13.             Class.forName("com.mysql.jdbc.Driver");  
  14.               
  15.             Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/threegorgessouldb""root""123456");  
  16.               
  17.             return conn;  
  18.         } catch (ClassNotFoundException e) {  
  19.             e.printStackTrace();  
  20.             throw new SQLException("没有找到驱动包");  
  21.         }  
  22.     }  
  23.       
  24.     public static void close(Connection conn, PreparedStatement pst, ResultSet res) throws SQLException {  
  25.         if(res != null) {  
  26.             res.close();  
  27.         }  
  28.         if(pst != null) {  
  29.             pst.close();  
  30.         }  
  31.         if(conn != null) {  
  32.             conn.close();  
  33.         }  
  34.     }  
  35.       
  36.     public static void executeUpdate(String sql, Object[] params) throws SQLException {  
  37.         Connection conn = null;  
  38.         PreparedStatement pst = null;  
  39.           
  40.         try {  
  41.             conn = DataBaseUtils.getConnection();  
  42.               
  43.             pst = conn.prepareStatement(sql);  
  44.               
  45.             for(int i=0;i<params.length;i++) {  
  46.                 pst.setObject(i + 1, params[i]);  
  47.             }             
  48.             pst.executeUpdate();  
  49.               
  50.             System.out.println(sql);  
  51.         } finally {  
  52.             DataBaseUtils.close(conn, pst, null);  
  53.         }  
  54.     }  
  55. }  

 index.jsp  

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'left.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.      
  25.   <body>  
  26.       <a href="ClassifyAction!listToMenuId?menuId=11000">三峡简介</a><br>  
  27.       <a href="ClassifyAction!listToMenuId?menuId=21000">三峡文化</a><br>  
  28.       <a href="ClassifyAction!listToMenuId?menuId=31000">三峡精神</a><br>  
  29.       <a href="ClassifyAction!listToMenuId?menuId=41000">三峡景点</a><br>  
  30.   </body>  
  31. </html>  

update.jsp

 

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'index.jsp' starting page</title>  
  13.     <meta http-equiv="pragma" content="no-cache">  
  14.     <meta http-equiv="cache-control" content="no-cache">  
  15.     <meta http-equiv="expires" content="0">      
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.     <meta http-equiv="description" content="This is my page">  
  18.     <!-- 
  19.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  20.     -->  
  21.     <script type="text/javascript" src="ckeditor/ckeditor.js"></script>  
  22.     <script type="text/javascript" src="ckfinder/ckfinder.js"></script>  
  23.       
  24.   </head>  
  25.     
  26.   <body>  
  27.     
  28.      <form  id="form1" name="form1" action="ClassifyAction!updateClassify" method="post" >   
  29.             <input type="hidden" name="classify.classifyId"  value="${classify.classifyId }"/>   
  30.             <input type="hidden" name="classify.classifyTitle" value="${classify.classifyTitle }" />  
  31.             <input type="hidden" name="menuId" value="${classify.menu.menuId }" />  
  32.             <table width="650" height="450" border="0" align="center">                 
  33.                 <tr>    
  34.                     <td valign="top">    
  35.                         内容:    
  36.                     </td>    
  37.                     <td>    
  38.                         <textarea id="editor1" name="editor1" class="ckeditor"><p>${classify.classifyContent}. </p></textarea>    
  39.                         <script type="text/javascript">    
  40.                             CKEDITOR.replace( 'editor1' );    
  41.                         </script>    
  42.                     </td>    
  43.                 </tr>    
  44.                 <tr>    
  45.                     <td></td>    
  46.                     <td>    
  47.                         <input type="submit" name="Submit" value="提交" />    
  48.                         <input type="reset" name="Reset" value="重置" />    
  49.                     </td>    
  50.                 </tr>    
  51.             </table>    
  52.         </form>    
  53.   </body>  
  54. </html>  

第五步:

          因为struts2拦截器会拦截所有以"/"开头的URL,CKEd的servlet拦截器拦截所有以"
/ckfinder/core/connector/java/connector.java"开头的URL.当时CKEditor上传图片的时 候,URL中包含有"/"(指网站根目录),优先被struts2的拦截器拦截,这样上传的URL请求就不会转发到CKEditor的servlet中, 所有上传图片也就不会成功.
 
   解决步骤:
  
第一个办法(最简单):是将struts2的拦截器"/*"改成"#.action"或者"*.do",可以这样改
  1.     
  2. <filter-mapping>    
  3.         <filter-name>struts2</filter-name>    
  4.         <url-pattern>*.action</url-pattern>    
  5. </filter-mapping>    
 
第二个办法是:
自己来定义一个CKeditor的过滤器,让它来继承Struts2的过滤器FilterDispatcher,其完整代码是
  1. package com.orange.web.filter;  
  2. import java.io.IOException;  
  3. import javax.servlet.FilterChain;  
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.ServletRequest;  
  6. import javax.servlet.ServletResponse;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import org.apache.struts2.dispatcher.FilterDispatcher;  
  9.   
  10. @SuppressWarnings("deprecation")    
  11. public class FCKFilter extends FilterDispatcher {    
  12.      public void doFilter(ServletRequest req,ServletResponse res, FilterChain chain) throws IOException, ServletException {       
  13.             HttpServletRequest request = (HttpServletRequest) req;      
  14.             String URI = request.getRequestURI();     
  15.             String[] uriArray = URI.split("/ckfinder/core/connector/java/*/");     
  16.             int arrayLen = uriArray.length;     
  17.             if (arrayLen >= 2) {     
  18.                 chain.doFilter(req, res);      
  19.             }else {     
  20.                 super.doFilter(req, res, chain);       
  21.             }     
  22.         }      
  23.  }   
第六步:运行测试