Vaadin - 用HTML包装任意组件

时间:2017-06-12 18:03:16

标签: vaadin

我想用HTML包装一个Component,让我们说一个“a”。是否有捷径可寻? PS。我不想添加ClickListener。

类似的东西:

CssLayout layout = new CssLayout();
SomeHtmlElement html = new SomeHtmlElement("a");
html.addAttribute("href","http://stackoverflow.com");
html.addComponent(layout);

1 个答案:

答案 0 :(得分:1)

你可以使用 CustomLayout来 将组件放入任意HTML环境(See also the Book of Vaadin)

创建该HTML不是Vaadin的直接部分。所以你必须选择一些 第三方图书馆。出于示例的目的使用 Jsoup,因为它包含在Vaadin服务器中 允许DOM操作(我不会使用):

// run with `spring run --watch <file>.groovy`
@Grab('com.vaadin:vaadin-spring-boot-starter:2.0.1')

import com.vaadin.ui.*
import com.vaadin.ui.themes.*
import com.vaadin.shared.*

@com.vaadin.spring.annotation.SpringUI
@com.vaadin.annotations.Theme("valo")
class MyUI extends UI {
        protected void init(com.vaadin.server.VaadinRequest request) {
        // create some HTML and provide a "place-holder" via `data-location`
        def doc = org.jsoup.Jsoup.parse('<main><h1>Hello World</h1><p><span data-location="button"/></p></main>')
        def button = new Button("Hello World", { Notification.show("Hello World") } as Button.ClickListener)
        // read the CustomLayout from an InputStream
        def c = new CustomLayout(new ByteArrayInputStream(doc.toString().getBytes('UTF-8')))
        // place the button in the CustomLayout via its name chosen by `data-location`
        c.addComponent(button, 'button')
        content=c
    }
}