cms-写帖子内容实现

写帖子后台:

mapper:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.open1111.dao.ArticleDao">

<resultMap type="Article" id="ArticleResult">
<result property="id" column="id"/>
<result property="title" column="title"/>
<result property="publishDate" column="publishDate"/>
<result property="content" column="content"/>
<result property="summary" column="summary"/>
<result property="titleColor" column="titleColor"/>
<result property="click" column="click"/>
<result property="isRecommend" column="isRecommend"/>
<result property="isSlide" column="isSlide"/>
<result property="keyWords" column="keyWords"/>

<association property="arcType" column="typeId" select="com.open1111.dao.ArcTypeDao.findById"></association>
</resultMap>

<select id="getNewest" resultMap="ArticleResult">
select * from t_article order by publishDate desc limit 0,7
</select>

<select id="getRecommend" resultMap="ArticleResult">
select * from t_article where isRecommend=1 order by publishDate desc limit 0,7
</select>

<select id="getSlide" resultMap="ArticleResult">
select * from t_article where isSlide=1 order by publishDate desc limit 0,5
</select>

<select id="getIndex" parameterType="Integer" resultMap="ArticleResult">
select * from t_article where typeId=#{typeId} order by publishDate desc limit 0,8
</select>

<select id="findById" parameterType="Integer" resultMap="ArticleResult">
select * from t_article where id=#{id}
</select>

<select id="getLastArticle" parameterType="Integer" resultMap="ArticleResult">
SELECT * FROM t_article WHERE id &lt; #{id} ORDER BY id DESC LIMIT 1;
</select>

<select id="getNextArticle" parameterType="Integer" resultMap="ArticleResult">
SELECT * FROM t_article WHERE id &gt; #{id} ORDER BY id ASC LIMIT 1;
</select>

<update id="update" parameterType="Article">
update t_article
<set>
<if test="click!=0">
click=#{click},
</if>
</set>
where id=#{id}
</update>

<select id="list" parameterType="Map" resultMap="ArticleResult">
select * from t_article
<where>
<if test="typeId!=null">
and typeId=#{typeId}
</if>
</where>
order by publishDate desc
<if test="start!=null and size!=null">
limit #{start},#{size}
</if>
</select>

<select id="getTotal" parameterType="Map" resultType="Long">
select count(*) from t_article
<where>
<if test="typeId!=null">
and typeId=#{typeId}
</if>
</where>
</select>

<insert id="add" parameterType="Article">
insert into t_article values(null,#{title},#{publishDate},#{content},#{summary},#{titleColor},#{click},#{isRecommend},#{isSlide},#{slideImage},#{arcType.id},#{keyWords})
</insert>

</mapper>

dao:

package com.open1111.dao;

import java.util.List;
import java.util.Map;

import com.open1111.entity.Article;

/**
* 帖子Dao接口
* @author user
*
*/
public interface ArticleDao {

/**
* 获取最新的7条帖子
* @return
*/
public List<Article> getNewest();

/**
* 获取最新7条推荐的帖子
* @return
*/
public List<Article> getRecommend();

/**
* 获取最新5条幻灯的帖子
* @return
*/
public List<Article> getSlide();

/**
* 根据帖子类别来查找最新的8条数据
* @param typeId
* @return
*/
public List<Article> getIndex(Integer typeId);

/**
* 通过id查询帖子
* @param id
* @return
*/
public Article findById(Integer id);

/**
* 获取上一个帖子
* @param id
* @return
*/
public Article getLastArticle(Integer id);

/**
* 获取下一个帖子
* @param id
* @return
*/
public Article getNextArticle(Integer id);

/**
* 更新帖子
* @param article
* @return
*/
public Integer update(Article article);

/**
* 根据条件分页查询帖子
* @param map
* @return
*/
public List<Article> list(Map<String,Object> map);

/**
* 获取总记录数
* @param map
* @return
*/
public Long getTotal(Map<String,Object> map);

/**
* 添加帖子
* @param article
* @return
*/
public Integer add(Article article);
}

servcieImpl:

package com.open1111.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.open1111.dao.ArticleDao;
import com.open1111.entity.Article;
import com.open1111.service.ArticleService;

/**
* 帖子Service实现类
* @author user
*
*/
@Service("articleService")
public class ArticleServiceImpl implements ArticleService{

@Resource
private ArticleDao articleDao;

public List<Article> getNewest() {
return articleDao.getNewest();
}

public List<Article> getRecommend() {
return articleDao.getRecommend();
}

public List<Article> getSlide() {
return articleDao.getSlide();
}

public List<Article> getIndex(Integer typeId) {
return articleDao.getIndex(typeId);
}

public Article findById(Integer id) {
return articleDao.findById(id);
}

public Article getLastArticle(Integer id) {
return articleDao.getLastArticle(id);
}

public Article getNextArticle(Integer id) {
return articleDao.getNextArticle(id);
}

public Integer update(Article article) {
return articleDao.update(article);
}

public List<Article> list(Map<String, Object> map) {
return articleDao.list(map);
}

public Long getTotal(Map<String, Object> map) {
return articleDao.getTotal(map);
}

public Integer add(Article article) {
return articleDao.add(article);
}

}

servcie:

package com.open1111.service;

import java.util.List;
import java.util.Map;

import com.open1111.entity.Article;

/**
* 帖子Service接口
* @author user
*
*/
public interface ArticleService {

/**
* 获取最新的7条帖子
* @return
*/
public List<Article> getNewest();

/**
* 获取最新7条推荐的帖子
* @return
*/
public List<Article> getRecommend();

/**
* 获取最新5条幻灯的帖子
* @return
*/
public List<Article> getSlide();

/**
* 根据帖子类别来查找最新的8条数据
* @param typeId
* @return
*/
public List<Article> getIndex(Integer typeId);

/**
* 通过id查询帖子
* @param id
* @return
*/
public Article findById(Integer id);

/**
* 获取上一个帖子
* @param id
* @return
*/
public Article getLastArticle(Integer id);

/**
* 获取下一个帖子
* @param id
* @return
*/
public Article getNextArticle(Integer id);

/**
* 更新帖子
* @param article
* @return
*/
public Integer update(Article article);

/**
* 根据条件分页查询帖子
* @param map
* @return
*/
public List<Article> list(Map<String,Object> map);

/**
* 获取总记录数
* @param map
* @return
*/
public Long getTotal(Map<String,Object> map);

/**
* 添加帖子
* @param article
* @return
*/
public Integer add(Article article);
}

cintroller:

package com.open1111.controller.admin;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.open1111.entity.Article;
import com.open1111.service.ArticleService;
import com.open1111.util.ResponseUtil;

/**
* 帖子后台管理Controller层
* @author user
*
*/
@Controller
@RequestMapping("/admin/article")
public class ArticleAdminController {

@Resource
private ArticleService articleService;

/**
* 添加或者修改帖子信息
* @param article
* @param response
* @return
* @throws Exception
*/
@RequestMapping("/save")
public String save(Article article,HttpServletResponse response)throws Exception{
int resultTotal=0; // 操作的记录条数
article.setPublishDate(new Date());
if(article.getId()==null){ // 添加
resultTotal=articleService.add(article);
}else{ // 修改

}
StringBuffer result=new StringBuffer();
if(resultTotal>0){
result.append("<script language='javascript'>alert('提交成功');</script>");
}else{
result.append("<script language='javascript'>alert('提交失败,请联系管理员');</script>");
}
ResponseUtil.write(response, result);
return null;
}
}

 

后台页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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=UTF-8">
<title>写帖子页面</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/themes/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>

<script type="text/javascript" charset="gbk" src="${pageContext.request.contextPath}/static/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="gbk" src="${pageContext.request.contextPath}/static/ueditor/ueditor.all.min.js"> </script>
<!--建议手动加在语言,避免在ie下有时因为加载语言失败导致编辑器加载失败-->
<!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型,比如你在配置项目里配置的是英文,这里加载的中文,那最后就是中文-->
<script type="text/javascript" charset="gbk" src="${pageContext.request.contextPath}/static/ueditor/lang/zh-cn/zh-cn.js"></script>

<script type="text/javascript" src="${pageContext.request.contextPath}/static/colorPicker/js/jquery.bigcolorpicker.js"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/colorPicker/css/jquery.bigcolorpicker.css" type="text/css" />
<script type="text/javascript">

function checkChange(){
if(document.getElementById("isSlide").checked){
$("#hdtp").show();
}else{
$("#hdtp").hide();
}
}

function submitData(){
var title=$("#title").val();
var arcTypeId=$("#arcTypeId").combobox("getValue");
var summary=$("#summary").val();
var editor=UE.getEditor('editor').getContent();

if(title==null || title==''){
alert("请输入标题");
}else if(arcTypeId==null || arcTypeId==''){
alert("请选择帖子类别");
}else if(summary==null || summary==''){
alert("请输入摘要");
}else if(editor==null || editor==''){
alert("请输入内容");
}else{
$("#content").val(editor);
$("#fm").submit();
}


}

</script>
</head>
<body style="margin: 1px">
<div id="p" class="easyui-panel" title="编写帖子" style="padding: 5px">
<form id="fm" action="${pageContext.request.contextPath}/admin/article/save.do" method="post" enctype="multipart/form-data">
<table cellspacing="10px">
<tr>
<td width="80px">帖子标题:</td>
<td><input type="text" id="title" name="title" style="width: 400px"/></td>
</tr>
<tr>
<td>所属类别:</td>
<td>
<select class="easyui-combobox" style="width: 154px" id="arcTypeId" name="arcType.id" editable="false" panelHeight="auto">
<option value="">请选择帖子类别...</option>
<c:forEach var="arcType" items="${arcTypeList }">
<option value="${arcType.id }">${arcType.typeName }</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>帖子属性:</td>
<td>
<input type="checkbox" id="isSlide" name="isSlide" onclick="checkChange()" value="1"/>幻灯帖子
<input type="checkbox" id="isRecommend" name="isRecommend" value="1"/>推荐帖子
</td>
</tr>
<tr id="hdtp" style="display: none;">
<td>幻灯图片:</td>
<td>
<input type="file" id="slideImageFile" name="slideImageFile"/>
</td>
</tr>
<tr>
<td valign="top">帖子摘要:</td>
<td>
<textarea rows="3" cols="40" id="summary" name="summary"></textarea>
</td>
</tr>
<tr>
<td valign="top">帖子内容:</td>
<td>
<script id="editor" type="text/plain" style="width:1000px;height:500px;"></script>
<input type="hidden" id="content" name="content"/>
</td>
</tr>
<tr>
<td>关键字:</td>
<td>
<input type="text" id="keyWords" name="keyWords" style="width: 300px"/>&nbsp;(多个关键字中间用空格隔开)
</td>
</tr>
<tr>
<td>标题颜色:</td>
<td>
<input type="text" id=titleColor name="titleColor"/><input id="bn" type="button" value="颜色"/>
</td>
</tr>
<tr>
<td></td>
<td>
<a href="javascript:submitData()" class="easyui-linkbutton" data-options="iconCls:'icon-submit'">发布帖子</a>
</td>
</tr>
</table>
</form>
</div>

<script type="text/javascript">
//实例化编辑器
//建议使用工厂方法getEditor创建和引用编辑器实例,如果在某个闭包下引用该编辑器,直接调用UE.getEditor('editor')就能拿到相关的实例
var ue = UE.getEditor('editor');

// 实例化colorPicker
$("#bn").bigColorpicker("titleColor");
</script>
</body>
</html>

 

posted @ 2017-04-06 21:56  小拽A  阅读(318)  评论(0编辑  收藏  举报