Vaadin 7支持自定义javascript。但我的问题是,如果我们想要将jQuery与vaadin7集成,我们如何添加jQuery文件。目前@Javascript只支持添加javascript。如果我们想要添加css,我们就把它添加为sass样式。
发布于 2015-01-09 09:48:34
要将jQuery (或任何其他javascript库)添加到Vaadin 7应用程序中,请执行以下简单步骤:
首先,使用您最喜欢的IDE或Vaadin maven原型(或两者兼而有之)创建一个vaadin项目。创建一个从VaadinServlet扩展的新类,并override servletInitialized方法:
import javax.servlet.ServletException;
import com.vaadin.server.BootstrapFragmentResponse;
import com.vaadin.server.BootstrapListener;
import com.vaadin.server.BootstrapPageResponse;
import com.vaadin.server.ServiceException;
import com.vaadin.server.SessionInitEvent;
import com.vaadin.server.SessionInitListener;
import com.vaadin.server.VaadinServlet;
public class TestJqueryVaadinServlet extends VaadinServlet {
@Override
protected void servletInitialized() throws ServletException {
super.servletInitialized();
getService().addSessionInitListener(new SessionInitListener() {
@Override
public void sessionInit(SessionInitEvent event) throws ServiceException {
event.getSession().addBootstrapListener(new BootstrapListener() {
@Override
public void modifyBootstrapPage(BootstrapPageResponse response) {
// With this code, Vaadin servlet will add the line:
//
// <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" />
//
// as the first line inside the document's head tag in the generated html document
response.getDocument().head().prependElement("script").attr("type", "text/javascript").attr("src", "//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js");
}
@Override
public void modifyBootstrapFragment(BootstrapFragmentResponse response) {}
});
}
});
}
}然后在web.xml中添加对servlet的引用,或者用@WebServlet注释对类进行注释。
然后创建jQuery片段并使用JavaScript类调用它,例如:
public class MyVaadinUI extends UI {
@Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
Label label = new Label("This will fade-out once you click the button");
Button button = new Button("Hide Label");
button.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
JavaScript.getCurrent().execute("$('.v-label').animate({opacity: 0.0}, 3000);");
}
});
layout.addComponent(label);
layout.addComponent(button);
}
} 现在可以通过向组件或扩展类添加@样式表或@JavaScript注释来完成在插件中或作为应用程序的一部分包括样式表或JavaScript文件。在框架初始化客户端组件或扩展之前,每个注释都会使用带有URL的字符串列表到应该加载到页面上的资源。
URL可以是完全绝对URL(例如“https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js”),也可以是相对URL(例如"redbutton.css")。一个相对URL被转换成一个特殊的URL,该URL将从定义类所在的Java包下载文件。这意味着类com.example.RedButton上的@样式表({“com.example.RedButton”})将导致在浏览器中加载类路径上的com/examplon.css文件。@JavaScript以完全相同的方式工作
#!java
@StyleSheet("redbutton.css")
public class RedButton extends NativeButton {
public RedButton(String caption) {
super(caption);
addStyleName("redButton");
}
}在这个简单的例子中,RedButton组件只是添加了一个
redButton样式名称改为正常名称
NativeButton。redbutton.css与RedButton.java位于同一个文件夹中,具有以下内容:
#!css
.redButton {
background-color: red;
}这种新机制非常容易地包含带有加载项的样式表或JavaScript文件,并在使用外接程序时在浏览器中自动加载它们。
第二,也是我最喜欢的方式:
您还可以使用@样式表和@Javascript注释。简单多了。
@StyleSheet({
/*
* JQuery UI
*/
"vaadin://jquery/jquery-ui-1.9.2.custom/css/ui-darkness/jquery-ui-1.9.2.custom.min.css",
})
@JavaScript({
/*
* JQuery
*/
"vaadin://jquery/jquery-1.11.1.min.js",
/*
* JQuery UI
*/
"vaadin://jquery/jquery-ui-1.9.2.custom/js/jquery-ui-1.9.2.custom.min.js",
})
public class MyUI extends UI {
...
}https://stackoverflow.com/questions/27857487
复制相似问题