Ok thanks to Chris McDonough and the repoze community things are now much easier than they were on my last post, in this one I’ll explain how to install repoze.bfg with the jinja2 templating environment, and after that how to use it inside your java webapp, this is very interesting because you can use any resource from your application server from bfg!! how cool is that?
Ok, so first set up your virtualenv you can check how to do that on my last post if you don’t know (just follow it up to the “source sys/bin/activate”) command.
Now there are two ways to install repoze.bfg, the first one is:
easy_install -i http://dist.repoze.org/bfg/1.2/simple repoze.bfg.jinja2 |
and the second one is to install it from pypi which is almost the same:
easy_install repoze.bfg.jinja2 |
Cool isn’t it?
Now to create your repoze.bfg app use paster like this:
paster create -t bfg_jinja2_starter bfgapp |
And set it up as a development egg
cd bfgapp
jython setup.py devel |
You can test it if you want, running
paster serve bfgapp.ini |
And that’s it!! Much easier isn’t it?
Now, let’s look how to put this inside our java webapp, for this we are going to use a WSGI java servlet which is included with jython, it’s called modjy.
First of all copy the jython.jar file inside your WEB-INF/lib directory.
Next add the servlet to your WEB-INF/web.xml file, here is my example web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name>BFGApp</display-name> <servlet> <servlet-name>modjy</servlet-name> <servlet-class>com.xhaus.modjy.ModjyJServlet</servlet-class> <init-param> <param-name>python.home</param-name> <param-value>/home/iamedu/BFG/sys</param-value> </init-param> <init-param> <param-name>cache_callables</param-name> <param-value>1</param-value> </init-param> <init-param> <param-name>reload_on_mod</param-name> <param-value>1</param-value> </init-param> <init-param> <param-name>log_level</param-name> <param-value>debug</param-value> </init-param> <init-param> <param-name>load_site_packages</param-name> <param-value>1</param-value> </init-param> <init-param> <param-name>python.executable</param-name> <param-value>/home/iamedu/BFG/sys/bin/jython</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>DefaultServlet</servlet-name> <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class> <init-param> <param-name>listings</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>modjy</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>DefaultServlet</servlet-name> <url-pattern>/static/*</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> </web-app> |
Here there are two important initial parameters for the modjy servlet:
- python.home This one should point to your virtualenv
- python.executable This one should point to your virtualenv’s jython executable
You can see I also defined a “default” servlet, this is because there’s a problem with the modjy servlet managing static resources, and it’s probably faster to manage them directly with the default servlet.
Now to make the static resources accessible to the default servlet, just copy them from the templates/static directory in your BFG app to the web directory inside your java webapp, just copy the default.css file and the images directory don’t copy the whole static directory, unless you want to change your links to something like: “static/static/default.css”
Now we just need to add the wrapper between the modjy servlet and bfg, this is quite easy, create an application.py file inside your web directory with the following contents:
import site from repoze.bfg.paster import get_app handler = get_app('/home/iamedu/BFG/MyProject/MyProject.ini', 'main') |
And now deploy your app and voila!, you have repoze.bfg on you favorite servlet container, you can call your java classes from bfg as it is running in the same Java VM.
We use import site, because the modjy servlet doesn’t load site-packages automatically, and it’s probably a bad idea to write the complete path in the get_app, you should copy it to your webapp’s web directory and get the current work directory.
If you want to package your bfg app inside your java webapp you can copy it to the web directory, everything inside that directory is also in your pythonpath.
Hope this helps somebody!

Es mi sensación o ya te estás metiendo más en desarrollo web (templates)?
Saludos
bfg es mucho más que templates, es framework para desarrollo, y lo hago por que no quiero tener que estar lidiando con struts o jsp para desarrollar.. me parecen cosas del siglo pasado
Orales se ve bueno el framework creo q tendré que darle una leida, hay edu como te da tiempo de hacer tantas cosas
El edu es un cyborg programado para aprender y después nos va a queres convencer de que es el mejor candidato para que propicie la paz en la tierra y matará al que se revele… ay wey!! que miedo!!!