基于javaweb+mysql的新闻发布管理系统
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
后台:管理新闻类型、新闻、评论、链接
前台:看新闻、评论
后台
前台
技术框架
JSP Servlet MySQL JDBC Tomcat jQuery Bootstrap JavaScript
<p>Quisque iaculis, dui lectus varius vitae, tortor. Proin lacus. Pellentesque ac lacus. Aenean nonummy commodo nec, pede. Etiam blandit risus elit.</p>
<p>Ut pretium. Vestibulum rutrum in, adipiscing elit. Sed in quam in purus sem vitae pede. Pellentesque bibendum, urna sem vel risus. Vivamus posuere metus. Aliquam gravida iaculis nisl. Nam enim. Aliquam erat ac lacus tellus ac felis.</p>
<div style="border: 2px dashed green; background: #ddd; text-align: center;">
<p>This text is wrapped in a&nbsp;<tt>DIV</tt>&nbsp;element. We can type after this element though.</p>
</div>
</textarea>
<script>
// This call can be placed at any point after the
// <textarea>, or inside a <head><script> in a
// window.onload event handler.
CKEDITOR.replace( 'editor2', {
extraPlugins: 'magicline', // Ensure that magicline plugin, which is required for this sample, is loaded.
magicline_color: 'blue', // Blue line
allowedContent: true // Switch off the ACF, so very complex content created to
// show magicline's power isn't filtered.
});
</script>
</div>
<div id="footer">
<hr>
CKEDITOR.replace( 'editor2', {
allowedContent:
'h1 h2 h3 p blockquote strong em;' +
'a[!href];' +
'img(left,right)[!src,alt,width,height];' +
'table tr th td caption;' +
'span{!font-family};' +'
'span{!color};' +
'span(!marker);' +
'del ins'
} );
</pre>
<p>
The following rules may require additional explanation:
</p>
<ul>
<li>
<code>h1 h2 h3 p blockquote strong em</code> - These tags
are accepted by the editor. Any tag attributes will be discarded.
</li>
#columns #column3 > div
{
margin-right: 1px;
}
#columns > div > div
{
margin: 0px 10px;
padding: 10px 20px;
}
#columns blockquote
{
margin-left: 15px;
}
#tagLine
{
border-top: 5px solid #05B2D2;
padding-top: 20px;
}
#taglist {
display: inline-block;
margin-left: 20px;
}
}
package com.demo.web;
import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.demo.dao.LinkDao;
import com.demo.dao.NewsDao;
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
String realPath = request.getServletContext().getRealPath("");
System.out.println("图片目录:" + realPath);
System.out.println("图片目录:" + realPath);
System.out.println("图片目录:" + realPath);
System.out.println("图片目录:" + realPath);
System.out.println("图片目录:" + realPath);
try {
import java.io.IOException;
import java.sql.Connection;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import com.demo.dao.NewsDao;
import com.demo.dao.NewsTypeDao;
import com.demo.model.NewsType;
import com.demo.util.DbUtil;
config.allowedContent</a></code> to <code>true</code> like this:
</p>
<pre class="samples">
CKEDITOR.replace( <em>textarea_id</em>, {
<strong>allowedContent: true</strong>
} );
</pre>
<h2>Beyond data flow: Features activation</h2>
<p>
<abbr title="Advanced Content Filter">ACF</abbr> is far more than
<abbr title="Input/Output">I/O</abbr> control: the entire
<abbr title="User Interface">UI</abbr> of the editor is adjusted to what
filters restrict. For example: if <code><a></code> tag is
<strong>disallowed</strong>
by <abbr title="Advanced Content Filter">ACF</abbr>,
then accordingly <code>link</code> command, toolbar button and link dialog
are also disabled. Editor is smart: it knows which features must be
removed from the interface to match filtering rules.
</p>
<p>
CKEditor can be far more specific. If <code><a></code> tag is
<strong>allowed</strong> by filtering rules to be used but it is restricted
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
public class ImageUploadServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
// Trying to insert disallowed tag and attribute.
editor.setData( '<p <strong>style="color: red"</strong>>Hello <strong><em>world</em></strong>!</p>' );
alert( editor.getData() );
// Filtered data is returned.
"<p>Hello world!</p>"
</pre>
<p>
What happened? Since <code>config.allowedContent: 'p'</code> is set the editor assumes
that only plain <code><p></code> are accepted. Nothing more. This is why
<code>style</code> attribute and <code><em></code> tag are gone. The same
filtering would happen if we pasted disallowed HTML into this editor.
</p>
<p>
This is just a small sample of what <abbr title="Advanced Content Filter">ACF</abbr>
can do. To know more, please refer to the sample section below and
attribute is obligatory for <code><img></code> tag.
<code>alt</code>, <code>width</code>, <code>height</code>
and <code>class</code> attributes are accepted but
<code>class</code> must be either <code>class="left"</code>
or <code>class="right"</code>
</li>
<li>
<code>table tr th td caption</code> - These tags
are accepted by the editor. Any tag attributes will be discarded.
</li>
<li>
<code>span{!font-family}</code>, <code>span{!color}</code>,
<code>span(!marker)</code> - <code><span></code> tags
{
padding: 20px;
margin: 0 0 0 30px;
font-size: 1.4em;
color: #666;
}
#columns
{
color: #333;
overflow: hidden;
padding: 20px 0;
}
#columns > div
{
float: left;
width: 33.3%;
}
#columns #column1 > div
{
}
}
public int newsAdd(Connection con,News news)throws Exception{
String sql="insert into t_news values(null,?,?,now(),?,?,0,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, news.getTitle());
pstmt.setString(2, news.getContent());
pstmt.setString(3, news.getAuthor());
pstmt.setInt(4, news.getTypeId());
pstmt.setInt(5, news.getIsHead());
pstmt.setInt(6, news.getIsImage());
pstmt.setString(7, news.getImageName());
pstmt.setInt(8, news.getIsHot());
return pstmt.executeUpdate();
}
public int newsUpdate(Connection con,News news)throws Exception{
String sql="update t_news set title=?,content=?,author=?,typeId=?,isHead=?,isImage=?,imageName=?,isHot=? where newsId=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, news.getTitle());
pstmt.setString(2, news.getContent());
PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(PropertiesUtil.getValue("backPageSize")));
List<Comment> commentBackList=commentDao.commentList(con, new Comment(),pageBean,s_bCommentDate,s_aCommentDate);
request.setAttribute("pageCode", pageCode);
request.setAttribute("commentBackList", commentBackList);
request.setAttribute("navCode", NavUtil.genNewsManageNavigation("新闻评论管理", "新闻评论维护"));
request.setAttribute("mainPage", "/background/comment/commentList.jsp");
request.getRequestDispatcher("/background/mainTemp.jsp").forward(request, response);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void commentDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String commentIds=request.getParameter("commentIds");
Connection con=null;
try{
public class NewsServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
DbUtil dbUtil=new DbUtil();
NewsDao newsDao=new NewsDao();
NewsTypeDao newsTypeDao=new NewsTypeDao();
CommentDao commentDao=new CommentDao();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String action=request.getParameter("action");
if("list".equals(action)){
this.newsList(request, response);
}else if("show".equals(action)){
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void linkPreSave(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String linkId=request.getParameter("linkId");
Connection con=null;
try{
con=dbUtil.getCon();
if(StringUtil.isNotEmpty(linkId)){