回顾:JavaWeb.05.新闻系统功能制作
项目功能的完善
indext.jsp主界面
read.jsp阅读新闻界面
show.jsp新闻分类展示界面
indext.jsp主界面
<%@page import="java.nio.charset.StandardCharsets"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>bootstrap</title>
<meta content="width=device-width, initial-scale=1" name="viewport">
<link href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
* {
outline: none !important;
}
body,
html {
background: #7f8d90;
}
nav,
.breadcrumb {
border-radius: 0px !important;
margin-bottom: 0px !important;
}
.breadcrumb {
margin-bottom: 20px !important;
background: #36485c;
color: white;
}
li h4 {
width: 300px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.breadcrumb .active {
color: yellow;
}
</style>
</head>
<body>
<nav class="navbar navbar-default hidden-sm hidden-xs">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="${pageContext.request.contextPath}/index.jsp"
style="font-size: 25px;">🐖</a>
</div>
<ul class="nav navbar-nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown"> 新闻管理
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="${pageContext.request.contextPath}/add.jsp">新闻发布</a></li>
<li class="divider"></li>
<li><a href="#">类别管理</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a><%=request.getParameter("yh") %>
</a></li>
<li><a href="#">退出<span class="glyphicon glyphicon-off"></span></a></li>
</ul>
</div>
</nav>
<ol class="breadcrumb">
<li>您当前的位置是</li>
<li>新闻发布系统</li>
<li class="active">首页</li>
</ol>
<!--主界面的表单要设置action,每个表单最好都设置,post可以让id不显示在路径上-->
<form action="${pageContext.request.contextPath}/index.jsp" method="post" class="form-inline" style="margin: 0 auto 20px;">
<div class="form-group" style="display: block;text-align: center;">
<div class="input-group">
<div class="input-group-addon">新闻标题</div>
<!--表单提交的时候会将name属性的值带到路径上-->
<input name="newName" class="form-control" placeholder="请在此输入搜索的关键字" type="text">
<span class="input-group-btn">
<button class="btn btn-primary" type="submit">搜索🔍</button>
</span>
</div>
</div>
</form>
<div class="container">
<ul class="list-group">
<%
//如何取到路径上的表单查询框值newName:
String newName=request.getParameter("newName");
//当从登陆界面进入主界面时,newName=null,所以要在主界面用""做模糊查询显示所有已发布新闻
if(newName==null){
newName="";//查询所有数据
}
//防止newName乱码,字符.getBytes拿到字节,并转成中文
newName=new String(newName.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//定义连接字符串
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
//获得连接
Connection con = DriverManager.getConnection(url, "scott", "123");
//查询所有的新闻数据:用拿到的newName去数据库里面做模糊查询
PreparedStatement ps = con.prepareStatement("select * from t2_news where news_title like ?");
//占位符赋值
ps.setString(1,"%"+newName+"%");
//得到结果集
ResultSet rs = ps.executeQuery();
//结果集中有很多数据
while (rs.next()) {
%>
<!--两个《%%》里面的内容会跟随迭代器一起迭代-->
<li class="list-group-item">
<h4 class="list-group-item-heading">
<!-- ?newId= 取到get路径上的值,将数据库中的id传给read界面 -->
<a href=" ${pageContext.request.contextPath}/read.jsp?newId=<%=rs.getInt(1)%>" data-placement="bottom" data-toggle="tooltip" title="国家卫健委:昨日新增确诊病例29例,其中本土病例2例">
<%=rs.getString(2)%>
</a>
</h4>
<p class="list-group-item-text text-right">
<!-- rs.getString(4)拿到数据库数据,=在网页打印 -->
<span class="glyphicon glyphicon-user"><code><%=rs.getString(4)%></code></span>
<!-- 点击量 -->
<span class="glyphicon glyphicon-time"><code><%=rs.getInt(6)%></code></span>
<!-- 收藏量 -->
<span class="glyphicon glyphicon-time"><code><%=rs.getInt(7)%></code></span>
<span class="glyphicon glyphicon-time"><code><%=rs.getString(5)%></code></span>
</p>
</li>
<%
}
if (!con.isClosed()) {
con.close();
}
ps.close();
rs.close();
%>
</ul>
</div>
<div class="container text-center">
<ul class="pagination" style="margin: 20px auto;">
<li>
<a href="#"><span>«</span></a>
</li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li>
<a href="#"><span>»</span></a>
</li>
</ul>
</div>
<script>
$(function () {
$('[data-toggle="tooltip"]').tooltip({
trigger: "hover"
})
})
</script>
</body>
</html>
read.jsp阅读新闻界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<!DOCTYPE html>
<html lang="zh">
<head>
<!-- read读取新闻内容的界面具有新闻修改,删除按钮 -->
<meta charset="UTF-8">
<title>bootstrap</title>
<meta content="width=device-width, initial-scale=1" name="viewport">
<link href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
* {
outline: none !important;
}
body,
html {
background: #7f8d90;
}
nav,
.breadcrumb {
border-radius: 0 !important;
margin-bottom: 0 !important;
}
.breadcrumb {
margin-bottom: 20px !important;
background: #36485c;
color: white;
}
input,
select,
textarea,
.panel-heading {
border: none !important;
border-radius: 0 !important;
}
.breadcrumb .active {
color: yellow;
}
</style>
</head>
<body>
<nav class="navbar navbar-default hidden-sm hidden-xs">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="${pageContext.request.contextPath}/index.jsp"
style="font-size: 25px;">🐖</a>
</div>
<ul class="nav navbar-nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">
新闻管理
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="#">新闻发布</a></li>
<li class="divider"></li>
<li><a href="#">类别管理</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a>245@qq.com</a></li>
<li><a href="#">退出<span class="glyphicon glyphicon-off"></span></a></li>
</ul>
</div>
</nav>
<ol class="breadcrumb">
<li>您当前的位置是</li>
<li>新闻发布系统</li>
<li class="active">新闻阅读</li>
</ol>
<%
//获得新闻的id,根据id去数据库做查询操作
String newId = request.getParameter("newId");
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//定义连接字符串
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
//获得连接
Connection con = DriverManager.getConnection(url, "scott", "123");
//查询所有的新闻数据
PreparedStatement ps = con.prepareStatement("select * from t2_news where news_id=?");
//占位符的设置(页面上的值都是String类型)
ps.setInt(1,Integer.parseInt(newId));
//得到结果集
ResultSet rs = ps.executeQuery();
//定义需要的值
String title="";
int count=0;
String author="";
String publisher="";
String content="";
int click=0;
//使用迭代器进行数据查询
if(rs.next()){
//可以取值
title=rs.getString(2);
publisher=rs.getString(5);
author=rs.getString(4);
content=rs.getString(8);
click=rs.getInt(6)+1;//加载这个页时就阅读了一次了
}
//到这里新闻内容就已经被用户阅读了
//将数据库里面的点击量+1
ps=con.prepareStatement("update t2_news set news_click=news_click+1 where news_id=?");
ps.setInt(1,Integer.parseInt(newId));
//执行修改
ps.executeUpdate();//修改不需要判断是否修改成功,直接反应在页面上
%>
<div class="container" style="background: rgba(239, 231, 231, 0.9);border-radius:10px;">
<h1><%=title%></h1>
<h3 class="text-right">
<small>
<span class="glyphicon glyphicon-user"><span class="label label-default"><%=author%></span></span>
<span class="glyphicon glyphicon-eye-open"><span class="label label-default"><%=click%></span></span>
<span class="glyphicon glyphicon-time"><span class="label label-info"><%=publisher%></span></span>
</small>
</h3>
<samp><%=content%></samp>
<div class="btn-group btn-group-justified" style="margin-bottom: 20px;">
<div class="btn-group">
<!-- newId是index传过来的! -->
<!--删除按钮不能直接在read界面写删除操作是代码是因为这上面的代码会随着页面的每一次加载而运行,会每次一加载界面就删除,所以要写doDel后台-->
<a href="${pageContext.request.contextPath}/doDel.jsp?newId=<%=newId%>" class="btn btn-danger" type="button">删除</a>
</div>
<div class="btn-group">
<!--将newId也传给修改界面upd-->
<a href="${pageContext.request.contextPath}/upd.jsp?newId=<%=newId%>" class="btn btn-info" type="button">修改</a>
</div>
</div>
</div>
<div class="container" style="background: rgba(239, 231, 231, 0.9);border-radius:10px;margin-top: 10px;">
<div class="panel panel-default" style="margin-top: 20px;">
<!-- 循环在评论表中做查询显示-->
<%
ps=con.prepareStatement("select * from t2_comment where comment_from=?");
ps.setInt(1,Integer.parseInt(newId));
rs=ps.executeQuery();
while(rs.next()){
%>
<div class="panel-heading">
<span class="glyphicon glyphicon-user"><span class="label label-success"></span></span>
<p style="margin-top: 10px;text-indent: 2em;">
用户名<samp><%=rs.getString(4)%></samp>
</p>
<p>
<samp><%=rs.getString(5)%></samp>
</p>
<p class="text-right">
<span class="glyphicon glyphicon-time"><span class="label label-info"><%=rs.getString(3)%></span></span>
</p>
<p>
<a href="${pageContext.request.contextPath}/doDelPl.jsp?newId=<%=newId%>" class="btn btn-danger" type="button">删除</a>
</p>
</div>
<%
}
%>
</div>
</div>
<!-- 评论表的表单 -->
<!-- 点击按钮时(表单中的按钮默认为提交按钮),会发出name属性值在路径上,
将一条评论插入数据库时要拿到这条评论的id-->
<form action="doAddPl.jsp" class="container" style="background: rgba(239, 231, 231, 0.9);border-radius:10px;margin-top: 10px;padding: 30px;">
<!--评论的id/id就是评论的来源-->
<input type="hidden" value=<%=newId %> name=newId>
<div class="form-group">
<label for="name">Name</label>
<input name="author" id="name" class="form-control" placeholder="用户名称" required type="text">
</div>
<div class="form-group">
<label for="email">content</label>
<input name="content" id="email" class="form-control" placeholder="评论内容" required type="text">
</div>
<button class="btn btn-default" type="submit">发布评论</button>
</form>
<div style="height: 50px;"></div>
</body>
</html>
show.jsp新闻分类展示界面
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
</head>
<!--新闻分类显示界面,读出数据库的内容显示-->
<body>
<nav class="navbar navbar-default hidden-sm hidden-xs">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" style="font-size: 25px;">当前是新闻首页</a>
</div>
<ul class="nav navbar-nav navbar-right" style="margin-right: 20px;">
<li><a href="#">返回管理员首页<span class="glyphicon glyphicon-road"></span></a></li>
</ul>
</div>
</nav>
<div class="container" style="margin: 10px auto;">
<div class="btn-group btn-group-justified">
<%
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//定义连接字符串
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
//获得连接
Connection con = DriverManager.getConnection(url, "scott", "123");
//查询所有的新闻数据
PreparedStatement ps = con.prepareStatement("select * from t1_topic");
//得到结果集
ResultSet rs = ps.executeQuery();
//结果集中有很多数据
while (rs.next()) {
String topicId=request.getParameter("topicId");
if(topicId==null){
topicId="-1";
}
%>
<!-- active激活 -->
<!-- java代码会优先执行 -->
<a href="show.jsp?topicId=<%=rs.getInt(1) %>" <%=rs.getInt(1)==Integer.parseInt(topicId)? "active":"" %>
type="button" class="btn btn-success"><%=rs.getString(2)%></a>
<%
}
%>
</div>
</div>
<div class="container">
<ul class="list-group">
<%
String topicId=request.getParameter("topicId");
String sql="select * from t2_news";//查询所有
//如果这个界面路径携带了 id就不为null
//如果这个界面路径没有携带 id就为null
if(topicId!=null){
//where前面要留一个空格,和上面的sql语句拼接!
sql+=" where news_topic="+topicId;//加上主题查询
}
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
%>
<li class="list-group-item">
<h4 class="list-group-item-heading">
<a href="" data-toggle="tooltip" data-placement="bottom" title="国家卫健委:昨日新增确诊病例29例,其中本土病例2例">
国家卫健委:昨日新增确诊病例29例,其中本土病例2例
</a>
</h4>
<p class="list-group-item-text text-right">
<span class="glyphicon glyphicon-user"><code><%=rs.getString(2) %></code></span>
<span class="glyphicon glyphicon-eye-open"><code><%=rs.getString(4) %></code></span>
<span class="glyphicon glyphicon-tag"><code><%=rs.getString(6) %></code></span>
<span class="glyphicon glyphicon-time"><code><%=rs.getString(5)%></code></span>
</p>
</li>
<%
}
%>
</ul>
</div>
</body>
</html>
你要尽全力保护你的梦想。那些嘲笑你梦想的人,他们注定失败,他们想把你变成和他们一样。我坚信,只要心中有梦想,我就会与众不同。你也是。