Original stickers:
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, means that I actually had to do it. Turns out it is quite easy and PHP makes for a very nice, compact view technology for Spring MVC. This was a bit of tease since the code for this won ' t go out until at least next week, but since a number of people has been En asking for the while, I thought I ' d give a preview ...
First, let me show how it looks by using the sample "Imagedb" Application this 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 is the JSP and PHP views Side-by-side:
}
} ?>
< table border="1" cellspacing="0" cellpadding="5"> < form action=" Imageupload " method=" POST " enctype=" Multipart/form-data ">
foreach ($images as $image) { ?> < table border="1" cellspacing="0" cellpadding="5">
<%@ page session="false"%> <%@ page import="Java.util.List, Java.util.Iterator, Org.springframework.samples.imagedb.ImageDescriptor"%> < 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"> |
"10%" >name |
<%= image.getname() %> |
"2" > "imagecontent?name=<%= image.getname ()%>" height="> |
Description (<%= image.getdescriptionlength() %>) |
<%= image.getshortdescription() %> |
</table> < P> <%%>
< table border="1" cellspacing="0" cellpadding="5"> < form action=" Imageupload " method=" POST " enctype=" Multipart/form-data ">
"10%" >name |
"Text" name="name">
|
Content |
"File" name="image">
|
Description |
"description" </span> cols=<span class= "St0" > "+" </span> rows=<span class= "St0" > "5" </ Span>> |
"2" >"Submit" value="Upload image"> |
</form> </table>
"Cleardatabase" >clear database
| < body>
"10%" >name |
getName, $image () ?> |
"2" > "Imagecontent?name= getName ()?>" height="> |
Description ( = $image-getdescriptionlength() ?> ) |
getshortdescription, $image () ?> |
</table> < p>
"10%" >name |
"Text" name="name">
|
Content |
"File" name="image">
|
Description |
"description" </span> cols=<span class= "St0" > "+" </span> rows=<span class= "St0" > "5" </ Span>> |
"2" >"Submit" value="Upload image"> |
</form> </table>
"Cleardatabase" >clear database
Jsp:
<%@ page session= "false"%> <%@ page= "Java.util.List,"%>
<%< span="">= (List) request.getattribute ("Images" < span=""> < span=""> (Iterator it =< span="">= < span=""> %>
Name |
<%= Image.getname ()%> |
"Height=" > |
Description (<%= image.getdescriptionlength ()%>) |
<%= image.getshortdescription ()%> |
<%< span="">%>
Clear Database
< span="">
< span="">< span="">(?>< span="">< span="">< span="">
Name |
|
->getname ()?> "height=" > |
Description ( ->getdescriptionlength ()?>) |
|
?>
What I think is interesting between these, and the PHP, even though it ' s calling Java objects, has a simpl ER syntax. It's not a major issue, but you can see the PHP is as reasonable as any other view for Java.
Now how does you configure it? Just Add the Quercusview class to a urlbasedviewresolver and give a php suffix and you ' re-done:
< span=""> < span=""> < span=""> < span="">< span="">< span="">< span="">< span="">< span="">< span="">< span="">< span="">< span="">< span="">< c24> < span=""> < span=""> < span=""> < span=""> < span="">< span=""> < span="">< span="">< span=""> < span="">< span="">< span=""> < span="">< span="">< /c55> < span=""> < span="">
If you ' re interested in the implementation ... The view is pretty easy-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 are just a modified quercusservlet that injects the model values as PHP globals. I ' m not sure so that's right just yet, but it's a start. The other option would is to put the values as PHP superglobals or in a specialized Spring array.
http://www.bkjia.com/PHPjc/626628.html www.bkjia.com true http://www.bkjia.com/PHPjc/626628.html techarticle Original stickers: Http://blog.caucho.com/2009/04/14/using-php-as-a-spring-mvc-view-via-quercus/This week, Ive been prepping For a talk on Quercus in which I promised to show a demo of SPR ...