如何在Vaadin中更改默认HTML模板 [英] How to change default HTML template in Vaadin
问题描述
我正在使用Vaadin和maven应用程序。我想要的是更改默认的html模板。
运行应用程序时,生成的HTML如下所示:
< html>< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8>
< meta http-equiv =X-UA-Compatiblecontent =IE = 10; chrome = 1>
< style type =text / css> html,body {height:100%; margin:0;}< / style>
< link rel =快捷图标type =image / vnd.microsoft.iconhref =./ VAADIN / themes / customBootstrap / favicon.ico>
< link rel =icontype =image / vnd.microsoft.iconhref =./ VAADIN / themes / customBootstrap / favicon.ico>
< link rel =stylesheettype =text / csshref =./ 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扩展了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/html5 shiv.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有条件地添加脚本。 / p>
仅供参考,我实际上是在尝试做同样的事情,因为我在我的项目中使用了twitters Bootstrap,媒体查询。这些脚本是否与Vaadin 7一起工作还有待观察。
I'm using Vaadin with maven application. What I want is changing the default html template. When I run the application, the generated HTML looks like this :
<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"
...
I want to change the "meta" properties, add "link" and also add some other components like :
<!--[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]-->
Is there a way to do that with Vaadin ? Thanks!
Create a custom servlet that extend the VaadinServlet:
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");
}
}}
);
}
});
}
}
Then change the default servlet in your web.xml from
<servlet>
<servlet-name>myappname</servlet-name>
<servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
...
To
<servlet>
<servlet-name>myappnam</servlet-name>
<servlet-class>your.package.name.BootstrapServlet</servlet-class>
...
This will not add the
<!--[if lt IE 9]>
however it will conditionally add the scripts based on whether or not the UserAgent is IE 8 or not.
FYI, I am actually trying to do the exact same thing because I am using twitters Bootstrap, media queries, in my project. Whether or not those scripts work along side Vaadin 7 is yet to be seen.
这篇关于如何在Vaadin中更改默认HTML模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!