我正在尝试编写简单的 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) stack: eval@[native code] Jda BR Zc Xc line: 340: Unexpected token '<' - Original JSON-text: html>
我做错了什么?
最佳答案
编辑 问题是您的 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>
现在应该可以了。 感谢您分享您的代码。通过这种方式调试应用程序会更容易。
关于java - Vaadin:单击按钮后将文本字段中的文本设置为标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23072623/