基于:Vaadin 和 Spring Boot 创建的简单待办事项(Todo)应用程序的 Java 类。以下是代码的详细解释:
package org.vaadin.marcus.spring;//声明包这行代码声明了该类属于 org.vaadin.marcus.spring 包。
import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.html.H4;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.*;
import com.vaadin.flow.server.VaadinService;
import javax.servlet.http.Cookie;
import java.awt.*;
@Route("/")
//定义一个名为TODO的公共类,继承VerticalLayout的并实现了afternavigationobserver接口
public class Todo extends VerticalLayout implements AfterNavigationObserver{
private VerticalLayout totoList = new VerticalLayout();
private TextField todoField = new TextField();
public Todo() {
Button addButton = new Button ("Add");
addButton.addClickListener((this::onAdd));
add(new H1("TODO"),new H4("HELLO"+getUserName()),todoField,new HorizontalLayout(todoField,addButton));
}
public String getUserName() {
for (Cookie cookie : VaadinService.getCurrentRequest().getCookies()) {
if (cookie.getName().equals("username")) {
return cookie.getValue();
}
}
return "";
}
/*当 "Add" 按钮被点击时,这个方法会被调用。它从 todoField 中获取值,
创一个复选框,并将其添加到 totoList 布局中。但是,这里有一个问题:
它只是添加复选框的字符串表示形式,而不是实际的复选框组件。
你可能需要修改此方法以正确添加复选框组件。*/
public void onAdd(ClickEvent event){
String todoVal = todoField.getValue();
Checkbox checkbox = new Checkbox(todoVal);
totoList.add(String.valueOf(checkbox));
}
@Override
public void afterNavigation(AfterNavigationEvent afterNavigationEvent) {
for (Cookie cookie : VaadinService.getCurrentRequest().getCookies()) {
if (cookie.getName().equals("username")){
return;
}
}
getUI().ifPresent(ui -> {
ui.navigate("/login.html");
});
}
}
package org.vaadin.marcus.spring;
import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.checkbox.Checkbox;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.html.H4;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.*;
import com.vaadin.flow.server.VaadinService;
import javax.servlet.http.Cookie;
import javax.swing.*;
@Route("/")
public class Todo extends VerticalLayout implements AfterNavigationObserver{
private VerticalLayout totoList = new VerticalLayout();
private TextField todoField = new TextField();
public Todo() {
Button addButton = new Button("Add");
addButton.addClickListener((this::onAdd));
add(new H1("TODO")new H4("HELLO WORLD"+getUserName()),totoList,new HorizontalLayout(todoField,addButton));
}
public String getUserName(){
for (Cookie cookie : VaadinService.getCurrentRequest().getCookies()) {
if (cookie.getName().equals("username")){
return cookie.getValue();
}
}
return"";
}
public void onAdd(ClickEvent event){
String todoVal = todoField.getValue();
Checkbox checkbox = new Checkbox(todoVal);
totoList.add(checkbox);
}
@Override
public void afterNavigation(AfterNavigationEvent afterNavigationEvent) {
for (Cookie cookie : VaadinService.getCurrentRequest().getCookies()) {
if (cookie.getName().equals("username")){
return;
}
}
getUI().ifPresent(ui -> {
ui.navigate("/login.html");
});
}
}
什么是持久化?
持久化是指将程序的数据存储到非易失性存储介质(如硬盘、数据库)中,以便在程序关闭后仍能保留。这能够确保数据在应用程序重启时仍然可用。持久化是在软件开发中一个重要的概念,特别是当需要长期存储和检索数据时。
在编程领域,持久化通常涉及将程序中的数据结构或对象保存到永久性存储中,以便在程序重新启动或在不同的执行环境中能够恢复这些数据。这有助于确保应用程序在运行过程中创建的数据不会丢失。
常见的持久化方法包括:
- 文件系统持久化: 将数据保存到文件中,可以是文本文件、二进制文件或其他格式。这是一种简单的方法,适用于小规模的数据。
- 数据库持久化: 使用数据库管理系统(如MySQL、PostgreSQL、MongoDB等)将数据存储在数据库中。这种方式适用于大规模和复杂的数据集,同时提供了查询和事务支持。
- 对象持久化: 将对象的状态保存到永久存储中,以便在程序重启时能够还原对象。这通常通过对象关系映射(ORM)框架来实现,例如Hibernate。
- 缓存持久化: 将数据保存到缓存中,以提高访问速度。这并不是真正的永久性存储,但可以提高数据的读取性能。
持久化的主要目标是确保数据的长期存储和可恢复性。这对于应用程序的稳定性和可靠性非常重要,尤其是在需要处理大量用户数据或重要业务数据的情况下。
//首先保证添加了依赖
//默认添加过依赖了
<!-- pom.xml -->
<!-- 添加 Spring Boot Starter Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加 Spring Data JPA 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 添加 H2 数据库(或其他你喜欢的数据库) -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
//然后,创建一个实体类表示任务项,以及一个JPA Repository用于数据库操作。
// Task.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String description;
// Constructors, getters, setters
}
// TaskRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
public interface TaskRepository extends JpaRepository<Task, Long> {
}
//修改Todo类方便使用数据库来存储和检索任务项
// Todo.java
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.component.checkbox.Checkbox;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.html.H4;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.AfterNavigationEvent;
import com.vaadin.flow.router.AfterNavigationObserver;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.spring.annotation.UIScope;
import com.vaadin.flow.server.VaadinService;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.Cookie;
@Route("/")
@UIScope
public class Todo extends VerticalLayout implements AfterNavigationObserver {
private final VerticalLayout totoList = new VerticalLayout();
private final TextField todoField = new TextField();
// Inject TaskRepository
@Autowired
private final TaskRepository taskRepository;
public Todo(TaskRepository taskRepository) {
this.taskRepository = taskRepository;
Button addButton = new Button("Add");
addButton.addClickListener(this::onAdd);
add(new H1("TODO"), new H4("HELLO WORLD " + getUserName()), totoList,
new HorizontalLayout(todoField, addButton));
loadTasks(); // Load tasks from the database on initialization
}
private void loadTasks() {
// Retrieve tasks from the database and display them
taskRepository.findAll().forEach(task -> {
Checkbox checkbox = new Checkbox(task.getDescription());
totoList.add(checkbox);
});
}
public String getUserName() {
for (Cookie cookie : VaadinService.getCurrentRequest().getCookies()) {
if (cookie.getName().equals("username")) {
return cookie.getValue();
}
}
return "";
}
public void onAdd(ClickEvent<Button> event) {
String todoVal = todoField.getValue();
// Save the task to the database
Task newTask = new Task();
newTask.setDescription(todoVal);
taskRepository.save(newTask);
// Display the task
Checkbox checkbox = new Checkbox(todoVal);
totoList.add(checkbox);
// Clear the input field
todoField.clear();
}
@Override
public void afterNavigation(AfterNavigationEvent afterNavigationEvent) {
for (Cookie cookie : VaadinService.getCurrentRequest().getCookies()) {
if (cookie.getName().equals("username")) {
return;
}
}
getUI().ifPresent(ui -> ui.navigate("/login.html"));
}
}