Reputation: 313
As Vaadin is a Java web application framework, so is it possible to insert the jQuery javascript snippet in the Vaadin Java code?
Upvotes: 14
Views: 9824
Reputation: 714
You can use the @JavaScript and @StyleSheet annotations
@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 {
...
}
For execution:
JavaScript.getCurrent().execute("...javascript code here...")
Be careful with larger scripts. Adding javascript via the vaadin annotation has very poor performance. Better inject the script in the html header manually.
Upvotes: 5
Reputation: 1493
One more customizing for ApplicationServlet:
public class VaadinApplicationServlet extends ApplicationServlet {
@Override
protected void writeAjaxPageHtmlHeader(BufferedWriter page, String title, String themeUri, HttpServletRequest request) throws IOException {
page.write("<script language='javascript' src='http://code.jquery.com/jquery-1.8.0.min.js'></script>");
super.writeAjaxPageHtmlHeader(page, title, themeUri, request);
}
}
Upvotes: 3
Reputation: 2766
Yes it is.
Create your own ApplicationServlet extending class like this:
public class MyApplicationServlet extends ApplicationServlet {
@Override
protected void writeAjaxPageHtmlVaadinScripts(Window window,
String themeName, Application application, BufferedWriter page,
String appUrl, String themeUri, String appId,
HttpServletRequest request) throws ServletException, IOException {
page.write("<script type=\"text/javascript\">\n");
page.write("//<![CDATA[\n");
page.write("document.write(\"<script language='javascript' src='./jquery/jquery-1.4.4.min.js'><\\/script>\");\n");
page.write("//]]>\n</script>\n");
super.writeAjaxPageHtmlVaadinScripts(window, themeName, application,
page, appUrl, themeUri, appId, request);
}
}
Then replace the Vaadin servlet in your web.xml (the default is com.vaadin.terminal.gwt.server.ApplicationServlet
):
<servlet-class>com.example.MyApplicationServlet</servlet-class>
You can then make jQuery calls in your code by calling:
MyApplication.getMainWindow().executeJavascript(jQueryString);
Upvotes: 10