文章目录
新闻管理界面小练习
发布日期为系统当前时间,所有的新闻内容要求使用在线编辑器进行编辑
要求:
1.添加新闻:添加新闻时写清楚新闻的标题、作者、内容,默认情况下刚添加的新闻要经过管理员的审核,新闻发布时间为新闻的提交时间
注意:发布新闻的作者就是当前正在登陆的人员。所有管理员发布的新闻可以直接发布,就是管理发布的新闻的lockflag=1.所有的普通用户发布的新闻必须是lockflag=0.
2.修改新闻:将已有的信息进行显示,如果是管理员修改新闻,则可以控制新闻的锁定状态
普通用户用户只能查看到所有的已通过的新闻。普通的用户只能查看新闻以及修改和删除自己的新闻。新闻的内容不能显示的页面上,想要查看新闻的内容,点击新闻的标题跳转的一个单独的页面进行查看。
如果是管理员查看新闻的时候,要显示新闻的编号,新闻的标题,新闻的作者,新闻的发布时间以及操作,在操作中,如果是没有通过的新闻,则在操作中显示通过/删除
如果是通过的新闻,需要判断是否是自己发布的,如果是自己发布的,可以修改可以删除,如果是别人发布的只能删除。
查看新闻的时候要求按照新闻的发布时间降序排序
数据库
t_user1表:
/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Version : 50519
Source Host : localhost:3306
Source Database : test
Target Server Type : MYSQL
Target Server Version : 50519
File Encoding : 65001
Date: 2020-11-18 19:00:01
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for t_user1
-- ----------------------------
DROP TABLE IF EXISTS `t_user1`;
CREATE TABLE `t_user1` (
`userid` varchar(16) NOT NULL,
`password` varchar(16) NOT NULL,
`username` varchar(16) NOT NULL,
`flag` int(2) NOT NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_user1
-- ----------------------------
INSERT INTO `t_user1` VALUES ('13878978888', '123456', '张三', '1');
INSERT INTO `t_user1` VALUES ('13878978999', '123456', 'admin', '0');
news表:
/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Version : 50519
Source Host : localhost:3306
Source Database : test
Target Server Type : MYSQL
Target Server Version : 50519
File Encoding : 65001
Date: 2020-11-18 19:00:20
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for news
-- ----------------------------
DROP TABLE IF EXISTS `news`;
CREATE TABLE `news` (
`nid` int(3) NOT NULL AUTO_INCREMENT,
`title` varchar(100) NOT NULL,
`author` varchar(16) NOT NULL,
`pubdate` date NOT NULL,
`content` varchar(1000) NOT NULL,
`lockflag` int(2) NOT NULL,
PRIMARY KEY (`nid`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of news
-- ----------------------------
INSERT INTO `news` VALUES ('1', 'title1', 'admin', '2020-11-16', 'hello1', '0');
INSERT INTO `news` VALUES ('2', 'title2', 'admin', '2020-11-16', 'hello2', '0');
INSERT INTO `news` VALUES ('3', 'title3', '张三', '2020-11-16', 'hello3', '0');
INSERT INTO `news` VALUES ('4', 'title4', 'admin', '2020-11-16', 'hello4', '0');
INSERT INTO `news` VALUES ('5', 'title5', '张三', '2020-11-16', 'hello5', '0');
INSERT INTO `news` VALUES ('6', 'title6', 'admin', '2020-11-16', 'hello6', '0');
INSERT INTO `news` VALUES ('8', 'title8', '张三', '2020-11-16', 'hello8', '0');
INSERT INTO `news` VALUES ('9', 'title9', '张三', '2020-11-16', 'hello1', '0');
INSERT INTO `news` VALUES ('10', 'title10', 'admin', '2020-11-16', 'hello1', '0');
INSERT INTO `news` VALUES ('11', '你好', 'admin', '2020-11-16', 'hello', '0');
INSERT INTO `news` VALUES ('12', '你好1', '张三', '2020-11-16', 'nihao1', '0');
INSERT INTO `news` VALUES ('13', '11', 'admin', '2020-11-17', 'gg', '0');
INSERT INTO `news` VALUES ('14', '112', 'admin', '2020-11-17', 'qwertghj', '0');
INSERT INTO `news` VALUES ('15', 'hello12', '张三', '2020-11-18', 'hello13', '0');
基于EL表达式和JSTL
addOrUpdate.jsp:
<%@page import="com.oracle.vo.News"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
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="${requestScope.basePath}">
<title>My JSP 'add.jsp' starting 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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function $(id){
return document.getElementById(id);
}
window.onload = function(){
var title = $("title");
var content = $("content");
var sp1 = $("sp1");
var btn_ok=$("btn_ok");
var btn_cancel = $("btn_cancel");
//对提交按钮设置点击事件
if(btn_ok!=null&&btn_ok!=undefined){
btn_ok.onclick = function(){
//定义一个变量代表是否提交
var flag=true;
//获取title和content的value
var titleValue = title.value;
var contentValue = content.value;
//判断值是否为空
if(titleValue==null||titleValue==""){
sp1.innerHTML="<font color='red'>title不能为空</font>"
flag=false;
return;
}else{
sp1.innerHTML=""
flag=true;
}
if(contentValue==null||contentValue==""){
sp1.innerHTML="<font color='red'>content不能为空</font>"
flag=false;
return;
}else{
sp1.innerHTML=""
flag=true;
}
//判断是否能够提交
if(flag==true){
var f1 = $("f1");
f1.submit();
}
}
}
//对取消按钮设置点击事件
btn_cancel.onclick = function(){
var f2 = $("f2");
f2.submit();
}
}
</script>
</head>
<body>
<c:if test="${requestScope.operator==null }">
<c:redirect url="selectServlet"/>
</c:if>
<c:if test="${requestScope.operator!='add'&&requestScope.news==null }">
<c:redirect url="selectServlet"/>
</c:if>
<form action="operator" method="POST" id="f1">
<c:if test="${requestScope.operator=='update'}">
<input type="hidden" name="author" value="${sessionScope.username}"/>
<input type="hidden" name="lockflag" value="${sessionScope.userflag}"/>
<input type="hidden" name="operator" value="updateNews"/>
<input type="hidden" name="nid" value="${requestScope.news.nid}"/>
title:<input type="text" name="title" id="title" value="${requestScope.news.title}"/><span id="sp1"></span><br/>
content:<textarea rows="10" cols="30" id="content" name="content">${requestScope.news.content}</textarea><br/>
<input type="button" value="更新" id="btn_ok"/><input type="button" value="取消" id="btn_cancel"/>
</c:if>
<c:if test="${requestScope.operator=='add' }">
<input type="hidden" name="author" value="${sessionScope.username}"/>
<input type="hidden" name="lockflag" value="${sessionScope.userflag}"/>
<input type="hidden" name="operator" value="addNews"/>
<input type="hidden" name="nid" value="${requestScope.news.nid}"/>
title:<input type="text" name="title" id="title" value="${requestScope.news.title}"/><span id="sp1"></span><br/>
content:<textarea rows="10" cols="30" id="content" name="content">${requestScope.news.content}</textarea><br/>
<input type="button" value="更新" id="btn_ok"/><input type="button" value="取消" id="btn_cancel"/>
</c:if>
<c:if test="${requestScope.operator=='select'}">
title:<input type="text" name="title" id="title" value="${requestScope.news.title}" readonly="readonly"/><span id="sp1"></span><br/>
content:<textarea rows="10" cols="30" id="content" name="content" readonly="readonly">${requestScope.news.content}</textarea><br/>
<input type="button" value="返回" id="btn_cancel"/>
</c:if>
</form>
<form action="selectServlet" method="POST" id="f2">
</form>
</body>
</html>
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
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="${requestScope.basePath}">
<title>My JSP 'index.jsp' starting 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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script>
function $(id){
return document.getElementById(id);
}
//当页面加载完毕
window.onload = function(){
//获取页面的组件
var userid = $("userid");
var password = $("password");
var sp1 = $("sp1");
var sp2 = $("sp2");
var btn_login = $("btn_login");
//对按钮设置点击事件
btn_login.onclick = function(){
//定义一个变量代表表单是否可以提交
var flag = true;
//获取userid的value
var useridValue = userid.value;
//获取密码的value
var passwordValue = password.value;
//判断userid是否为空
if(useridValue==null||useridValue==""){
flag=false;
sp1.innerHTML = "<font color='red'>userid不能为空</font>";
return;
}else{
flag=true;
sp1.innerHTML = "";
}
//判断password是否为空
if(passwordValue==null||passwordValue==""){
flag=false;
sp2.innerHTML = "<font color='red'>password不能为空</font>";
return;
}else{
flag=true;
sp2.innerHTML = "";
}
//定义userid的正则表达式
var useridReg = /(^1[3-9]\d{9}$)|(^\w[a-zA-Z0-9]{5,7}@[0-9a-zA-Z]{2,7}(\.com\|\.cn\|\.net)$)/;
//判断userid是否满足格式
if(useridValue.match(useridReg)==null){
flag=false;
sp1.innerHTML= "<font color='red'>userid的格式不正确</font>";
return;
}else{
flag=true;
sp1.innerHTML = "";
}
//定义password的正则表达式
var passwordReg = /\d{6,16}/;
//判断userid是否满足格式
if(passwordValue.match(passwordReg)==null){
flag=false;
sp2.innerHTML= "<font color='red'>password的格式不正确</font>";
return;
}else{
flag=true;
sp2.innerHTML = "";
}
//判断是否能提交
if(flag==true){
document.forms[0].submit();
}
}
}
</script>
</head>
<body>
<%
//获取错误信息的内容
//String errorInfo = (String)request.getAttribute("errorInfo");
/*
EL表达式可以判定是否为空
*/
//判断得到的错误信息是否为空,比如我们第一次访问这个页面就可能是空
//if(errorInfo==null){
// errorInfo="";
//}
%>
<form action="check" method="POST">
userid:<input type="text" name="userid" id="userid"/><span id="sp1">${requestScope.errorInfo}</span><br/>
password:<input type="password" name="password" id="password"/><span id="sp2"></span><br/>
<input type="button" value="登录" id="btn_login"/><input type="reset" value="重置"/>
</form>
</body>
</html>
showNews.jsp:
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.oracle.vo.News"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
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="${requestScope.basePath }">
<title>My JSP 'showNews.jsp' starting 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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script>
window.onload = function(){
var deleteas = document.getElementsByName("deletea");
for(var i=0;i<deleteas.length;i++){
var deletea = deleteas[i];
deletea.onclick = function(){
var deleteTitle = this.getAttribute("title");
var flag = confirm("确定要删除"+deleteTitle+"吗??");
return flag;
}
}
}
</script>
</head>
<body>
<table border="1" align="center">
<tr>
<td colspan="5"><a href="operator?operator=add">添加新闻</a></td>
</tr>
<tr>
<th>新闻的编号</th>
<th>新闻的标题</th>
<th>新闻的作者</th>
<th>新闻的发布时间</th>
<th>操作</th>
</tr>
<c:if test="${requestScope.news==null}">
<tr>
<td colspan="5">暂无数据</td>
</tr>
</c:if>
<c:if test="${requestScope.news!=null}">
<c:forEach items="${requestScope.news }" var="n">
<tr>
<td>${n.nid }</td>
<td><a href="operator?operator=select&nid=${n.nid }">${n.title}</a></td>
<td>${n.author }</td>
<td>${n.pubdate }</td>
<c:if test="${sessionScope.userflag==0 }">
<c:if test="${n.getLockflag()==0}">
<c:if test="${n.author==sessionScope.username }">
<td><a href="operator?operator=update&nid=${n.nid }">修改</a>/<a href="operator?operator=delete&nid=${n.nid }" name="deletea" title="${n.title }">删除</a></td>
</c:if>
<c:if test="${n.author!=sessionScope.username }">
<td><a href="operator?operator=delete&nid=${n.nid }" name="deletea" title="${n.title }">删除</a></td>
</c:if>
</c:if>
<c:if test="${n.getLockflag()!=0}">
<c:if test="${n.author==sessionScope.username }">
<td><a href="operator?operator=update&nid=${n.nid }">修改</a>/<a href="operator?operator=delete&nid=${n.nid }" name="deletea" title="${n.title }">删除</a></td>
</c:if>
<c:if test="${n.author!=sessionScope.username }">
<td><a href="operator?operator=pass&nid=${n.nid }">通过</a>/<a href="operator?operator=delete&nid=${n.nid }" name="deletea" title="${n.title }">删除</a></td>
</c:if>
</c:if>
</c:if>
<c:if test="${sessionScope.userflag!=0 }">
<c:if test="${n.author==sessionScope.username }">
<td><a href="operator?operator=update&nid=${n.nid }">修改</a>/<a href="operator?operator=delete&nid=${n.nid }" name="deletea" title="${n.title }">删除</a></td>
</c:if>
<c:if test="${n.author!=sessionScope.username }">
<td>-</td>
</c:if>
</c:if>
</tr>
</c:forEach>
<tr align="center">
<td colspan="5">
<a href="selectServlet?pn=1">首页</a>
<a href="selectServlet?pn=${requestScope.pageNum-1}">上一页</a>
<a href="selectServlet?pn=${requestScope.pageNum+1}">下一页</a>
<a href="selectServlet?pn=${requestScope.totalPage}">尾页</a>
</td>
</tr>
</c:if>
</table>
</body>
</html>
Dao类:
package com.oracle.dao;
import java.util.List;
public interface BaseDao<T> {
public abstract void insert(T t);
public abstract void update(T t);
public abstract void delete(T t);
public abstract T selectById(T t);
public abstract List<T> selectAll();
}
package com.oracle.dao;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.oracle.tools.DBTools;
import com.oracle.vo.News;
public class NewsDao implements BaseDao<News> {
@Override
public void insert(News t) {
String sql = "insert into news(title,author,pubdate,content,lockflag) values(?,?,?,?,?)";
DBTools.executeUpdate(sql, t.getTitle(),t.getAuthor(),t.getPubdate(),t.getContent(),t.getLockflag());
}
@Override
public void update(News t) {
String sql = "update news set title=?,author=?,pubdate=?,content=?,lockflag=? where nid=?";
DBTools.executeUpdate(sql, t.getTitle(),t.getAuthor(),t.getPubdate(),t.getContent(),t.getLockflag(),t.getNid());
}
@Override
public void delete(News t) {
String sql = "delete from news where nid=?";
DBTools.executeUpdate(sql, t.getNid());
}
@Override
public News selectById(News t) {
News n = null;
String sql = "select * from news where nid=?";
List<Object[]> objs = DBTools.executeQuery(sql, t.getNid());
if(objs!=null&&objs.size()>0){
Object[] obj = objs.get(0);
int nid = (Integer)obj[0];
String title = (String)obj[1];
String author = (String)obj[2];
Date pubdate = (Date)obj[3];
String content