国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【Java Web】使用ajax在論壇中發(fā)布帖子

這篇具有很好參考價值的文章主要介紹了【Java Web】使用ajax在論壇中發(fā)布帖子。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

  • AJAX
    • 異步的JavaScript與XML,不是一門新技術(shù),只是一個新術(shù)語;
    • 使用AJAX,網(wǎng)頁能增量更新在頁面上,不需要刷新整個頁面;
    • JSON的使用比XML使用更加普遍;
  • 示例
    • 使用jQuery發(fā)送AJAX請求;
  • 實踐
    • 采用AJAX請求,實現(xiàn)發(fā)布帖子功能。

1. DiscussPost類

package com.nowcoder.community.entity;

import java.util.Date;

public class DiscussPost {

    private int id;
    private int userId;
    private String title;
    private String content;
    private int type;
    private int status;
    private Date createTime;
    private int commentCount;
    private double score;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public int getCommentCount() {
        return commentCount;
    }

    public void setCommentCount(int commentCount) {
        this.commentCount = commentCount;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "DiscussPost{" +
                "id=" + id +
                ", userId=" + userId +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", type=" + type +
                ", status=" + status +
                ", createTime=" + createTime +
                ", commentCount=" + commentCount +
                ", score=" + score +
                '}';
    }
}

2. 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.nowcoder.community.dao.DiscussPostMapper">

    <sql id="selectFields">
        id, user_id, title, content, type, status, create_time, comment_count, score
    </sql>

    <sql id="insertFields">
        user_id, title, content, type, status, create_time, comment_count, score
    </sql>

    <select id="selectDiscussPosts" resultType="DiscussPost">
        select <include refid="selectFields"></include>
        from discuss_post
        where status != 2
        <if test="userId!=0">
            and user_id = #{userId}
        </if>
        order by type desc, create_time desc
        limit #{offset}, #{limit}
    </select>

    <select id="selectDiscussPostRows" resultType="int">
        select count(id)
        from discuss_post
        where status != 2
        <if test="userId!=0">
            and user_id = #{userId}
        </if>
    </select>

    <insert id="insertDiscussPost" parameterType="DiscussPost">
        insert into discuss_post (<include refid="insertFields"></include>)
        values (#{userId},#{title},#{content},#{type},#{status},#{createTime},#{commentCount},#{score})
    </insert>


</mapper>

3. Service

package com.nowcoder.community.service;

import com.nowcoder.community.dao.DiscussPostMapper;
import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
public class DiscussPostService {

    @Autowired
    private DiscussPostMapper discussPostMapper;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    public List<DiscussPost> findDiscussPosts(int userId, int offset, int limit) {
        return discussPostMapper.selectDiscussPosts(userId, offset, limit);
    }

    public int findDiscussPostRows(int userId) {
        return discussPostMapper.selectDiscussPostRows(userId);
    }

    public int addDiscussPost(DiscussPost post){
        if(post == null){
            throw new IllegalArgumentException("參數(shù)不能為空");
        }

        // 轉(zhuǎn)義HTML標(biāo)記
        post.setTitle(HtmlUtils.htmlEscape(post.getTitle()));
        post.setContent(HtmlUtils.htmlEscape(post.getContent()));
        // 過濾敏感詞
        post.setTitle(sensitiveFilter.filter(post.getTitle()));
        post.setContent(sensitiveFilter.filter(post.getContent()));

        return discussPostMapper.insertDiscussPost(post);
    };

}

4. Controller

package com.nowcoder.community.controller;

import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.service.DiscussPostService;
import com.nowcoder.community.util.CommunityUtil;
import com.nowcoder.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping(path = "/discuss")
public class DiscussPostController {

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private HostHolder hostHolder;

    @RequestMapping(path="/add", method = RequestMethod.POST)
    @ResponseBody
    public String addDiscussPost(String title, String content){
        User user = hostHolder.getUser();
        if(user == null){  // 未登錄
            return CommunityUtil.getJSONString(403,"當(dāng)前用戶未登錄!");
        }
        DiscussPost post = new DiscussPost();
        post.setUserId(user.getId());
        System.out.println(user.getId());
        post.setTitle(title);
        post.setContent(content);
        post.setCreateTime(new Date());
        discussPostService.addDiscussPost(post);

        // 報錯的情況將來統(tǒng)一處理
        return CommunityUtil.getJSONString(0,"發(fā)布成功!");
    }
}

5. 前端jquery

$(function(){
	$("#publishBtn").click(publish);
});

function publish() {
	// 點擊“發(fā)布”,隱藏發(fā)布框
	$("#publishModal").modal("hide");

	// 獲取標(biāo)題和內(nèi)容
	var title = $("#recipient-name").val();
	var content = $("#message-text").val();

	// 發(fā)送異步請求
	$.post(
		CONTEXT_PATH + "/discuss/add",
		{"title":title, "content":content},
		function (data){
			data = $.parseJSON(data);
			// 在提示框中顯示返回的消息
			$("#hintBody").text(data.msg);
			// 顯示提示框,2秒后自動關(guān)閉
			$("#hintModal").modal("show");
			setTimeout(function(){
				$("#hintModal").modal("hide");
				// 刷新頁面
				if(data.code == 0){
					window.location.reload();
				}
			}, 2000);
		}
	)


}

文章來源地址http://www.zghlxwxcb.cn/news/detail-694019.html

到了這里,關(guān)于【Java Web】使用ajax在論壇中發(fā)布帖子的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Spring Boot 3.2發(fā)布:大量Java 21的支持上線,改進(jìn)可觀測性

    Spring Boot 3.2發(fā)布:大量Java 21的支持上線,改進(jìn)可觀測性

    就在今天凌晨,Spring Boot 3.2正式發(fā)布了!該版本是在Java 21正式發(fā)布之后的重要支持版本,所以在該版本中包含大量對Java 21支持的優(yōu)化。 下面,我們分別通過Spring官方發(fā)布的博文和Josh Long長達(dá)80+分鐘的介紹視頻,一起認(rèn)識一下Spring Boot 3.2最新版本所帶來的全新內(nèi)容。 官方博文

    2024年02月05日
    瀏覽(25)
  • 手把手搭建 java spring boot 框架 maven 項目 web 網(wǎng)址訪問

    手把手搭建 java spring boot 框架 maven 項目 web 網(wǎng)址訪問

    第一步我們?nèi)? spring boot 官網(wǎng)創(chuàng)建項目并下載壓縮包? 創(chuàng)建項目網(wǎng)址: Spring Initializr https://start.spring.io/ 我們添加一個 srping web 的拓展包 接下來我們點擊 generate?創(chuàng)建 并下載壓縮包即可 接下來我們將壓縮文件包解壓到項目根目錄使用編輯器打開即可,如果編輯器提示?點擊構(gòu)

    2024年04月23日
    瀏覽(22)
  • Java Web現(xiàn)代化開發(fā):Spring Boot + Mybatis + Redis二級緩存

    Java Web現(xiàn)代化開發(fā):Spring Boot + Mybatis + Redis二級緩存

    Spring-Boot因其提供了各種開箱即用的插件,使得它成為了當(dāng)今最為主流的Java Web開發(fā)框架之一。Mybatis是一個十分輕量好用的ORM框架。Redis是當(dāng)今十分主流的分布式key-value型數(shù)據(jù)庫,在web開發(fā)中,我們常用它來緩存數(shù)據(jù)庫的查詢結(jié)果。 本篇博客將介紹如何使用Spring-Boot快速搭建一

    2024年01月17日
    瀏覽(19)
  • Java中使用Spring Boot創(chuàng)建RESTful API

    在當(dāng)今的Web開發(fā)中,構(gòu)建RESTful API已經(jīng)成為一個常見的任務(wù)。Spring Boot框架提供了一種簡單、快速和高效的方式來創(chuàng)建和部署這樣的API。本文將引導(dǎo)您逐步了解如何使用Spring Boot來構(gòu)建和開發(fā)RESTful API。 首先,我們需要設(shè)置開發(fā)環(huán)境。確保您的系統(tǒng)上已經(jīng)安裝了以下軟件: Ja

    2024年02月10日
    瀏覽(27)
  • Java web編寫在線論壇系統(tǒng)(bbs) 完整源碼 附帶詳細(xì)的設(shè)計報告

    Java web編寫在線論壇系統(tǒng)(bbs) 完整源碼 附帶詳細(xì)的設(shè)計報告

    今天為大家分享一個java語言編寫的在線論壇系統(tǒng),目前系統(tǒng)功能已經(jīng)很全面,后續(xù)會進(jìn)一步完善。整個系統(tǒng)界面漂亮,有完整得源碼,希望大家可以喜歡。喜歡的幫忙點贊和關(guān)注。一起編程、一起進(jìn)步 開發(fā)語言為Java,開發(fā)環(huán)境Eclipse或者IDEA都可以。數(shù)據(jù)庫采用:MySQL。 本項

    2024年02月08日
    瀏覽(18)
  • 【Spring Boot】Spring Boot實現(xiàn)完整論壇功能示例代碼

    以下是一個簡單的Spring Boot論壇系統(tǒng)示例代碼: 首先是數(shù)據(jù)庫設(shè)計,我們創(chuàng)建以下幾張表: user表,存儲用戶信息,包括id、username、password、email、create_time等字段。 topic表,存儲帖子信息,包括id、title、content、user_id、create_time等字段。 comment表,存儲評論信息,包括id、con

    2024年02月11日
    瀏覽(28)
  • 使用Spring Boot和Docker快速部署Java應(yīng)用程序

    隨著微服務(wù)的興起,容器化技術(shù)已成為現(xiàn)代應(yīng)用程序開發(fā)和部署的關(guān)鍵部分。Docker作為一種流行的容器化解決方案,廣泛應(yīng)用于企業(yè)和開發(fā)者社區(qū)。與此同時,Spring Boot作為一種優(yōu)秀的Java開發(fā)框架,大大簡化了基于Spring的應(yīng)用程序開發(fā)。在本文中,我們將探討如何將Spring Bo

    2024年02月01日
    瀏覽(26)
  • 2023 最新版IntelliJ IDEA 2023.1創(chuàng)建Java Web前(vue3)后端(spring-boot3)分離 項目詳細(xì)步驟(圖文詳解)

    2023 最新版IntelliJ IDEA 2023.1創(chuàng)建Java Web前(vue3)后端(spring-boot3)分離 項目詳細(xì)步驟(圖文詳解)

    2023 最新版IntelliJ IDEA 2023.1創(chuàng)建Java Web 項目詳細(xì)步驟(圖文詳解) 本篇使用當(dāng)前Java Web開發(fā)主流的spring-boot3框架來創(chuàng)建一個Java前后端分離的項目,前端使用的也是目前前端主流的vue3進(jìn)行一個簡單的項目搭建,讓你距離Java全棧開發(fā)更近一步 ?????。 使用版本: “17.0.1”

    2024年02月12日
    瀏覽(34)
  • 基于Spring Boot的校園論壇網(wǎng)站

    ?? 作者主頁:超級無敵暴龍戰(zhàn)士塔塔開 ?? 簡介:Java領(lǐng)域優(yōu)質(zhì)創(chuàng)作者??、 簡歷模板、學(xué)習(xí)資料、面試題庫【關(guān)注我,都給你】 ??文末獲取源碼聯(lián)系?? 基于Spring Boot的校園論壇網(wǎng)站,java項目。 eclipse和idea都能打開運行。 推薦環(huán)境配置:eclipse/idea jdk1.8 maven mysql 前端技術(shù):

    2024年02月08日
    瀏覽(17)
  • Spring boot使用Kafka Java反序列化漏洞 CVE-2023-34040

    Spring boot使用Kafka Java反序列化漏洞 CVE-2023-34040

    背景:公司項目掃描到 Spring-Kafka上使用通配符模式匹配進(jìn)行的安全繞過漏洞 CVE-2023-20873 中等風(fēng)險 | 2023年8月23日 | CVE-2023-34040 在Spring for Apache Kafka 3.0.9及更早版本以及2.9.10及更早版本中,存在可能的反序列化攻擊向量,但只有在應(yīng)用了不常見的配置時才會出現(xiàn)。攻擊者必須在

    2024年02月07日
    瀏覽(45)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包