Using PHP as a Spring MVC View via Quercus
This week, I’ve been prepping for a talk on Quercus in which I promised to show a demo of Spring MVC using a PHP view. So that means that I actually had to do it.
Turns out it was quite easy and PHP makes for a very nice, compact view technology for Spring MVC. This is a bit of tease since the code for this won’t go out until at least next week, but since a number of people have been asking for this a while, I thought I’d give a preview…
First, let me show how it looks by using the sample “ImageDB” application that ships with Spring. Here’s a screenshot of the app in action:

Basically, you upload an image to the page and it keeps track of what you’ve uploaded in a database. Here are the JSP and PHP views side-by-side:
|
<%@ page session="false" %>
<%@ page import="java.util.List, java.util.Iterator, org.springframework.samples.imagedb.ImageDescriptor" %> <!– imageList.jsp –> <html> <% <p> <p><a href="clearDatabase">Clear database</a> </body> |
<html>
<body> <?php <p> <p><a href="clearDatabase">Clear database</a> </body> |
What I think is interesting between these two is that the PHP, even though it’s calling Java objects, has a simpler syntax. It’s not a major issue, but you can see that PHP is as reasonable as any other view for Java.
Now how do you configure it? Just add the QuercusView class to a UrlBasedViewResolver and give a php suffix and you’re done:
<!–
- DispatcherServlet application context for the image database.
–>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!– Activates @Autowired for ImageController –>
<context:annotation-config/>
<!– MultiActionController that defines user interface actions as separate methods –>
<bean id="imageController" class="org.springframework.samples.imagedb.web.ImageController"/>
<!– MultipartResolver for parsing file uploads, implementation for Commons FileUpload –>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="com.caucho.spring.quercus.QuercusView"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".php"/>
</bean>
</beans>
If you’re interested in the implementation… The view was pretty easy to connect up once I learned Spring’s view API. It’s essentially a Servlet.service() call with a map of model values. So the QuercusView class above is just a modified QuercusServlet that injects the model values as PHP globals. I’m not sure that that’s right just yet, but it’s a start. The other option would be to put the values as PHP superglobals or in a specialized Spring array.

April 24th, 2009 at 10:37 pm
This looks great, and it solves a long-standing problem with a legacy application we’ve been trying to migrate. Is QuercusView in the repository now? How should I get updates on the status of this implementation?
Thanks.
April 27th, 2009 at 9:46 am
Hi delsvr,
It is in the repository right now and is built with “ant artifacts”. Then just use resin/artifacts/spring/dist/resin-spring.jar. I’m not sure yet how we’ll “ship” a prebuilt version of this. The easiest way to get updates would be to write to resin-interest. At this point, I don’t see any problems with the implementation so we’ll need people to test it to see if it breaks before I make any more updates.
Thanks,
Emil
February 5th, 2010 at 12:44 pm
Emil,
Actually, you’re still cheating with the JSP. Once you remove the pure Java inside of the scriptlet tags and replace it with proper JSTL and EL, the JSP gets considerably larger, making PHP even more compact, comparatively speaking.
Another nice thing about PHP over JSP is that writing helper classes in PHP is MUCH less time-consuming than writing your own JSP tag libraries. Just create a new PHP class (or even plain-old code fragment in another PHP file), call one of include/include_once/require_once, and you’re done. Another point to take home is that PHP was meant for the web; specifically, the presentation layer. PHP comes built-in with an EXTREMELY large array of functions that a template writer can use out of the box, making the JSTL look like its on a diet. Another bonus is that when iteratively testing out your page design (code, reload page, fix, code, reload page, …), you don’t have to wait for your template code to get compiled; with PHP is just shows up instantaneously (assuming you’re in Quercus “development mode”, which is speedy enough as it is). No need to wait for a recompile, no need to use something like JRebel, no more OOM errors after X number of page reloads, etc.
And lastly, since PHP is so well-known and has so much mindshare, even novice developers from the greater PHP world can be brought in to a team and begin work immediately without having to learn yet another technology.
Oh, and ONE MORE THING: with Quercus, you don’t have to make sure all of your PHP module versions line up perfectly. Using traditional PHP, you’re often stuck with one version for a long time unless you want to go through the painful and often fruitless process of recompiling PHP, all of its modules, and Apache. I use RHEL/CentOS almost exclusively, and you’re pretty much stuck with one version of PHP (not counting patch releases). With Quercus, it’s just a matter of downloading ONE single quercus.jar file and restarting the application server. Voila. Total time: less than 45 seconds. The alternative could take days and lots of Googling.
Anyway, great work. I love Quercus. Might Caucho consider open-sourcing the part of Quercus/Resin that compiles PHP into Java bytecode? Remember that with Quercus you’re selling a platform here, not an application. Therefore it behooves Caucho to get this technology into as many hands as possible without restrictions or constraints. For more info on what I’m referring to as “Platform”, see Joel Spolsky’s post here: http://www.joelonsoftware.com/articles/Platforms.html
By keeping the full potential (PHP -> Java bytecode compilation) of Quercus locked up, you’re increasing the chances of it being a niche technology only. By getting it into as many hands as possible, however, you’re giving Caucho and its commercial offerings more free exposure to the people who really matter to your bottom line: developers and IT folk. Think about it.
Michael
April 3rd, 2012 at 12:37 pm
Emil,
I just recently got into developing Web Applications and have been researching all the various options out there. I have a feeling the project I’m working on will be in a position where we have a PHP front end and a Java Backend. I wrote up a few test apps using Spring/JavaEE on various application servers. I’d like to use Spring as our Framework, but due to my level of experience I’m all sorts of lost on how I might handle our integration points with PHP.
For example, let’s take a simple web form to handle user logins. The latest test I did had a PHP page on Apache POST to a Java Servlet on Tomcat. I know I’m missing something simple, but when running the form through JSP, Spring just pulls in the Form elements/attributes and assigns them to a POJO. I have no idea how to do that with strait HTML/PHP aside from manually parsing the POST request and assigning it to an object. Any insight?
Many Thanks,
-Jason
January 21st, 2013 at 1:26 am
Hi Emil, where can I download the source code of this demo project? Thanks.
January 29th, 2013 at 2:18 am
Hi Emil,
Can we use Apache tiles or Sitemesh templating framework while using PHP as a view? If yes, could you please help how to do that?
-Zia