将代码添加到html head标签的Vaadin 8方法是什么?

时间:2017-10-07 17:17:23

标签: javascript vaadin vaadin8

Other SO answers建议覆盖#include <iostream> #include <chrono> struct clock {}; int main() { clock c; (void)c; } // Error: main.cpp:37:7: error: ‘template<class duration_tt> struct clock’ redeclared as different kind of symbol class clock ^~~~~ In file included from /usr/include/pthread.h:24:0, from /usr/include/x86_64-linux-gnu/c++/6/bits/gthr-default.h:35, from /usr/include/x86_64-linux-gnu/c++/6/bits/gthr.h:148, from /usr/include/c++/6/ext/atomicity.h:35, from /usr/include/c++/6/bits/ios_base.h:39, from /usr/include/c++/6/ios:42, from /usr/include/c++/6/ostream:38, from /usr/include/c++/6/iostream:39, from main.cpp:1: /usr/include/time.h:189:16: note: previous declaration ‘clock_t clock()’ extern clock_t clock (void) __THROW; ,但我无法在Vaadin 8中找到这些类和方法。

我在ApplicationServlet.writeAjaxPageHtmlHeadercom.vaadin.server.VaadinServlet找不到任何相似内容。

com.vaadin.ui.UI注释,但是如果我把它放在我的UI类上,脚本将被加载到我的应用程序的每个页面。我只在一个特定页面上需要它。

2 个答案:

答案 0 :(得分:4)

最初的HTML页面在Vaadin中称为bootstrap页面。有一些文档提示您在Book of Vaadin中找到正确的方向。

在Vaadin 8中,您需要将BootstrapListener添加到会话中。您可以通过在VaadinServlet中添加SessionInitListener来获取创建的会话。

注册会话

此示例使用Vaadin和Spring Boot,但在不使用Spring Boot时也适用相同的原则。

@Component("vaadinServlet")
@WebServlet(urlPatterns = "/*", name = "BootstrapVaadinServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = BoostrapUi.class, productionMode = false)
public class BootstrapVaadinServlet extends SpringVaadinServlet {
    private static final Logger logger = LoggerFactory.getLogger(BootstrapVaadinServlet.class);
    @Override
    protected void servletInitialized() throws ServletException {
        super.servletInitialized();
        getService().addSessionInitListener(this::addBoostrapListenerOnSessionInit);
    }

    private void addBoostrapListenerOnSessionInit(SessionInitEvent sessionInitEvent) {
        sessionInitEvent.getSession().addBootstrapListener(new AppBootstrapListener());
    }
}

实施html头标记修改

public class AppBootstrapListener implements BootstrapListener {
    @Override
    public void modifyBootstrapFragment(BootstrapFragmentResponse bootstrapFragmentResponse) {

    }

    @Override
    public void modifyBootstrapPage(BootstrapPageResponse res) {
        Elements headTags = res.getDocument().getElementsByTag("head");
        Element head = headTags.get(0);
        head.appendChild(metaExample(res.getDocument()));
    }

    private Node metaExample(Document document) {
        Element meta = document.createElement("meta");
        meta.attr("author", "Me");
        return meta;
    }
}

答案 1 :(得分:0)

如果使用插件没问题,请尝试HeaderTags

  

概述

     

使用此附加组件,您可以定义要添加到主机页面的标记   在UI类中添加注释。

来自add ons用法示例的示例

@MetaTags({
   // Replaces the Vaadin X-UA-Compatible header
   @Meta(httpEquiv = "X-UA-Compatible", content = "hello"),
   @Meta(name = "test", content = "test") })

// And showing how to create a link tag as well
@Link(rel = "foobar", href = "about:blank")
public class DemoUI extends UI {
    ...
}