引言

前端实现:Vue.js 3

1. 项目初始化

首先,您需要创建一个Vue.js 3项目。可以使用Vue CLI来完成这一步骤:

vue create comment-system

2. 安装依赖

接下来,安装必要的依赖,如Axios用于网络请求和Vuex用于状态管理:

cd comment-system
npm install axios vuex

3. 创建Vuex Store

// store.js
import { createStore } from 'vuex';

export default createStore({
  state() {
    return {
      comments: []
    };
  },
  mutations: {
    setComments(state, comments) {
      state.comments = comments;
    }
  },
  actions: {
    fetchComments({ commit }) {
      // 这里可以替换为您的API端点
      axios.get('/api/comments')
        .then(response => {
          commit('setComments', response.data);
        })
        .catch(error => {
          console.error('Error fetching comments:', error);
        });
    }
  }
});

4. 创建评论组件

<!-- Comment.vue -->
<template>
  <div>
    <ul>
      <li v-for="comment in comments" :key="comment.id">
        <span>{{ comment.author }}</span>: {{ comment.content }}
      </li>
    </ul>
    <form @submit.prevent="addComment">
      <input v-model="newComment.author" placeholder="Author" required>
      <input v-model="newComment.content" placeholder="Comment" required>
      <button type="submit">Post Comment</button>
    </form>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';

export default {
  data() {
    return {
      newComment: {
        author: '',
        content: ''
      }
    };
  },
  computed: {
    ...mapState(['comments'])
  },
  methods: {
    ...mapActions(['fetchComments']),
    addComment() {
      // 这里可以替换为您的API端点
      axios.post('/api/comments', this.newComment)
        .then(() => {
          this.newComment.author = '';
          this.newComment.content = '';
          this.fetchComments();
        })
        .catch(error => {
          console.error('Error posting comment:', error);
        });
    }
  },
  created() {
    this.fetchComments();
  }
};
</script>

5. 在主组件中使用评论组件

<!-- App.vue -->
<template>
  <div id="app">
    <comment-component></comment-component>
  </div>
</template>

<script>
import CommentComponent from './components/Comment.vue';
import store from './store';

export default {
  name: 'App',
  components: {
    CommentComponent
  },
  store
};
</script>

后端实现:Spring Boot

1. 创建Spring Boot项目

使用Spring Initializr创建一个新的Spring Boot项目,并添加Spring Web依赖。

2. 创建评论实体

// Comment.java
package com.example.commentssystem.entity;

public class Comment {
    private Long id;
    private String author;
    private String content;
    // getters and setters
}

3. 创建评论服务

// CommentService.java
package com.example.commentssystem.service;

import com.example.commentssystem.entity.Comment;
import com.example.commentssystem.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentService {
    @Autowired
    private CommentRepository commentRepository;

    public List<Comment> getAllComments() {
        return commentRepository.findAll();
    }

    public Comment addComment(Comment comment) {
        return commentRepository.save(comment);
    }

    // Additional CRUD operations
}

4. 创建评论控制器

”`java // CommentController.java package com.example.commentssystem.controller;

import java.util