在事件触发后,我从 TextArea 收到的值始终相同,如果我在按下发送按钮之前更改该值,则它可以正常工作,在按下按钮后,它会停留在我设置的最后一个值上。
VaadinUI:
@Override
public void init(VaadinRequest request) {
…
// build json mock data layout structure and connect field to processo jsonRequestTextArea.setValue(mockJsonData);
HorizontalLayout mainLayout = new HorizontalLayout(jsonRequestTextArea, actions);
processor.setJsonRequesttextArea(jsonRequestTextArea);
// add listener for process button
sendRequestButton.addClickListener(e -> processor.processRequest());
}
处理器:
public void processRequest() {
…
// set json content paramenter
parametersMap.add(":contentFile", jsonRequesttextArea.getValue());
}
我按照此 Site 上找到的示例进行操作 代码可以从 git 克隆:
git clone https://github.com/spring-guides/gs-crud-with-vaadin.git
有没有办法始终从 TextArea 获取最后更新的文本?
最佳答案
没问题
通过调用“getValue”获取 TextArea
对象的当前内容时,我发现没有问题。
这是 Vaadin 8.3.0 中的完整示例应用程序。每次单击该按钮时,TextArea 的内容都会在前面添加文本“:contentFile”以及 UTC 中的当前日期时间。所以每次点击都是获取值,并设置一个新值;每次都可以正常工作,没有问题。
package com.basilbourque;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.*;
import java.time.Instant;
/**
* Testing retrieval of contents from a TextArea.
* <p>
* See: https://stackoverflow.com/q/48447476/642706
* <p>
* By Basil Bourque.
*/
@Theme ( "mytheme" )
public class MyUI extends UI
{
static final long serialVersionUID = 42L;
@Override
protected void init ( VaadinRequest vaadinRequest )
{
// Init layout.
final VerticalLayout layout = new VerticalLayout();
layout.setHeight( 100 , Unit.PERCENTAGE );
// Widgets
TextArea textArea = new TextArea( "TextArea" );
textArea.setWidth( 100F , Unit.PERCENTAGE );
textArea.setHeight( 100F , Unit.PERCENTAGE );
String mode = textArea.getValueChangeMode().toString(); // `ValueChangeMode` enum. https://vaadin.com/download/release/8.3/8.3.0/docs/api/com/vaadin/shared/ui/ValueChangeMode.html
textArea.setValue( "ValueChangeMode: " + mode );
Button processButton = new Button( "Process" );
processButton.addClickListener( clickEvent ->
{
String x = ":contentFile=" + Instant.now() + "\n" + textArea.getValue();
textArea.setValue( x );
textArea.setCursorPosition( 0 ); // Move insertion-point to top of the field for easy data-entry.
} );
// Arrange layout.
layout.addComponents( textArea , processButton );
setContent( layout );
}
@WebServlet ( urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true )
@VaadinServletConfiguration ( ui = MyUI.class, productionMode = false )
public static class MyUIServlet extends VaadinServlet
{
static final long serialVersionUID = 42L;
}
}
猜想
您的问题写得不清楚,并且缺乏足够的细节和信息。但似乎您在感知 TextArea
的值更改方面遇到了延迟。
我怀疑您可能正在从另一个线程访问 TextArea
的值。
如果是这样,您必须记住,(a) 用户在 Web 浏览器客户端中键入文本作为数据输入,与 (b) 键入的文本到达服务器端之间可能存在延迟。存储在实际小部件的状态中。
您的 Vaadin 应用程序实际上位于 Java 的服务器上,其 UI 由 Vaadin 框架客户端使用 HTML、CSS、JavaScript 等在 Web 浏览器中自动远程呈现。该客户端框架接受用户的键入和自动将其传递回服务器端以存储在 Java 对象(TextArea
实例)中。
但是服务器端对象多久会根据用户的输入进行更新?涉及多少延迟?这取决于 ValueChangeMode
您可以使用方法 TextArea::setValueChangeMode
指定和 TextArea::getValueChangeMode
。常量BLUR
、EAGER
、LAZY
、 和 TIMEOUT 各自规定了不同的计时。默认值为 LAZY
,记录为:
On every user event, schedule a server-side event after a defined interval, cancelling the currently-scheduled event if any. This is a good choice if you want to, for instance, wait for a small break in the user's typing before sending the event.
要加快将用户输入传送到服务器端的速度,请将模式更改为 EAGER
以便几乎在每次击键时进行更新。但这会增加网络流量,使您的应用程序更加“喋喋不休”。在互联网连接较差的情况下效果不佳,但在本地网络上可能没问题。
要降低将用户输入移动到服务器端的频率,请选择BLUR
,以便仅在键盘焦点离开该字段并移动到另一个字段或其他小部件后才传递输入。
因此,当您访问 TextArea
实例的当前值时,它实际上可能不是那么当前。当用户键入时,新文本可能尚未通过网络从浏览器端 HTML/JavaScript 传输到服务器端 Java 对象。
顺便说一句,如果从另一个线程使用 TextArea
,请确保通过 UI.access
正确执行此操作。和 VaadinSession.access
方法。切勿从后台线程直接访问用户界面小部件。
关于java - Vaadin 文本区域已过时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48447476/