I am currently evaluating the play framework. So far it seems to me, that it is a very pragmatical framework with really nice ideas apart from any specifications but just to get the job done. I like that approach very much. The mailinglist is quite alive and the developers are very responsive. If you need to have a quick development process, check it out. In the next few paragraphs I will describe howto to add basic search capability to the yabe sample (by using lucene in the backend, but you wont get any contact with the lucene API), which features yet another blog engine. This example assumes that you have taken a look at the yabe example and that you are familiar with the structure of a play project. Everything else is quite understandable from the code I will paste. Of course this code is rough, but it is only here to show how quick you can get out some already nice featured prototypes. Furthermore you should read the small text about the search module, which needs to be activated as well.
- The first step is to annotate your Post class with the @Indexed annotation and to annotate the content and title property with the @Field annotation (you need to import the search.jar from your play/modules/search installation in eclipse in order to use the import feature).
- The second step is to add a search controller to our Application (in Application.java):
public static void search(String search) {
Query query;
String highlight;
if (search.contains(":")) {
query = Search.search(search, Post.class);
highlight = search.split(":", 2)[1];
} else {
query = Search.search("content:"+search, Post.class);
highlight = search;
}
List results = query.fetch();
render(results, search, highlight);
}
- Third step: Add the search form to main.html (for example between header and main divs)
<div style="margin-bottom: 20px;">
#{form @Application.search()}
<p>
<input type="text" name="search" id="search" />
<input type="submit" value="Search" />
</p>
#{/form}
</div>
- Forth step: Create search query result page, search.html
#{extends 'main.html' /}
#{set title:'Search results' /}
<p>
<div>Your query for <b>${search}</b> returned ${results.size()} result(s)</div>
</p>
<p>
#{list items:results, as:'result'}
<div><a style="color : white" href="@{Application.show(result.id)}?highlight=${highlight}">${result.title}</a></div>
#{/list}
</p> - In theory, we are finished now. But that would be boring. What about highlighting the results in the text? Using a little jquery magic (from here) and the highlight parameter from above can do the trick. The first part is to adapt the show.html file for this, where I put the javascript code into. The code search for the class "real-content" and highlights inside that class the specified word, in this case the highlight parameter (from the request). So all the rendering is done client side.
<script type="text/javascript">
jQuery.fn.highlight = function (str, className)
{
var regex = new RegExp(str, "g");
return this.each(function ()
{
if (str.length > 0) {
this.innerHTML = this.innerHTML.replace(regex, "<span class=\"" + className + "\">" + str + "</span>");
}
});
};
$(document).ready(function() {
$(".real-content").highlight("${params.highlight}", "highlight");
})
</script>
- The second part is needed in display.html, where we need to change some classes, especially to mark our blog content with the real-content class. So I just wrapped the content rendering:
<div class="post-content">
<div class="about">Detail: </div>
<div class="real-content">${_post.content.nl2br()}</div>
</div>
- Last but not least, you need to add a new highlight class to main.css, for example .highlight { background : red; }
Now, try it. Reload your app, and check by searching for "fowler" or for "model" (returning two matches with the default data) and watch the highlighting. You can also search for "title:MVC", if you want to search only in titles.
Of course, some glitches are included. You cannot search for "Play!" what will be interpreted as special lucene parameter, words shorter than three letters are not possible either. But you can work that out I guess.
What else can I say about play? I think it should get some more attention. There are really nice ideas (like the "we dont need no real session" approach for rest and scalability, the caching aspect, the scalability ground works. I also like the templating and the strict conventions which should help you organize yourself.
Next steps for me and play: I'll hope to find the time to add dojo widgets to the crud module, so you would have a much nicer client validation as well as things like NumberTextBoxes and calendar widgets (but that's just me being more a dojo fan than a jquery one).