首页 > Java > java教程 > 正文

如何用Java开发小型在线问答系统

P粉602998670
发布: 2025-10-11 19:39:01
原创
359人浏览过
答案是使用Java的Spring Boot结合Thymeleaf、JPA和H2/MySQL实现在线问答系统,包含提问、回答、查看列表等功能,通过实体类Question和Answer建模,Controller处理请求,Thymeleaf渲染页面,项目结构清晰,适合初学者快速上手开发基础Web应用。

如何用java开发小型在线问答系统

开发一个小型在线问答系统可以用Java结合常见的Web技术来实现。整个系统需要支持用户提问、回答问题、查看问题列表等基本功能。以下是具体实现思路和步骤,适合初学者或中级开发者参考。

1. 系统功能设计

明确核心功能模块,便于后续开发:

  • 用户提问:用户输入标题和内容发布问题
  • 浏览问题列表:按发布时间排序展示所有问题
  • 回答问题:用户对某个问题提交回答
  • 查看问题详情:显示问题及其所有回答

不需要用户登录功能时可省略身份验证,简化开发。

2. 技术选型与架构

使用轻量级Java Web技术快速搭建:

立即学习Java免费学习笔记(深入)”;

  • 后端框架:Spring Boot(自动配置,内嵌Tomcat)
  • 前端页面:Thymeleaf 模板引擎(简单易用)
  • 数据存储:H2 或 MySQL 数据库 + JPA(方便对象映射)
  • 构建工具:Maven

这种组合适合小型项目,开发效率高,部署简单。

3. 核心代码结构

在Spring Boot项目中建立以下主要类:

实体类 Question.java

代表一个问题,包含id、标题、内容、提问时间、回答集合:

问问小宇宙
问问小宇宙

问问小宇宙是小宇宙团队出品的播客AI检索工具

问问小宇宙 77
查看详情 问问小宇宙
@Entity
public class Question {
    @Id @GeneratedValue
    private Long id;
    private String title;
    private String content;
    private LocalDateTime createdAt = LocalDateTime.now();

    @OneToMany(mappedBy = "question", cascade = CascadeType.ALL)
    private List<Answer> answers = new ArrayList<>();

    // getter 和 setter 省略
}
登录后复制
实体类 Answer.java

代表一个回答,关联到某个问题:

@Entity
public class Answer {
    @Id @GeneratedValue
    private Long id;
    private String content;
    private LocalDateTime createdAt = LocalDateTime.now();

    @ManyToOne
    @JoinColumn(name = "question_id")
    private Question question;

    // getter 和 setter
}
登录后复制
数据访问接口

使用JPA简化数据库操作:

public interface QuestionRepository extends JpaRepository<Question, Long> {}
public interface AnswerRepository extends JpaRepository<Answer, Long> {}
登录后复制
控制器 QuestionController.java

处理网页请求:

@Controller
public class QuestionController {

    @Autowired
    private QuestionRepository questionRepo;

    @Autowired
    private AnswerRepository answerRepo;

    // 显示所有问题
    @GetMapping("/")
    public String listQuestions(Model model) {
        model.addAttribute("questions", questionRepo.findAll());
        return "index";
    }

    // 显示提问表单
    @GetMapping("/ask")
    public String showForm(Model model) {
        model.addAttribute("question", new Question());
        return "ask";
    }

    // 提交问题
    @PostMapping("/ask")
    public String postQuestion(Question question) {
        questionRepo.save(question);
        return "redirect:/";
    }

    // 查看问题详情及回答
    @GetMapping("/question/{id}")
    public String viewQuestion(@PathVariable Long id, Model model) {
        Question q = questionRepo.findById(id).orElseThrow();
        model.addAttribute("question", q);
        model.addAttribute("answer", new Answer());
        return "detail";
    }

    // 提交回答
    @PostMapping("/answer")
    public String postAnswer(@RequestParam Long questionId,
                             @RequestParam String content) {
        Answer answer = new Answer();
        answer.setContent(content);
        Question q = questionRepo.findById(questionId).orElseThrow();
        answer.setQuestion(q);
        answerRepo.save(answer);
        return "redirect:/question/" + questionId;
    }
}
登录后复制

4. 前端页面(Thymeleaf)

创建HTML模板文件放在 src/main/resources/templates 目录下。

首页 index.html
<div th:each="q : ${questions}">
  <a th:href="https://www.php.cn/link/c9d0389bac9d234bd66c88ef7be066d0"><h3 th:text="${q.title}"></h3></a>
  <p th:text="${#strings.abbreviate(q.content, 100)}"/>
</div>
<a href="/ask">提出新问题</a>
登录后复制
提问页 ask.html
<form action="/ask" method="post">
  <input type="text" name="title" placeholder="标题" required />
  <textarea name="content" placeholder="详细描述" required></textarea>
  <button type="submit">提交问题</button>
</form>
登录后复制
详情页 detail.html

显示问题和所有回答,并提供回答表单:

<h2 th:text="${question.title}"></h2>
<p th:text="${question.content}"></p>

<div th:each="a : ${question.answers}">
  <p th:text="${a.content}"></p>
</div>

<form action="/answer" method="post">
  <input type="hidden" name="questionId" th:value="${question.id}" />
  <textarea name="content" placeholder="写下你的回答" required></textarea>
  <button type="submit">提交回答</button>
</form>
登录后复制

运行Spring Boot主类,访问 http://localhost:8080 即可使用系统。

基本上就这些。不复杂但容易忽略细节,比如时间显示格式、异常处理、表单验证等,后续可以逐步增强。这个基础版本能帮助理解Web应用的基本流程:请求 → 控制器 → 服务/数据 → 页面渲染。

以上就是如何用Java开发小型在线问答系统的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号