当前位置:Gxlcms > PHP教程 > UsingPHPasaSpringMVCViewviaQuercus(转)_PHP教程

UsingPHPasaSpringMVCViewviaQuercus(转)_PHP教程

时间:2021-07-01 10:21:17 帮助过:7人阅读

原贴:

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:

< /table> < p> < % https://www.gxlcms.com/} %>

< table border=https://www.gxlcms.com/"1" cellspacing=https://www.gxlcms.com/"0" cellpadding=https://www.gxlcms.com/"5"> < form action=https://www.gxlcms.com/"imageUpload" method=https://www.gxlcms.com/"post" encType=https://www.gxlcms.com/"multipart/form-data">

< /form> < /table>

"clearDatabase">Clear database

< /html>

< /table> < p> https://www.gxlcms.com/ https://www.gxlcms.com/} https://www.gxlcms.com/?>

< table border=https://www.gxlcms.com/"1" cellspacing=https://www.gxlcms.com/"0" cellpadding=https://www.gxlcms.com/"5"> < form action=https://www.gxlcms.com/"imageUpload" method=https://www.gxlcms.com/"post" encType=https://www.gxlcms.com/"multipart/form-data">

< /form> < /table>

"clearDatabase">Clear database

< /html>

<%@ page session=https://www.gxlcms.com/"false" %> < %@ page import=https://www.gxlcms.com/"java.util.List, java.util.Iterator, org.springframework.samples.imagedb.ImageDescriptor" %>

< body>

<% https://www.gxlcms.com/List images = https://www.gxlcms.com/(https://www.gxlcms.com/Listhttps://www.gxlcms.com/) request.getAttributehttps://www.gxlcms.com/(https://www.gxlcms.com/"images"https://www.gxlcms.com/); https://www.gxlcms.com/for https://www.gxlcms.com/(Iterator it = images.iteratorhttps://www.gxlcms.com/(https://www.gxlcms.com/); it.hasNexthttps://www.gxlcms.com/(https://www.gxlcms.com/);https://www.gxlcms.com/) https://www.gxlcms.com/{ ImageDescriptor image = https://www.gxlcms.com/(ImageDescriptorhttps://www.gxlcms.com/) it.https://www.gxlcms.com/nexthttps://www.gxlcms.com/(https://www.gxlcms.com/); %> < table border=https://www.gxlcms.com/"1" cellspacing=https://www.gxlcms.com/"0" cellpadding=https://www.gxlcms.com/"5">

"10%">Name<%= image.getNamehttps://www.gxlcms.com/(https://www.gxlcms.com/) %>
"2">"https://www.gxlcms.com/imageContent?name=<%= image.getName() %>" height=https://www.gxlcms.com/"100">
Description https://www.gxlcms.com/(<%= image.getDescriptionLengthhttps://www.gxlcms.com/(https://www.gxlcms.com/) %>https://www.gxlcms.com/)<%= image.getShortDescriptionhttps://www.gxlcms.com/(https://www.gxlcms.com/) %>
"10%">Name"text" name=https://www.gxlcms.com/"name">
Content"file" name=https://www.gxlcms.com/"image">
Description
"2">"submit" value=https://www.gxlcms.com/"Upload image">
< body>

https://www.gxlcms.com/ https://www.gxlcms.com/foreach https://www.gxlcms.com/(https://www.gxlcms.com/$images https://www.gxlcms.com/as https://www.gxlcms.com/$imagehttps://www.gxlcms.com/) https://www.gxlcms.com/{ https://www.gxlcms.com/?> < table border=https://www.gxlcms.com/"1" cellspacing=https://www.gxlcms.com/"0" cellpadding=https://www.gxlcms.com/"5">

"10%">Name$image->https://www.gxlcms.com/getNamehttps://www.gxlcms.com/(https://www.gxlcms.com/) ?>
"2">"imageContent?name=getName() ?>" height=https://www.gxlcms.com/"100">
Description https://www.gxlcms.com/(https://www.gxlcms.com/= https://www.gxlcms.com/$image->https://www.gxlcms.com/getDescriptionLengthhttps://www.gxlcms.com/(https://www.gxlcms.com/) https://www.gxlcms.com/?>https://www.gxlcms.com/)$image->https://www.gxlcms.com/getShortDescriptionhttps://www.gxlcms.com/(https://www.gxlcms.com/) ?>
"10%">Name"text" name=https://www.gxlcms.com/"name">
Content"file" name=https://www.gxlcms.com/"image">
Description
"2">"submit" value=https://www.gxlcms.com/"Upload image">

JSP:

<%@ page session="false" %>
 <%@ page https://www.gxlcms.com/="java.util.List,
https://www.gxlcms.com/" %>




 

<%https://www.gxlcms.com/= (List) request.getAttribute("images"https://www.gxlcms.com/https://www.gxlcms.com/ (Iterator it =https://www.gxlcms.com/=https://www.gxlcms.com/%>
 
Name<%= image.getName() %>
Description (<%= image.getDescriptionLength() %>)<%= image.getShortDescription() %>

<%https://www.gxlcms.com/%>

Clear database

https://www.gxlcms.com/


 

https://www.gxlcms.com/ (https://www.gxlcms.com/ https://www.gxlcms.com/ https://www.gxlcms.com/https://www.gxlcms.com/?>
 
Name->getName() ?>
Description (->getDescriptionLength() ?>)->getShortDescription() ?>

?>

Clear database

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:

https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/
 https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/
 https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/

        https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/
         https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/

        https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/
         https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/

        https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/
         https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/

        https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/
                 https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/
                 https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/
                 https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/
         https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/
 https://www.gxlcms.com/https://www.gxlcms.com/https://www.gxlcms.com/

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.

https://www.gxlcms.com/https://www.gxlcms.com/www.bkjia.comhttps://www.gxlcms.com/truehttps://www.gxlcms.com/http://www.bkjia.com/PHPjc/626628.htmlhttps://www.gxlcms.com/TechArticlehttps://www.gxlcms.com/原贴: 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...

人气教程排行