JavaWeb評論功能實(shí)現(xiàn)步驟以及代碼實(shí)例
前言
評論功能是后端要寫常見的功能之一,一般性的網(wǎng)站也都會(huì)包含這一功能。像是購物網(wǎng)站、視頻網(wǎng)站下方都會(huì)有用戶評論的功能。
一、分析功能
首先要分析功能:1.用戶登錄點(diǎn)擊商品后可查看所有普通用戶的評論。
2.用戶可以添加評論,發(fā)送到評論區(qū)。
3.用戶可以刪除該用戶寫的評論。(不能刪除其他人的評論)
二、實(shí)現(xiàn)功能
1.建評論表
外鍵約束:user_id關(guān)聯(lián)user表、motorcycle_id關(guān)聯(lián)商品表(motorcycle)。

然后創(chuàng)建實(shí)體類。
2.Dao層、service層核心代碼實(shí)現(xiàn)
查詢評論:
String sql="select c.id, c.user_id,c.motorcycle_id,c.motorcycle_comment,u.username from comment c left join user u on c.user_id=u.id where c.motorcycle_id=?";
添加評論:
String sql = "insert into comment(user_id,motorcycle_id,motorcycle_comment) values(?,?,?)"
刪除評論:
String sql = "delete from comment where id=?";
service層直接調(diào)用,不做處理。
clist = cDao.getMotorcycleComment(motorcycleId);
3.servlet層編寫核心代碼
將查詢結(jié)果放到request域里。
List<Comment> clist=commentService.getMotorcycleComment(id);
// for (Comment c:clist
// ) {
// System.out.println(c);
// }
request.getSession().setAttribute("MotorcycleId", id);
request.setAttribute("clist", clist);調(diào)用刪除后重定向到詳情頁。
commentService.deleteComment(commentId);
resp.sendRedirect("/motorcycle_detail?id="+motorcycleId);添加也是,添加完后重定向到商品詳情頁。
CommentService commentService=new CommentService();
int userId= Integer.parseInt(req.getParameter("userId"));
int motorcycleId= Integer.parseInt(req.getParameter("motorcycleId"));
String motorcycleComment=req.getParameter("comment");
commentService.addComment(userId,motorcycleId,motorcycleComment);
// req.getRequestDispatcher("/motorcycle_detail?id="+motorcycleId).forward(req, resp);
resp.sendRedirect("/motorcycle_detail?id="+motorcycleId);4.jsp核心代碼

三、展示效果圖
效果查看

添加一條后

數(shù)據(jù)庫變化:新增一條信息

點(diǎn)擊刪除:發(fā)現(xiàn)已經(jīng)沒有該評論。

刷新數(shù)據(jù)庫后:

總結(jié)
效果展示完成。實(shí)現(xiàn)起來不算難,但要明白其中的外鍵約束關(guān)系,明白其中的邏輯。代碼不是很多,大家快練起來~
到此這篇關(guān)于JavaWeb評論功能實(shí)現(xiàn)步驟以及代碼實(shí)例的文章就介紹到這了,更多相關(guān)JavaWeb評論功能實(shí)現(xiàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring使用注解進(jìn)行引用類型的自動(dòng)裝配逐步分析
自動(dòng)裝配是springboot的核心,一般提到自動(dòng)裝配就會(huì)和springboot聯(lián)系在一起。實(shí)際上Spring Framework早就實(shí)現(xiàn)了這個(gè)功能。Spring Boot只是在其基礎(chǔ)上,通過SPI的方式,做了進(jìn)一步優(yōu)化2023-03-03
解析Java的設(shè)計(jì)模式編程之解釋器模式的運(yùn)用
這篇文章主要介紹了解析Java的設(shè)計(jì)模式編程之解釋器模式的運(yùn)用,文中對解釋器模式的優(yōu)缺點(diǎn)及適用場景作出了分析,需要的朋友可以參考下2016-02-02
SpringBoot?Test的webEnvironment源碼解讀
這篇文章主要為大家介紹了SpringBoot?Test的webEnvironment源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
java構(gòu)造函數(shù)示例(構(gòu)造方法)
這篇文章主要介紹了java構(gòu)造函數(shù)示例(構(gòu)造方法),需要的朋友可以參考下2014-03-03
SpringBoot獲取yml和properties配置文件的內(nèi)容
這篇文章主要為大家詳細(xì)介紹了SpringBoot獲取yml和properties配置文件的內(nèi)容,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
解讀@Scheduled任務(wù)調(diào)度/定時(shí)任務(wù)非分布式
這篇文章主要介紹了解讀@Scheduled任務(wù)調(diào)度/定時(shí)任務(wù)非分布式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

