原贴:

http://blog.caucho.com/2009/04/14/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> < body>

<% List images = (List) request.getAttribute("images"); for (Iterator it = images.iterator(); it.hasNext();) { ImageDescriptor image = (ImageDescriptor) it.next(); %> < table border="1" cellspacing="0" cellpadding="5">   <tr><td width="10%">Name</td><td><%= image.getName() %>&nbsp;</td></tr>         <tr><td colspan="2"><img src="data:imageContent?name=<%= image.getName() %>" height="100"></td></tr>         <tr><td>Description (<%= image.getDescriptionLength() %>)</td><td><%= image.getShortDescription() %>&nbsp;</td></tr> < /table> < p> < % } %>

<p> < table border="1" cellspacing="0" cellpadding="5"> < form action="imageUpload" method="post" encType="multipart/form-data">   <tr><td width="10%">Name</td><td><input type="text" name="name"><br></td></tr>   <tr><td>Content</td><td><input type="file" name="image"><br></td></tr>   <tr><td>Description</td><td><textarea name="description" cols="80" rows="5"></textarea></td></tr>   <tr><td colspan="2"><input type="submit" value="Upload image"></td></tr> < /form> < /table>

<p><a href="clearDatabase">Clear database</a>

</body> < /html>

<html> < body>

<?php foreach ($images as $image) { ?> < table border="1" cellspacing="0" cellpadding="5">   <tr><td width="10%">Name</td><td><?= $image->getName() ?>&nbsp;</td></tr>         <tr><td colspan="2"><img src="data:imageContent?name=<?= $image->getName() ?>" height="100"></td></tr>         <tr><td>Description (<?= $image->getDescriptionLength() ?>)</td><td><?= $image->getShortDescription() ?>&nbsp;</td></tr> < /table> < p> <?php } ?>

<p> < table border="1" cellspacing="0" cellpadding="5"> < form action="imageUpload" method="post" encType="multipart/form-data">   <tr><td width="10%">Name</td><td><input type="text" name="name"><br></td></tr>   <tr><td>Content</td><td><input type="file" name="image"><br></td></tr>   <tr><td>Description</td><td><textarea name="description" cols="80" rows="5"></textarea></td></tr>   <tr><td colspan="2"><input type="submit" value="Upload image"></td></tr> < /form> < /table>

<p><a href="clearDatabase">Clear database</a>

</body> < /html>

JSP:

<%@ page session="false" %>
<%@ page import="java.util.List,
java.util.Iterator,
org.springframework.samples.imagedb.ImageDescriptor" %> <!– imageList.jsp –> <html>
<body> <%
List images = (List) request.getAttribute("images");
for (Iterator it = images.iterator(); it.hasNext();) {
ImageDescriptor image = (ImageDescriptor) it.next();
%>
<table border="1" cellspacing="0" cellpadding="5">
<tr><td width="10%">Name</td><td><%= image.getName() %>&nbsp;</td></tr>
<tr><td colspan="2"><img src="data:imageContent?name=<%= image.getName() %>" height="100"></td></tr>
<tr><td>Description (<%= image.getDescriptionLength() %>)</td><td><%= image.getShortDescription() %>&nbsp;</td></tr>
</table>
<p>
<%
}
%> <p>
<table border="1" cellspacing="0" cellpadding="5">
<form action="imageUpload" method="post" encType="multipart/form-data">
<tr><td width="10%">Name</td><td><input type="text" name="name"><br></td></tr>
<tr><td>Content</td><td><input type="file" name="image"><br></td></tr>
<tr><td>Description</td><td><textarea name="description" cols="80" rows="5"></textarea></td></tr>
<tr><td colspan="2"><input type="submit" value="Upload image"></td></tr>
</form>
</table> <p><a href="clearDatabase">Clear database</a> </body>
</html>

PHP:

<html>
<body> <?php
foreach ($images as $image) {
?>
<table border="1" cellspacing="0" cellpadding="5">
<tr><td width="10%">Name</td><td><?= $image->getName() ?>&nbsp;</td></tr>
<tr><td colspan="2"><img src="data:imageContent?name=<?= $image->getName() ?>" height="100"></td></tr>
<tr><td>Description (<?= $image->getDescriptionLength() ?>)</td><td><?= $image->getShortDescription() ?>&nbsp;</td></tr>
</table>
<p>
<?php
}
?> <p>
<table border="1" cellspacing="0" cellpadding="5">
<form action="imageUpload" method="post" encType="multipart/form-data">
<tr><td width="10%">Name</td><td><input type="text" name="name"><br></td></tr>
<tr><td>Content</td><td><input type="file" name="image"><br></td></tr>
<tr><td>Description</td><td><textarea name="description" cols="80" rows="5"></textarea></td></tr>
<tr><td colspan="2"><input type="submit" value="Upload image"></td></tr>
</form>
</table> <p><a href="clearDatabase">Clear database</a> </body>
</html>

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:

<?xml version="1.0" encoding="UTF-8"?>
<!–
- 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.

Using PHP as a Spring MVC View via Quercus(转)的更多相关文章

  1. [Spring MVC] - view的redirect和forward

    可以通过redirect/forward:url方式转到另一个Action进行连续的处理.可以通过redirect:url 防止表单重复提交 .写法如下:return "forward:/o ...

  2. Spring MVC视图层:thymeleaf vs. JSP

    本文对比了同一Spring MVC工程中相同页面(一个订阅表单)分别采用Thymeleaf和JSP(包括JSP.JSTL.Spring tag lib)两种方式的实现. 本文的所有代码来自一个可运行的 ...

  3. SpringBoot集成Spring MVC视图

    SpringBoot在springmvc的视图解析器方面就默认集成了ContentNegotiatingViewResolver和BeanNameViewResolver,在视图引擎上就已经集成自动配 ...

  4. Spring MVC 解读——View,ViewResolver(转)

    上一篇文章(1)(2)分析了Spring是如何调用和执行控制器方法,以及处理返回结果的,现在我们就分析下Spring如何解析返回的结果生成响应的视图. 一.概念理解 View ---View接口表示一 ...

  5. spring mvc DispatcherServlet详解之三---request通过ModelAndView中获取View实例的过程

    整个spring mvc的架构如下图所示: 上篇文件讲解了DispatcherServlet第二步:通过request从Controller获取ModelAndView.现在来讲解第三步:reques ...

  6. 使用高性能xml序列化框架jibx作为spring mvc的xml view

    package org.springframework.web.servlet.view.xml; import java.io.ByteArrayOutputStream; import java. ...

  7. Spring MVC基础知识整理➣View与Controller数据交互

    概述 Spring MVC是由View—Controller—Model组成,其中View和Controller的数据交互,成为了关注的核心点.MVC中,我们将View中的数据传递到Controlle ...

  8. Spring MVC 学习笔记3 - 利用Default Annotation 模式获取请求,使Controller与View对应,并传值。

    1. WEB-INF/web.xml 这里定义了获取请求后,执行的第一步.抓取请求. <servlet> <servlet-name>appServlet</servle ...

  9. 内容协商在视图View上的应用【享学Spring MVC】

    每篇一句 人生很有意思:首先就得活得长.活得长才能够见自己,再长就可以见众生 前言 在经过 前两篇 文章了解了Spring MVC的内容协商机制之后,相信你已经能够熟练的运用Spring MVC提供的 ...

随机推荐

  1. 使用Java开发微信公众平台(二)——消息的接收与响应

    上一篇文章(http://www.jerehedu.com/fenxiang/171807_for_detail.htm )中,我们学习了使用Java语言开发微信公众平台的第一部分——环境搭建与开发接 ...

  2. IOS 多线程 NSThread

    一个正在运行的应用程序是一个进程,一个进程会默认开启一个主线程,但是在主线程中的操作是串行的,也就是当有多个任务同时需要完成的时候,是按照顺序一个个执行.因此,为了提高效率,会在进程中开启多个线程,每 ...

  3. TSQL:判断某较短字符串在较长字符串中出现的次数。

    给定一个较短字符串shortStr='ab',和一个较长字符串longStr='adkdabkwelabwkereabrsdweo2342ablk234lksdfsdf1abe': 判断shortSt ...

  4. 强大的Mockito测试框架

    转载:https://blog.csdn.net/dc_726/article/details/8568537 1自动生成Mock类   在需要Mock的属性上标记@Mock注解,然后@RunWith ...

  5. 五毛党可能要失业了,因为AI水军来了

    当AI已经开始写稿.唱歌.翻译文章.把语音转录为文字的时候,我们其实应该清醒的认识到,五毛党要消亡了. 相信大部分人和小编一样,现在只要出门吃饭,就会打开大众点评搜好吃的,看评分,看网友的评论.一般来 ...

  6. Android 为何比 iOS 卡?【转载】

    Android 卡是必须的,当你的手机装了 20 多个 app,那不卡才叫见鬼了呢,我手机微信都打不开,手机直接自动重启啦~哪种东西生来就是完美的呢?即便是台式机,也是越用越慢.换句话,如果没有特别原 ...

  7. 比特币 Bitcoin 是什么,我勒个去,哈耶克果然超前——货币的非国有化,容我思量一下【转载+整理】

    原文地址 比特币矿业史(上):故事的开始,CPU 时代 比特币矿业史(中):群众的觉醒 ,GPU 时代 比特币矿业史(下):巨头的诞生 ,ASIC 时代 本文内容 引子 0 序 1 故事的开始 : C ...

  8. android中ListView控件最简单的用法

    创建一个活动,在xml文件中添加一个ListView控件,id定义为list1,并且设置为满屏显示,代码如下: <ListView android:id="@+id/list1&quo ...

  9. jQuery动画animate方法使用介绍

    用于创建自定义动画的函数. 返回值:jQuery animate(params, [duration], [easing], [callback]) 如果使用的是“hide”.“show”或“togg ...

  10. Go语言和ASP.NET的一般处理程序在处理WEB请求时的速度比较

    Go语言和ASP.NET的一般处理程序在处理WEB请求时的速度比较 1.首先写一个Go语言的简单WEB程序,就返回一个HelloWord! package main import ( f " ...