【问题标题】:Vaadin: set text from TextField to Label after Button clickVaadin:在按钮单击后将文本从 TextField 设置为 Label
【发布时间】:2014-04-15 00:26:13
【问题描述】:

我正在尝试编写简单的 Vaadin 表单。我想在用户单击按钮后将文本从 TextField 写入标签。

我有这个代码:

package com.example;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

public class TestForm extends UI {

    @WebServlet(value = "/TestForm", asyncSupported = true)
    @VaadinServletConfiguration(productionMode = false, ui = TestForm.class)
    public static class Servlet extends VaadinServlet {
    }

    private TextField tf;
    private Label lbl;
    private Button btn;

    @Override
    protected void init(VaadinRequest request) {
        final VerticalLayout layout = new VerticalLayout();
        layout.setMargin(true);
        setContent(layout);

        tf = new TextField("Enter text");
        lbl = new Label();
        btn = new Button("Click");    

        btn.addClickListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                lbl.setCaption(tf.getValue());
            }
        });

        layout.addComponent(tf);
        layout.addComponent(btn);
        layout.addComponent(lbl);       

    }
}

但是当我点击按钮后标签没有发生,但是网络浏览器显示错误:

通讯问题 记下任何未保存的数据,然后单击此处继续。(SyntaxError) 堆栈:eval@[native code] Jda BR Zc Xc line: 340: Unexpected token '

我做错了什么?

【问题讨论】:

  • 我创建了一个测试项目,您的代码可以正常工作。控制台中是否有任何堆栈跟踪?
  • 在我写完答案后看到了 nexus 的评论。没想到自己尝试您的代码。如果你有一个堆栈跟踪,它确实很有用。

标签: java javascript vaadin onclicklistener


【解决方案1】:

编辑 问题是您的 web.xml 中有两个 servlet 映射。 我注释掉了其中一个:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="vaadinpokus" version="3.0">

  <display-name>vaadintest</display-name>
  <description>...</description>
  <context-param>
      <description>Vaadin production mode</description>
      <param-name>productionMode</param-name>
      <param-value>false</param-value>
  </context-param>
  <servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
    <init-param>
          <param-name>UI</param-name>
          <param-value>com.example.MyVaadinUI</param-value>
    </init-param>
    <async-supported>true</async-supported>
  </servlet>

  <servlet-mapping>
    <servlet-name>myservlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

  <!--<servlet-mapping>
    <servlet-name>myservlet</servlet-name>
    <url-pattern>/VAADIN/*</url-pattern>
  </servlet-mapping>-->
</web-app>

现在应该可以了。 感谢您分享您的代码。这种方式更容易调试应用程序。

【讨论】:

  • 我试试这个,但还是不行。有我的项目代码dl.dropboxusercontent.com/u/6943408/vaadintest.zip你能看一下吗?单击按钮后,我收到此错误db.tt/RpWcbMCA
  • 我回家后看看
  • 我尝试在另一台计算机上编译项目并使用jetty而不是tomcat,但我得到了同样的错误:-(
  • 谢谢,现在可以了!我必须在 web.xml 或类中只有一个 servlet。 |但是如何制作 2 个单独的页面?在经典 JSP 中我有多个 servlet,在 Spring 中我有多个控制器。
  • 我找到了 - 如果我需要多个窗口,最好的解决方案可能是使用 Navigator vaadin.com/book/vaadin7/-/page/advanced.navigator.html
【解决方案2】:

您应该尝试 lbl.setValue 而不是 lbl.setCaption

btn.addClickListener(new Button.ClickListener() {
     public void buttonClick(ClickEvent event) {
         lbl.setValue(tf.getValue());
     }
 });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 2020-03-31
    • 2018-05-04
    • 2021-03-27
    • 1970-01-01
    相关资源
    最近更新 更多