As Vaadin is a Java web application framework, so is it possible to insert the jQuery javascript snippet in the Vaadin Java code?
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);
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.
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);
}
}
来源:https://stackoverflow.com/questions/9303534/is-it-possible-to-use-jquery-inside-of-vaadin-framework