首页 > Java > java教程 > 正文

Java中如何创建一个小型在线投票平台

P粉602998670
发布: 2025-11-12 19:19:02
原创
651人浏览过
答案是使用Spring Boot快速搭建在线投票平台,通过Spring MVC处理请求,JPA操作MySQL数据库,Thymeleaf渲染页面,实现投票主题展示、选项提交与结果统计功能。

java中如何创建一个小型在线投票平台

要创建一个小型在线投票平台,Java可以结合Spring Boot、Thymeleaf(或JSP)、数据库(如MySQL)来快速搭建。整个系统包括用户发起投票、查看选项、提交投票和查看结果等功能。下面是一个简单可行的实现思路。

1. 项目结构与技术选型

使用Spring Boot可以快速构建Web应用,简化配置。主要技术如下:

  • 后端框架: Spring Boot + Spring MVC + Spring Data JPA
  • 前端模板: Thymeleaf(或HTML+Ajax)
  • 数据库: MySQL 或 H2(开发阶段可用内存数据库)
  • 构建工具 Maven 或 Gradle

创建Maven项目,在pom.xml中引入关键依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>
登录后复制

2. 数据模型设计

定义两个核心实体类:VoteTopic(投票主题)和VoteOption(选项)。

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

VoteTopic.java

百度作家平台
百度作家平台

百度小说旗下一站式AI创作与投稿平台。

百度作家平台 146
查看详情 百度作家平台
@Entity
public class VoteTopic {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    @OneToMany(mappedBy = "topic", cascade = CascadeType.ALL)
    private List<VoteOption> options = new ArrayList<>();

    // getter 和 setter
}
登录后复制

VoteOption.java

@Entity
public class VoteOption {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String optionText;
    private int voteCount = 0;

    @ManyToOne
    @JoinColumn(name = "topic_id")
    private VoteTopic topic;

    // getter 和 setter
}
登录后复制

3. 控制器处理请求

编写VoteController处理页面跳转和投票逻辑。

@Controller
public class VoteController {

    @Autowired
    private VoteTopicRepository topicRepo;

    @GetMapping("/vote/{id}")
    public String showVotePage(@PathVariable Long id, Model model) {
        VoteTopic topic = topicRepo.findById(id).orElse(null);
        if (topic == null) return "error";
        model.addAttribute("topic", topic);
        return "vote_page"; // 对应 templates/vote_page.html
    }

    @PostMapping("/vote/submit")
    public String submitVote(@RequestParam Long optionId) {
        Optional<VoteOption> opt = optionRepo.findById(optionId);
        if (opt.isPresent()) {
            VoteOption option = opt.get();
            option.setVoteCount(option.getVoteCount() + 1);
            optionRepo.save(option);
        }
        return "redirect:/result/" + option.getTopic().getId();
    }

    @GetMapping("/result/{id}")
    public String showResult(@PathVariable Long id, Model model) {
        VoteTopic topic = topicRepo.findById(id).orElse(null);
        model.addAttribute("topic", topic);
        return "result_page";
    }
}
登录后复制

4. 前端页面展示(Thymeleaf示例)

resources/templates/vote_page.html中显示投票项:

<h2 th:text="${topic.title}"></h2>
<form action="/vote/submit" method="post">
    <div th:each="option : ${topic.options}">
        <input type="radio" th:value="${option.id}" name="optionId" required>
        <label th:text="${option.optionText}"></label><br>
    </div>
    <button type="submit">提交投票</button>
</form>
登录后复制

结果页result_page.html显示统计:

<h2>投票结果:<span th:text="${topic.title}" /></h2>
<ul>
    <li th:each="option : ${topic.options}">
        <span th:text="${option.optionText}" /> : <strong th:text="${option.voteCount}" />票
    </li>
</ul>
<a href="/vote/${topic.id}">返回投票</a>
登录后复制

基本上就这些。启动类加上@SpringBootApplication,配置好数据库连接(application.properties),运行即可访问。

以上就是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号