gpt4 book ai didi

java - 为什么不使用click参数(Spring boot和vaadin)

转载 作者:行者123 更新时间:2023-12-01 18:41:24 25 4
gpt4 key购买 nike

我创建了一个聊天,但无法发送消息,但消息保存在 MySQL 数据库中。我认为问题是我的 Click and Event 参数未使用 enter image description here

您可以检查代码。我写的一切都是正确的,也许我犯了一个错误。我使用 Spring Boot 和 Vaadin Framework 14。我点击发送按钮,但它对我不起作用,但它会将发送者的姓名和消息保存在数据库中

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Key;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.dependency.StyleSheet;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.html.Paragraph;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.page.Push;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.PWA;
import org.springframework.beans.factory.annotation.Autowired;
import org.vaadin.marcus.spring.config.MessagesInfoManager;
import org.vaadin.marcus.spring.config.MessageConfigurator;
import org.vaadin.marcus.spring.model.Message;
import org.vaadin.marcus.spring.model.MessageInfo;
import org.vaadin.marcus.spring.service.RestService;

import java.util.LinkedHashMap;
import java.util.List;
import org.springframework.scheduling.annotation.Scheduled;

@StyleSheet("frontend://styles/styles.css")
@Route
@PWA(name = "Vaadin MessagesInfoManager", shortName = "Vaadin MessagesInfoManager")
@Push
public class MainView extends VerticalLayout {
private final MessagesInfoManager messagesInfoManager;
private final RestService restService;
private String username;
private TextField textField;
private Message messageLast;


@Autowired
public MainView(RestService restService) {
this.messagesInfoManager = MessageConfigurator.getInstance().getChatMessagesInfoManager();
addClassName("main-view");
setSizeFull();
setDefaultHorizontalComponentAlignment(Alignment.CENTER);

H1 header = new H1("Vaadin Chat");
header.getElement().getThemeList().add("dark");

add(header);

askUsername();
this.restService = restService;
}

private void askUsername() {
HorizontalLayout layout = new HorizontalLayout();
TextField usernameField = new TextField();
Button startButton = new Button("Start chat");

layout.add(usernameField, startButton);

startButton.addClickListener(click -> { // Click parametr not used(Why?)
username = usernameField.getValue();
remove(layout);
showChat(username);
});

add(layout);
}

private void showChat(String username) {
MessageList messageList = new MessageList();

List<Message> lasts = restService.getLast();
for (Message message : lasts) {
messageList.add(new Paragraph(message.getFromV() + ": " + message.getMessageV()));
}

messageLast = lasts.get(lasts.size() - 1);

add(messageList, createInputLayout(username, messageList));
expand(messageList);
}

private Component createInputLayout(String username, MessageList messageList) {
HorizontalLayout layout = new HorizontalLayout();
layout.setWidth("100%");

TextField messageField = new TextField();
messageField.addKeyDownListener(Key.ENTER, keyDownEvent -> sender(messageField, messageList));
Button sendButton = new Button("Send");
sendButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);

layout.add(messageField, sendButton);
layout.expand(messageField);

messageField.addFocusListener(event -> {
for (Message message : messagesInfoManager.getMessagesByUI(getUI())) {
if (!message.getFromV().equals(username)) {
message.setUnread(false);
this.restService.updateMessage(message.getId(), message);
}
}
});

sendButton.addClickListener(click -> sender(messageField, messageList)); // Click parametr not used(Why?)
messageField.focus();

return layout;
}

private void sender(TextField textField, MessageList messageList) {
Message message = new Message(username, textField.getValue());
message = restService.saveMessage(message);
messageLast = message;
this.textField = textField;
// messagesInfoManager.updateMessageUIInfo(new MessageInfo(messageList, message, this));
textField.clear();
textField.focus();

}

@Scheduled(fixedDelay = 1000)
public void scheduleFixedDelayTask() {

MessageList messageList = new MessageList();

List<LinkedHashMap> lasts = restService.getUnreadMessages(messageLast);

for (LinkedHashMap message : lasts) {
messageList.add(new Paragraph(message.get("fromV") + ": " + message.get("messageV")));
}

messagesInfoManager.updateMessageUIInfo(new MessageInfo(messageList, messageLast, this));

}

}

最佳答案

如果它将其保存到数据库,那么显然该监听器可以工作。未使用 eventclick 参数,因为您没有使用它们,这很好。

在发送消息时,您没有将消息添加到 MessageList 中,也没有以任何其他方式刷新列表,因此消息无法显示。

关于java - 为什么不使用click参数(Spring boot和vaadin),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59925746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com