Is it possible to use jQuery inside of Vaadin framework?

Kyleinincubator picture Kyleinincubator · Feb 16, 2012 · Viewed 9.3k times · Source

As Vaadin is a Java web application framework, so is it possible to insert the jQuery javascript snippet in the Vaadin Java code?

Answer

miq picture miq · Feb 16, 2012

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);