Using RingoJS with Spring

91 views
Skip to first unread message

James Cook

unread,
Aug 20, 2011, 9:55:55 PM8/20/11
to rin...@googlegroups.com
Because there is more talk about using Spring and RingoJS together, I thought I would share some code we use to make it easy to get a reference to the Spring ApplicationContext from within your controllers. In a Java controller, you usually get a reference to Spring's context in your Servlet's init() method because you end up calling a static helper function and passing in your ServletConfig. RingoJS adheres to the JSGI specification which is agnostic to how infrastructure like Spring is made available to the controller layer.

RingoJS uses the org.ringojs.jsgi.JsgiServlet class to bootstrap the SSJS environment. We extend this class to provide some Spring helper functions that can be accessed from a RingoJS controller.

package org.ringojs.jsgi;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

public class ExtJsgiServlet extends JsgiServlet {

    private ApplicationContext _springContext;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        final ServletContext context = config.getServletContext();
        _springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
    }

    public ApplicationContext getSpringContext() {
        return _springContext;
    }

    public Object getBean(String name) {
        return _springContext.getBean(name);
    }
}

In order to launch your RIngoJS web application using this servlet, you simply replace the JsgiServlet with ExtJsgiServlet.

web.xml

<servlet>
    <servlet-name>ringo</servlet-name>
    <servlet-class>org.ringojs.jsgi.ExtJsgiServlet</servlet-class>
    <init-param>
        <param-name>optlevel</param-name>
        <param-value>0</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>ringo</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

The cool thing is that the Servlet instance (the ExtJsgiServlet class) is now available to your controller using the request.env property. The 'env' property is part of the JSGI specification and it is the place where all of the custom keys used by the server or middleware MUST be placed. So, here is what your controller might look like:

app.post('/profiles/:profileId', function(req, profileId) {
    // Get access to the servlet using the 'env' garbage dump property
    var servlet = req.env.servlet;
    
    // Use the servlet's getBean(String) function to retrieve a configured bean from Spring
    var datasource = servlet.getBean('datasource');

    // Or maybe you are using Spring's reloadable resource bundles for externalizing your
    // website's localized content
    var context = servlet.getSpringContext();
    var welcomeMessage = context.getMessage('welcome', null, req.env.servletRequest.locale);
});
 
You may notice that the welcomeMessage example uses another property stored on the context; the servlet request. It's funny how the JSGI specification does not mention Locale. It is so important to globalization. At least RingoJS has made it accessible by exposing the original Java servlet request object off the env property..


Robert Thurnher

unread,
Aug 21, 2011, 8:12:11 AM8/21/11
to rin...@googlegroups.com
Interesting, thanks for sharing!

I've put this on a wiki page to increase findability for (potential) users.

http://ringojs.org/wiki/Ringo_with_Spring

It's linked from "Java integration" page which is linked from "TODO"
page (which is linked from the wiki's main page), BTW.

IMHO, it would be great if more such info would be added there by you
guys actually using Ringo in according Java (enterprise) environments.

HTH,
-- Robi

indranil32

unread,
Aug 21, 2011, 2:15:54 PM8/21/11
to RingoJS
Like servlet can we implement filters & listeners too????

My project is using heavy dose of ringo + spring + E4x integration.It
would be really help full if people provide more of such code snippets
and configurations(as above).

Regards,
Indranil
> *web.xml*
> *
> *

James Cook

unread,
Aug 21, 2011, 4:29:33 PM8/21/11
to rin...@googlegroups.com
Thanks robi. 

Indranil, if you are using Ringo's JsgiServlet to bootstrap your SSJS environment, then you can use Servlet listeners and filters.

To this end, we use Spring Security to secure all of our RingoJS access.

-- jim
Reply all
Reply to author
Forward
0 new messages