最近在學習KindEditor插件,一款蠻不錯的編輯器插件。在此將心得寫出來,希望對大家有所幫助。
最終實現的效果如圖所示:
(1)在管理員頁面manage.jsp點擊”添加新聞“按鈕。
(2)可以從網絡上面複製粘貼一篇文章,也可以自己編寫文章。現在測試圖片從其他網站複製粘貼的情況。
(3)點擊提交,提示提交成功後,進入主頁面index.jsp,發現剛纔編輯的文章已經顯示出來。
(4)這時候點擊標題,可以正常顯示文章來。
(5)除此外還可以實習本地圖片的上傳,文檔的上傳下載等功能。還有對上傳的文章進行修改等功能。
實現步驟:
1、在Eclipse或者Myeclipse中搭建KindEditor環境,可以查照我先前的博客的做法:
http://blog.csdn.net/lhq13400526230/article/details/9256301
2、在Oracle中設計一張表名爲news的數據庫表。之所以有主鍵和業務主鍵,是爲了讓業務和主鍵無關係
3、創建工程,工作的目錄如圖所示:包括控制層(Action)、模型層(model)、接口(Service)、接口的實現類(ServiceImpl)、Spring配置(applicationContext.xml)、Spring的數據庫配置(applicationContext_db.xml)、Spring的依賴注入(applicationContext_bean.xml)、存放上傳圖片、文件的目錄的文件夾attached、存放KindEditor插件的文件夾KindEditor-4.1.7、存放JSP頁面的文件夾JSP、存放新聞主頁的JSP--index.jsp、存放管理員首頁的JSP---manage.jsp等等。
4、web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Sttuts2過濾器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 監聽器Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 定位applicationContext.xml的物理位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
5、applicationContext.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<import resource="applicationContext_bean.xml"/>
<import resource="applicationContext_db.xml"/>
</beans>
6、在news.model下編寫News.java類
package news.model;
import java.sql.Blob;
import java.util.Date;
public class News {
private String id;//主鍵
private String newsid;//新聞主鍵
private String title;//新聞標題
private byte[] content;//新聞內容
private Date times;//新聞發佈時間
private String types;//新聞類型
private String author;//新聞作者
private String department;//新聞發佈部門
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public byte[] getContent() {
return content;
}
public void setContent(byte[] content) {
this.content = content;
}
public String getNewsid() {
return newsid;
}
public void setNewsid(String newsid) {
this.newsid = newsid;
}
public Date getTimes() {
return times;
}
public void setTimes(Date times) {
this.times = times;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTypes() {
return types;
}
public void setTypes(String types) {
this.types = types;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
7、生成對應的映射文件,其中的大部分是使用String類型的,”內容“字段”content“在數據庫中是以BLOB存放,非常的與此不同要注意。
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2013-7-5 15:57:38 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="news.model.News" table="NEWS">
<id name="id" type="java.lang.String">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="newsid" type="java.lang.String">
<column name="NEWSID" />
</property>
<property name="title" type="java.lang.String">
<column name="TITLE" />
</property>
<property name="content" >
<column name="content" />
</property>
<property name="times" type="java.util.Date">
<column name="TIMES" />
</property>
<property name="types" type="java.lang.String">
<column name="TYPES" />
</property>
<property name="author" type="java.lang.String">
<column name="AUTHOR" />
</property>
<property name="department" type="java.lang.String">
<column name="DEPARTMENT" />
</property>
</class>
</hibernate-mapping>
8、在news.service下編輯接口NewsService.java
package news.service;
import java.util.List;
import news.model.News;
public interface NewsService {
public String saveNews(News news,String content) throws Exception;//保存編譯的新聞
public List findNews(String newsid) throws Exception;//顯示新聞內容
public List listNews(String type) throws Exception;//顯示新聞列表
public void updateShowNews(News n) throws Exception;//更新新聞
}
9、編寫對應的接口實現類
package news.serviceImpl;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import news.action.NewsAction;
import news.model.News;
import news.service.NewsService;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
public class NewsServiceImpl implements NewsService{
static Logger log = Logger.getLogger(NewsServiceImpl.class);
private SessionFactory sessionFactory;
//保存數據
public String saveNews(News news,String htmlData) throws Exception {
String uuid=UUID.randomUUID().toString();//UUID產生主鍵
byte content[]=htmlData.getBytes("utf-8");//String類型轉化爲Byte類型
log.info("信息:"+htmlData);
Date date=new Date();//產生時間
DateFormat datefor = new SimpleDateFormat("yyyy-MM-dd-HHmmssFFFF");//時間格式化
String neswid = datefor.format(date);
news.setId(uuid);//設置主鍵
news.setNewsid(neswid);//設置新聞主鍵
news.setTimes(date);//設置時間
news.setContent(content);//設置內容
this.sessionFactory.getCurrentSession().save(news);
return neswid;
}
//修改新聞
public void updateShowNews(News n) throws Exception {
this.sessionFactory.getCurrentSession().update(n);
}
//顯示新聞的標題欄
public List listNews(String type) throws Exception {
String sql = " from News t where t.types=:types ";
Query query=this.sessionFactory.getCurrentSession().createQuery(sql);
query.setParameter("types", type);//新聞編號
return query.list();
}
//根據新聞業務主鍵newsid查詢新聞
public List findNews(String newsid) throws Exception {
String sql = " from News t where t.newsid=:newsid ";
Query query=this.sessionFactory.getCurrentSession().createQuery(sql);
query.setParameter("newsid", newsid);//新聞編號
return query.list();
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
10、編寫Spring與數據庫的連接配置applicationContext_db.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 用Bean定義數據源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- 定義數據庫驅動 -->
<property name="driverClass">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<!-- 定義數據庫URL -->
<property name="jdbcUrl">
<value>jdbc:oracle:thin:@localhost:1521:orcl</value>
</property>
<!-- 定義數據庫的用戶名 -->
<property name="user">
<value>lhq</value>
</property>
<!-- 定義數據庫的密碼 -->
<property name="password">
<value>lhq</value>
</property>
<property name="minPoolSize">
<value>1</value>
</property>
<property name="maxPoolSize">
<value>40</value>
</property>
<property name="maxIdleTime">
<value>1800</value>
</property>
<property name="acquireIncrement">
<value>2</value>
</property>
<property name="maxStatements">
<value>0</value>
</property>
<property name="initialPoolSize">
<value>2</value>
</property>
<property name="idleConnectionTestPeriod">
<value>1800</value>
</property>
<property name="acquireRetryAttempts">
<value>30</value>
</property>
<property name="breakAfterAcquireFailure">
<value>true</value>
</property>
<property name="testConnectionOnCheckout">
<value>false</value>
</property>
</bean>
<!--定義Hibernate的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 定義SessionFactory必須注入dataSource -->
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- 定義Hibernate的SessionFactory屬性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle10gDialect
</prop>
</props>
</property>
<!-- 定義POJO的映射文件 -->
<property name="mappingResources">
<list>
<value>news/model/News.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置事務攔截器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" /><!-- 只有一save、delete、update開頭的方法才能執行增刪改操作 -->
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" /><!-- 其他方法爲只讀方法 -->
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="interceptorPointCuts" expression="execution(* news.serviceImpl..*.*(..))" /> <!-- 對應實現類接口的包的位置 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
</aop:config>
</beans>
11、編寫Spring的依賴注入配置applicationContext_bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 業務層Service -->
<bean id="news_service" class="news.serviceImpl.NewsServiceImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"></ref>
</property>
</bean>
<!-- 控制層Action -->
<bean id="news_action" class="news.action.NewsAction">
<property name="news_services">
<ref bean="news_service" />
</property>
</bean>
</beans>
12、在news.action下編程NewsAction.java
package news.action;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import news.model.News;
import news.service.NewsService;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class NewsAction {
static Logger log = Logger.getLogger(NewsAction.class);
private NewsService news_services;
private News news;
//保存編輯的新聞信息
public String saveNews() throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
String htmlData = request.getParameter("content1");//獲取頁面的數據
news_services.saveNews(news,htmlData);
return "saveNewsSuccess";
}
//獲取要更新新聞的信息
public String updateContent() throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
String htmlData=request.getParameter("content1");
log.info("新聞的主鍵============="+news.getId());
log.info("新聞的內容============="+htmlData);
byte content[]=htmlData.getBytes("utf-8");
news.setContent(content);
news_services.updateShowNews(news);
return "updateContentSuccess";
}
//查找新聞的詳細信息
public String showNews() throws Exception{
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
String newsid=news.getNewsid();//從頁面獲取要查看的新聞ID
List list=news_services.findNews(newsid);
News n=(News) list.get(0);
DateFormat datefor = new SimpleDateFormat("yyyy-MM-dd HH:mm");//時間格式化
String times = datefor.format(n.getTimes());
String CONTENT=new String(n.getContent());//將byte數組轉化爲String類型
String auhtor=n.getAuthor();
String title=n.getTitle();
String types=n.getTypes();
String department=n.getDepartment();
String id=n.getId();
String no=n.getNewsid();
request.setAttribute("id", id);
request.setAttribute("newsid", no);
request.setAttribute("department", department);
request.setAttribute("title", title);
request.setAttribute("types", types);
request.setAttribute("content", CONTENT);
request.setAttribute("times", times);
request.setAttribute("author", auhtor);
return "showNewsSuccess";
}
//分別羅列出各個新聞的列表
public String NewsList() throws Exception{
Map request = (Map) ActionContext.getContext().get("request");//屬於
List list=news_services.listNews("國際新聞");
request.put("list_guoji", list);
List list2=news_services.listNews("國內新聞");
request.put("list_guonei", list2);
List list3=news_services.listNews("體育新聞");
request.put("list_tiyu", list3);
List list4=news_services.listNews("娛樂新聞");
request.put("list_yule", list4);
return "sucess";
}
public NewsService getNews_services() {
return news_services;
}
public void setNews_services(NewsService news_services) {
this.news_services = news_services;
}
public News getNews() {
return news;
}
public void setNews(News news) {
this.news = news;
}
}
13、編程Struts.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.multipart.saveDir" value="/tmp"></constant>
<package name="default" extends="struts-default">
<action name="saveNewsAction" class="news_action" method="saveNews">
<result name="saveNewsSuccess">/jsp/success.jsp</result><!--保存編輯信息信息成功跳轉頁面 -->
</action>
<action name="showNewsAction" class="news_action" method="showNews">
<result name="showNewsSuccess">/jsp/show_news_content.jsp</result><!--讀取新聞信息 -->
</action>
<action name="showNewsListAction" class="news_action" method="NewsList">
<result name="sucess">/jsp/show_news_list.jsp</result><!--讀取新聞列表信息 -->
</action>
<action name="manageNewsListAction" class="news_action" method="NewsList">
<result name="sucess">/jsp/manage_news_list.jsp</result><!--讀取新聞列表信息 -->
</action>
<action name="updateNewsAction" class="news_action" method="showNews">
<result name="showNewsSuccess">/jsp/manage_news_content.jsp</result><!--讀取新聞列表信息 -->
</action>
<action name="updateContentAction" class="news_action" method="updateContent">
<result name="updateContentSuccess">/jsp/updateSuccess.jsp</result><!--修改新聞列表信息 -->
</action>
</package>
</struts>
14、現在進入前臺JSP頁面的編寫,主要的頁面有如下:
15、index.jsp用於新聞網站的首頁
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
request.setCharacterEncoding("UTF-8");
%>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>新聞主頁</title>
</head>
<body >
<div align="center">
<table>
<tr>
<td><h2 align="center">新聞頻道</h2></td>
</tr>
<tr>
<td>
<iframe id="mainframe" src="showNewsListAction.action"
name="mainframe" frameborder="0" scrolling="no" width="800px"
height="100%"> </iframe></td>
</tr>
</table>
</div>
</body>
<script type="text/javascript">
function reinitIframe(){
var iframe = document.getElementById("mainframe");
try{
var bHeight = iframe.contentWindow.document.body.scrollHeight;
var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
var height = Math.max(bHeight, dHeight);
iframe.height = height;
}catch (ex){}
}
window.setInterval("reinitIframe()", 200);
</script>
</html>
16、show_news_list.jsp用於顯示查詢到已經發表的新聞列表。顯示的只是新聞的標題,不是內容。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
request.setCharacterEncoding("UTF-8");
String htmlData = request.getParameter("content1") != null ? request
.getParameter("content1") : "";
%>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>新聞顯示列表頁面</title>
</head>
<body>
<div style="float:right;width:350px;height:200px;">
<table>
<tr>
<td>
<h3>國際新聞</h3>
</td>
</tr>
<s:iterator value="#request.list_guoji" id="news1">
<tr>
<td><a
href="showNewsAction.action?news.newsid=<s:property value="#news1.newsid"/>">
<s:property value="#news1.title" />
</a></td>
</tr>
</s:iterator>
</table>
</div>
<div style="float:left;width:350px;height:200px;">
<table>
<tr>
<td>
<h3>國內新聞</h3>
</td>
</tr>
<s:iterator value="#request.list_guonei" id="news2">
<tr>
<td><a
href="showNewsAction.action?news.newsid=<s:property value="#news2.newsid"/>">
<s:property value="#news2.title" />
</a></td>
</tr>
</s:iterator>
</table>
</div>
<div style="float:right;width:350px;height:200px;">
<table>
<tr>
<td>
<h3>體育新聞</h3>
</td>
</tr>
<s:iterator value="#request.list_tiyu" id="news3">
<tr>
<td><a
href="showNewsAction.action?news.newsid=<s:property value="#news3.newsid"/>">
<s:property value="#news3.title" />
</a></td>
</tr>
</s:iterator>
</table>
</div>
<div style="float:left;width:350px;height:200px;">
<table>
<tr>
<td>
<h3>娛樂新聞</h3>
</td>
</tr>
<s:iterator value="#request.list_yule" id="news4">
<tr>
<td><a
href="showNewsAction.action?news.newsid=<s:property value="#news4.newsid"/>">
<s:property value="#news4.title" />
</a></td>
</tr>
</s:iterator>
</table>
</div>
</body>
</html>
17、show_news_content.jsp顯示具體的新聞內容
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
request.setCharacterEncoding("UTF-8");
%>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>新聞內容</title>
</head>
<body>
<div align="center">
<div>
<table>
<tr>
<td></td>
</tr>
<tr>
<td>發佈時間:<%=request.getAttribute("times")%></td>
<td>作者:<%=request.getAttribute("author")%></td>
</tr>
</table>
</div>
<hr>
<div>
<%=request.getAttribute("content")%>
</div>
</div>
</body>
</html>
18、manage.jsp管理員的主頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
request.setCharacterEncoding("UTF-8");
%>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>管理員頁面</title>
</head>
<body >
<div align="center">
<h2 align="center">管理員操作</h2>
<table>
<tr>
<td><a href="<%=path %>/jsp/add.jsp">添加新聞</a></td>
</tr>
<br>
<tr>
<td><a href="<%=path %>/update.jsp">修改新聞</a></td>
</tr>
</table>
</div>
</body>
</html>
19、add.jsp管理員用來添加新聞的頁面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
request.setCharacterEncoding("UTF-8");
String htmlData = request.getParameter("content1") != null ? request.getParameter("content1") : "";
%>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>KindEditor JSP</title>
<link rel="stylesheet" href="<%=path%>/kindeditor-4.1.7/themes/default/default.css" />
<link rel="stylesheet" href="<%=path%>/kindeditor-4.1.7/plugins/code/prettify.css" />
<script charset="utf-8" src="<%=path%>/kindeditor-4.1.7/kindeditor.js"></script>
<script charset="utf-8" src="<%=path%>/kindeditor-4.1.7/lang/zh_CN.js"></script>
<script charset="utf-8" src="<%=path%>/kindeditor-4.1.7/plugins/code/prettify.js"></script>
<script>
KindEditor.ready(function(K) {
var editor1 = K.create('textarea[name="content1"]', {
cssPath : 'kindeditor-4.1.7/plugins/code/prettify.css',
uploadJson : 'kindeditor-4.1.7/jsp/upload_json.jsp',
fileManagerJson : 'kindeditor-4.1.7/jsp/file_manager_json.jsp',
allowFileManager : true,
afterCreate : function() {
var self = this;
K.ctrl(document, 13, function() {
self.sync();
document.forms['example'].submit();
});
K.ctrl(self.edit.doc, 13, function() {
self.sync();
document.forms['example'].submit();
});
}
});
prettyPrint();
});
</script>
</head>
<body>
<div align="center">
<form name="example" method="post" action="saveNewsAction.action">
<h4>文章編輯</h4>
<table>
<tr>
<td>標題</td>
<td><input type="text" name="news.title" /></td>
<td>作者</td>
<td><input type="text" name="news.author" /></td>
<td>類型</td>
<td>
<select name="news.types">
<option>國內新聞</option>
<option>國際新聞</option>
<option>體育新聞</option>
<option>娛樂新聞</option>
</select>
</td>
<td>發佈單位</td>
<td>
<select name="news.department">
<option>辦公室</option>
<option>宣傳部</option>
<option>文藝團</option>
<option>工會</option>
</select>
</td>
</tr>
</table>
<textarea name="content1" cols="100" rows="8" style="width:800px;height:450px;visibility:hidden;margin:0px 100px 0px 100px;"><%=htmlspecialchars(htmlData)%></textarea>
<br />
<input type="submit" name="button" value="提交內容" /> (提交快捷鍵: Ctrl + Enter)
</form>
</div>
</body>
</html>
<%!
private String htmlspecialchars(String str) {
str = str.replaceAll("&", "&");
str = str.replaceAll("<", "<");
str = str.replaceAll(">", ">");
str = str.replaceAll("\"", """);
return str;
}
%>
20、提示新聞添加成功的頁面success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
%>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>成功頁面</title>
</head>
<body>
發佈成功!!
</body>
</html>
21、update.jsp更新新聞的頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
request.setCharacterEncoding("UTF-8");
%>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>管理員頁面</title>
</head>
<body >
<div align="center">
<table>
<tr>
<td><h2 align="center">管理員操作</h2></td>
</tr>
<tr>
<td>
<iframe id="mainframe" src="manageNewsListAction.action"
name="mainframe" frameborder="0" scrolling="no" width="800px"
height="100%"> </iframe></td>
</tr>
</table>
</div>
</body>
<script type="text/javascript">
function reinitIframe(){
var iframe = document.getElementById("mainframe");
try{
var bHeight = iframe.contentWindow.document.body.scrollHeight;
var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
var height = Math.max(bHeight, dHeight);
iframe.height = height;
}catch (ex){}
}
window.setInterval("reinitIframe()", 200);
</script>
</html>
22、manage_news_list.jsp顯示可以更新的新聞列表
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
request.setCharacterEncoding("UTF-8");
String htmlData = request.getParameter("content1") != null ? request
.getParameter("content1") : "";
%>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>新聞管理頁面</title>
</head>
<body>
<div style="float:right;width:350px;height:200px;">
<table>
<tr>
<td>
<h3>國際新聞</h3>
</td>
</tr>
<s:iterator value="#request.list_guoji" id="news1">
<tr>
<td><a
href="showNewsAction.action?news.newsid=<s:property value="#news1.newsid"/>">
<s:property value="#news1.title" />
</a></td>
<td><a
href="updateNewsAction.action?news.newsid=<s:property value="#news1.newsid"/>">
修改新聞
</a></td>
</tr>
</s:iterator>
</table>
</div>
<div style="float:left;width:350px;height:200px;">
<table>
<tr>
<td>
<h3>國內新聞</h3>
</td>
</tr>
<s:iterator value="#request.list_guonei" id="news2">
<tr>
<td><a
href="showNewsAction.action?news.newsid=<s:property value="#news2.newsid"/>">
<s:property value="#news2.title" />
</a></td>
<td><a
href="updateNewsAction.action?news.newsid=<s:property value="#news2.newsid"/>">
修改新聞
</a></td>
</tr>
</s:iterator>
</table>
</div>
<div style="float:right;width:350px;height:200px;">
<table>
<tr>
<td>
<h3>體育新聞</h3>
</td>
</tr>
<s:iterator value="#request.list_tiyu" id="news3">
<tr>
<td><a
href="showNewsAction.action?news.newsid=<s:property value="#news3.newsid"/>">
<s:property value="#news3.title" />
</a></td>
<td><a
href="updateNewsAction.action?news.newsid=<s:property value="#news3.newsid"/>">
修改新聞
</a></td>
</tr>
</s:iterator>
</table>
</div>
<div style="float:left;width:350px;height:200px;">
<table>
<tr>
<td>
<h3>娛樂新聞</h3>
</td>
</tr>
<s:iterator value="#request.list_yule" id="news4">
<tr>
<td><a
href="showNewsAction.action?news.newsid=<s:property value="#news4.newsid"/>">
<s:property value="#news4.title" />
</a></td>
<td><a
href="updateNewsAction.action?news.newsid=<s:property value="#news4.newsid"/>">
修改新聞
</a></td>
</tr>
</s:iterator>
</table>
</div>
</body>
</html>
20、manage_news_content.jsp具體修改新聞的頁面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
request.setCharacterEncoding("UTF-8");
String htmlData = request.getParameter("content1") != null ? request.getParameter("content1") : "";
%>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>KindEditor JSP</title>
<link rel="stylesheet" href="<%=path%>/kindeditor-4.1.7/themes/default/default.css" />
<link rel="stylesheet" href="<%=path%>/kindeditor-4.1.7/plugins/code/prettify.css" />
<script charset="utf-8" src="<%=path%>/kindeditor-4.1.7/kindeditor.js"></script>
<script charset="utf-8" src="<%=path%>/kindeditor-4.1.7/lang/zh_CN.js"></script>
<script charset="utf-8" src="<%=path%>/kindeditor-4.1.7/plugins/code/prettify.js"></script>
<script>
KindEditor.ready(function(K) {
var editor1 = K.create('textarea[name="content1"]', {
cssPath : 'kindeditor-4.1.7/plugins/code/prettify.css',
uploadJson : 'kindeditor-4.1.7/jsp/upload_json.jsp',
fileManagerJson : 'kindeditor-4.1.7/jsp/file_manager_json.jsp',
allowFileManager : true,
afterCreate : function() {
var self = this;
K.ctrl(document, 13, function() {
self.sync();
document.forms['example'].submit();
});
K.ctrl(self.edit.doc, 13, function() {
self.sync();
document.forms['example'].submit();
});
}
});
prettyPrint();
});
</script>
</head>
<body>
<div align="center">
<form name="example" method="post" action="updateContentAction.action">
<h4>文章修改</h4>
<table>
<tr style="display: none;">
<td><input type="text" name="news.id" value="<%=request.getAttribute("id")%>" /></td>
<td><input type="text" name="news.newsid" value="<%=request.getAttribute("newsid")%>" /></td>
<td><input type="text" name="news.times" value="<%=request.getAttribute("times")%>" /></td>
</tr>
<tr>
<td>標題</td>
<td><input type="text" name="news.title" value="<%=request.getAttribute("title")%>" /></td>
<td>作者</td>
<td><input type="text" name="news.author" value="<%=request.getAttribute("author")%>" /></td>
<td>類型</td>
<td>
<select name="news.types">
<option ><%=request.getAttribute("types")%></option>
<option >國內新聞</option>
<option >國際新聞</option>
<option >體育新聞</option>
<option >娛樂新聞</option>
</select>
</td>
<td>發佈單位</td>
<td>
<select name="news.department" >
<option><%=request.getAttribute("department")%></option>
<option>辦公室</option>
<option>宣傳部</option>
<option>文藝團</option>
<option>工會</option>
</select>
</td>
</tr>
</table>
<textarea name="content1" cols="100" rows="8" style="width:800px;height:450px;visibility:hidden;margin:0px 100px 0px 100px;"><%=request.getAttribute("content")%></textarea>
<br />
<input type="submit" name="button" value="提交內容" /> (提交快捷鍵: Ctrl + Enter)
</form>
</div>
</body>
</html>
21、updateSuccess.jsp提示新聞修改成功的頁面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
%>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>成功頁面</title>
</head>
<body>
修改成功!!
</body>
</html>
22、啓動程序,輸入相應的網址進行測試
管理員的網址:http://localhost:8080/news/manage.jsp
普通用戶的網址:http://localhost:8080/news/index.jsp