简体   繁体   English

我无法在 Vaadin 中将 int 转换为字符串

[英]I can't convert int to string in Vaadin

I have a small problem with Vaadin and Spring Boot.我对 Vaadin 和 Spring Boot 有一个小问题。 When I want to change convert int to string I get this error.当我想将 convert int 更改为 string 时,出现此错误。 I know it's probably a trivial problem but I'm just starting to learn Spring Boot.我知道这可能是一个微不足道的问题,但我才刚刚开始学习 Spring Boot。

com.vaadin.flow.component.textfield.IntegerField@1c7a3814 com.vaadin.flow.component.textfield.IntegerField@1c7a3814

@Route("main")
public class vaadinapp extends VerticalLayout {
  public vaadinapp() {
    IntegerField integerField = new IntegerField("Age");
    Button buttonName = new Button("Left", new Icon(VaadinIcon.ACADEMY_CAP));
    Label labelName = new Label();

    String integerFieldStr = integerField.toString();

    buttonName.addClickListener(clickEvent -> {
      labelName.setText(integerFieldStr);
    });

    add(integerField, buttonName, labelName);
  }
}

Invoking the toString method of the integerField object will return you string representation of the object, not the value, stored in it.调用integerField object 的toString方法将返回 object 的字符串表示形式,而不是存储在其中的值。 To retrieve the value you could use the getValue method instead.要检索值,您可以改用getValue方法。 Like this:像这样:

public vaadinapp() {
    IntegerField integerField = new IntegerField("Age");
    Button buttonName = new Button("Left", new Icon(VaadinIcon.ACADEMY_CAP));
    Label labelName = new Label();

    buttonName.addClickListener(clickEvent -> {
        String integerFieldStr = 
            Optional.ofNullable(integerField.getValue()) //you need this check since getValue could return null
           .map(String::valueOf)
           .orElse("");
        labelName.setText(integerFieldStr);
    });

    add(integerField, buttonName, labelName);
}

UPD: I'd also moved the value retrieval code into the listener. UPD:我还将值检索代码移动到侦听器中。 Thanks Eric for this point.感谢埃里克的这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM