其他分享
首页 > 其他分享> > 如何在Vaadin中更改默认HTML模板

如何在Vaadin中更改默认HTML模板

作者:互联网

我正在使用Vaadin和maven应用程序.我想要的是更改默认的html模板.
当我运行应用程序时,生成的HTML如下所示:

<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=10;chrome=1">
<style type="text/css">html, body {height:100%;margin:0;}</style>
<link rel="shortcut icon" type="image/vnd.microsoft.icon" href="./VAADIN/themes/customBootstrap/favicon.ico">
<link rel="icon" type="image/vnd.microsoft.icon" href="./VAADIN/themes/customBootstrap/favicon.ico">
<link rel="stylesheet" type="text/css" href="./VAADIN/themes/customBootstrap/styles.css"><script type="text/javascript"
...

我想更改“元”属性,添加“链接”并添加一些其他组件,如:

<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

Vaadin有办法做到这一点吗?
谢谢!

解决方法:

创建一个扩展VaadinServlet的自定义servlet:

public class BootstrapServlet extends VaadinServlet {

private static final long serialVersionUID = 1L;

@Override
protected void servletInitialized() throws ServletException {
    super.servletInitialized();
    getService().addSessionInitListener(new SessionInitListener() {

        private static final long serialVersionUID = 1L;

        @Override
        public void sessionInit(SessionInitEvent event) {
            event.getSession().addBootstrapListener( new BootstrapListener() {

                private static final long serialVersionUID = 1L;

                @Override
                public void modifyBootstrapFragment(
                        BootstrapFragmentResponse response) {

                }

                @Override
                public void modifyBootstrapPage(BootstrapPageResponse response) {


                    if(response.getRequest().getHeader("User-Agent").contains("MSIE 8.0")){

                        response.getDocument().head().appendElement("script").attr("type", "text/javascript").attr("src", "https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js");
                        response.getDocument().head().appendElement("script").attr("type", "text/javascript").attr("src", "https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js");

                    }
                }}
            );
        }
    });
  }
}

然后从中更改web.xml中的默认servlet

<servlet>
<servlet-name>myappname</servlet-name>
<servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
...

<servlet>
<servlet-name>myappnam</servlet-name>
<servlet-class>your.package.name.BootstrapServlet</servlet-class>
...

这不会添加

<!--[if lt IE 9]>

但是它会根据UserAgent是否为IE 8有条件地添加脚本.

仅供参考,我实际上是在尝试做同样的事情,因为我在我的项目中使用了twitters Bootstrap,媒体查询.这些脚本是否与Vaadin 7一起工作还有待观察.

标签:html,java,maven,vaadin
来源: https://codeday.me/bug/20190728/1562970.html