答案:使用Spring Boot开发活动报名小程序,包含活动展示、用户报名和数据存储功能。1. 采用Spring Web、JPA、Thymeleaf和H2/MySQL构建项目;2. 定义Activity和Participant实体类;3. 通过JPA实现数据访问接口;4. 编写Controller处理页面跳转与报名逻辑;5. 使用Thymeleaf创建前端页面展示活动列表与报名表单;6. 配置application.properties启用H2数据库并开启控制台;7. 可选初始化测试数据。运行应用后访问首页即可浏览活动并完成报名,适合学习扩展。

开发一个简易的活动报名小程序,可以用 Java 搭配 Spring Boot 快速实现。这个程序包含用户报名、活动展示和数据存储功能,适合学习或小型项目使用。以下是具体实现步骤。
使用 Spring Boot 构建后端服务,搭配 Thymeleaf 做简单页面展示(也可用前后端分离方式),数据库使用 H2 或 MySQL。
主要依赖:
定义活动的基本信息,如名称、时间、地点、报名人数限制等。
立即学习“Java免费学习笔记(深入)”;
@Entity
public class Activity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String description;
    private LocalDateTime time;
    private String location;
    private Integer maxParticipants;
    private Integer currentParticipants = 0;
    // 构造方法、getter 和 setter 省略
}
记录报名用户的姓名、联系方式等信息,并关联到具体活动。
@Entity
public class Participant {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String phone;
    
    @ManyToOne
    @JoinColumn(name = "activity_id")
    private Activity activity;
    // 构造方法、getter 和 setter 省略
}
使用 Spring Data JPA 快速实现数据库操作。
public interface ActivityRepository extends JpaRepository<Activity, Long> {}
public interface ParticipantRepository extends JpaRepository<Participant, Long> {
    List<Participant> findByActivityId(Long activityId);
}
创建 Controller 处理页面跳转和报名请求。
@Controller
public class ActivityController {
    @Autowired
    private ActivityRepository activityRepo;
    @Autowired
    private ParticipantRepository participantRepo;
    // 展示所有活动
    @GetMapping("/")
    public String listActivities(Model model) {
        model.addAttribute("activities", activityRepo.findAll());
        return "index";
    }
    // 报名页面
    @GetMapping("/signup/{id}")
    public String showSignupForm(@PathVariable Long id, Model model) {
        Activity activity = activityRepo.findById(id).orElse(null);
        if (activity == null || activity.getCurrentParticipants() >= activity.getMaxParticipants()) {
            return "error";
        }
        model.addAttribute("activity", activity);
        model.addAttribute("participant", new Participant());
        return "signup";
    }
    // 提交报名
    @PostMapping("/signup/{id}")
    public String registerParticipant(@PathVariable Long id,
                                      @ModelAttribute Participant participant,
                                      Model model) {
        Activity activity = activityRepo.findById(id).orElse(null);
        if (activity == null || activity.getCurrentParticipants() >= activity.getMaxParticipants()) {
            model.addAttribute("message", "报名失败:活动已满或不存在");
            return "result";
        }
        participant.setActivity(activity);
        participantRepo.save(participant);
        activity.setCurrentParticipants(activity.getCurrentParticipants() + 1);
        activityRepo.save(activity);
        model.addAttribute("message", "报名成功!");
        return "result";
    }
}
在 resources/templates/ 下创建 HTML 页面。
以 index.html 为例:
<div th:each="act : ${activities}">
  <h3 th:text="${act.name}" />
  <p th:text="${act.description}" />
  <p>时间: <span th:text="${#temporals.format(act.time, 'yyyy-MM-dd HH:mm')}" /></p>
  <p>地点: <span th:text="${act.location}" /></p>
  <p>人数: <span th:text="${act.currentParticipants}" />/<span th:text="${act.maxParticipants}" /></p>
  <a th:href="@{/signup/{id}(id=${act.id})}">报名</a>
</div>
使用 H2 数据库便于本地测试:
spring.datasource.url=jdbc:h2:mem:activitydb spring.datasource.driver-class-name=org.h2.Driver spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.jpa.hibernate.ddl-auto=create-drop spring.h2.console.enabled=true
打开 http://localhost:8080/h2-console 可查看数据。
在启动类中添加初始活动:
@PostConstruct
public void init() {
    Activity act = new Activity();
    act.setName("Java技术分享会");
    act.setDescription("介绍Spring Boot实战技巧");
    act.setTime(LocalDateTime.now().plusDays(7));
    act.setLocation("科技大厦3楼会议室");
    act.setMaxParticipants(30);
    activityRepo.save(act);
}
以上就是Java如何开发一个简易的活动报名小程序的详细内容,更多请关注php中文网其它相关文章!
 
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
 
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号