文字编辑器的运用

来源:互联网 发布:济源公务员网络培训 编辑:程序博客网 时间:2024/06/05 15:21

第一个页面 index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="BIG5"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>first page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 
 <script type="text/javascript">
    window.open("info.jsp","","width=500,height=400,left=0,top=0,alwaysRaised=yes");
 </script>
  </head>
    <body>
       this is the first page!
    </body>
</html>

第二个页面 info.jsp

<%@ page language="java" contentType="text/html; charset=BIG5" pageEncoding="BIG5" %>
<jsp:useBean id="function" scope="session" class="util.Function" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="charset=BIG5">
<title> 公告信息</title>
  <script>
      function edit()
      {
         window.open("editInfo.jsp","","top=0,left=0,height=600,width=750");
      }
  </script>
</head>
<body>
       <%
         String content = function.getContent();
       %>
              <form name="form1">
                 <table border="0" align="right">
                     <tr>
                        <td><input type="button" value="修改" onclick="edit()"/></td>
                     </tr>
                 </table>
              </form>
     <br>
      <div id="com"><%=content %></div>
</body>
</html>

第三个页面  editInfo.jsp

<%@ page language="java" contentType="text/html; charset=BIG5"
    pageEncoding="BIG5"%>
<%@page import="util.Function"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>公告信息修改</title>
 <script>
      function closeW()
      {
         window.close();
      }
  </script>
</head>
<body>
      公告信息修改
      <br/><br/>
      <input  type="hidden" id="newContent" name="new" maxlength="5000">
      <script type="text/javascript" src="KindEditor.js"></script>
      <script type="text/javascript">
         var content = window.opener.document.getElementById("com");//得到上一个页面id为com元素的值
         window.document.getElementById("newContent").innerText = content.innerHTML;//将得到的值赋给隐藏的input
         window.opener.close();//关闭上一个页面
      
         var editor = new KindEditor("editor");
         editor.hiddenName = "new";
         editor.width = "700px";
         editor.height = "400px";
         editor.show();
         function KindSubmit()
         {
             editor.data();  
             document.getElementById("contentText").value=document.getElementById("newContent").value;  //将编辑后的值赋给下面隐藏的input

         }
      </script>
     <br>
     <form action="endEdit.do">
        <table align="center">
           <tr>
              <td><input type="submit" name="ok" value="提交" onclick="javascript:KindSubmit();"></td>
              <td><input type="button" name="cancle" value="取消" onclick="closeW()"></td>
          </tr>
        </table>
        <br>
        <input type="hidden" name="content" id="contentText" value="test">
     </form>
</body>
</html>
第四:struts的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
  <form-beans>   
  </form-beans>
  <action-mappings>   
      <action path="/endEdit" type="com.java.action.EditAction">
       <forward name="success"  path="/success.jsp" />
     </action>
  </action-mappings>
  <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>

第五:EditAction

package com.java.action;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class EditAction extends Action
{
 public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)
 { 
  String content = request.getParameter("content");
  try
  {
   String contentBIG5 = new String(content.getBytes("ISO8859-1"),"BIG5");//编码方式转换
   util.Function function = new util.Function();
   String result = function.updateNews(contentBIG5 );
   if(result.equals("success"))
   {
    return mapping.findForward("success");
   }
   else
   {
    return mapping.findForward("failed");
   }
  }
  catch (UnsupportedEncodingException e)
  {
   e.printStackTrace();
  }
  return mapping.findForward("fail");
 }
}

 

第六:操作数据库函数

 public String getContent()
   {
    Connection conn = null;
    ResultSet rs = null;
    Statement stat = null;
  try
  {
   GetConnection getconn = new GetConnection();
   conn = getconn.getConnection();//得到数据库的连接,这部分代码省略
      stat = conn.createStatement();
   String sql = "select * from info";
   stat.execute(sql);
   rs = stat.getResultSet();
   while(rs.next())
   {
    Clob clob = rs.getClob("content");
    if(clob!=null)
    {
     String content = clob.getSubString(1, (int)clob.length());
     return content;
    }
    else
    {
     return "content is null";
    }
   }
  }
  catch (SQLException e)
  {
   e.printStackTrace();
  }
  finally
  {
   try
   {
    if(rs!=null){rs.close();}
    if(stat!=null){stat.close();}
    if(conn!=null){conn.close();}
    
   }
   catch (SQLException e)
   {
    e.printStackTrace();
   }
  }
  return "null";
   }
   //更新新聞函數
   public String updateNews(String content)
   {
    Connection conn = null;
    PreparedStatement pstat = null;
    ResultSet rs2 = null;
    try
    {
     GetConnection getConnection = new GetConnection();
     conn = getConnection.getConnection();
     conn.setAutoCommit(false);
     //第一步:插入一個空的ClOB
     String sql1="update INFO set content = EMPTY_CLOB()";
     pstat=conn.prepareStatement(sql1);
     pstat.executeUpdate();
    
           //第二步:取出該CLOB
           String sql2="select content from info for update";
           PreparedStatement ps2=conn.prepareStatement(sql2);
           rs2=ps2.executeQuery();
           while (rs2.next())
           {
      oracle.sql.CLOB clob = (oracle.sql.CLOB) rs2.getClob(1);
      BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
      out.write(content, 0, content.length());
      out.close();
   }
   conn.commit();
   return "success";
    }
    catch (SQLException e)
    {
     e.printStackTrace();
    }
    catch (IOException e)
    {
     e.printStackTrace();
    }
    finally
  {
   try
   {
    if(rs2!=null){rs2.close();}
    if(pstat!=null){pstat.close();}
    if(conn!=null){conn.close();}    
   }
   catch (SQLException e)
   {
    e.printStackTrace();
   }
  }
    return "fail";
   }

原创粉丝点击