Skip to content

Playframework: handling of validated objects

In my previous blog (see validations for the play framework) I showed you how easy it can be to handle validations with the play framework. This time I will try to explain how this can be combined with the controllers in the playframework to handle these errors.

The controllers in the playframework are the central entry point from the web to your domain. Each controller should extend playframework’s Controller and have a static void method for each action that can be done on the controllers. These actions are static since they are supposed to be a binding point between the stateless aspect of http towards the statefull objects of your models. The actions are bound to urls and type of request (POST or GET) through the routes configuration file. Using the routes configuration file you can create nice looking rest-like url’s that are bound to the controllers. Also this routes file will be used to generate the urls for you if you add them to your view template files. For instance, given that your routes file contains the following route: Read more…

Validations for the Play Framework

It can be very easy to add validations to the play framework. Just adding annotations to the objects will usually do the trick, but these are for the values of one field only, like @Required or @Email. If you want to check if some combined properties are valid, then you can use the @CheckWith annotation. With this annotation you have to define a class that will be used for the validation and optionally a message if this fails. For instance, a check on multiple fields for uniqueness in the database can be handled this way. The checkwith class must extend Check. An example will make it more clear
Read more…

Playframework: Comparing flash parameters for lookup tables

After working with play a bit more I ran into an issue with the flash parameters. If you validate some input form, and there are errors, you can store the previously filled parameters in the flash scope. After that you can re-populate the content of the form again with the flash parameters. However if you have a selection with a lookup table, this will most of the times fail.

Here is a bit of code to explain this situation in more detail. In the html there is a list of entities available:

<select name="orderline.id" id="orderline_id">
  <option value="">Select one
  #{list items: orderlines, as: 'orderline'}
    <option value='${orderline.id}' #{if flash['orderline.id'] != null && flash['orderline.id'] == orderline.id}selected="selected">${orderline.name}
  #{/list}
</select>

Read more…

Play framework impressions

The play framework is a web action-based framework that allows you to create web applications in fast and simple way. The play framework can start fast and reload your java classes in a fast way without restarting your application. Play focuses on developer productivity and targets RESTful architectures. Play doesn’t use a java webserver to start the application so the webserver starts up fast, but it does use many standard java libraries, like jpa with hibernate. Another nice feature of play is that you can start it up in test mode and then you have an embedded selenium test available to you.

Models
You can use existing jpa entities or create new ones extending from the Model that play provides. Extending this base class will get you an id and a number of find methods that can be used as static methods.
The properties are set as public fields in these model classes. This looks like a no-no to any experienced java developer, but if you think about it a bit more, this may not be such an awfull thing. Most models that I have seen are nothing more then a bunch of getters and setters for some properties. So why not make them public. The play framework will create the getters and setters and will use them, however this is done at run time.

Read more…

Common lift gotcha’s

Going to keep this here so maybe others will find this usefull as well and I will adding more gotcha’s here as I ran into them. So this basically is an overview to my thickness to understand lift correctly 🙂

lift Error locating template Message: Expected ‘;’
What this means is, that I copied an example of a snippet that does not work (anymore) on my computer. Using scala 2.8.1 and lift 2.2, the example of the simply lift website is like this:
<div id="main" class="lift:surround?with=default&at=content">

However it should be:
<div id="main" class="lift:surround?with=default;at=content">

Notice the amp between default and at was changed into a ‘;’
Read more…

When trying to learn lift

use a goooood editor.

Today I was trying to get a really simple form working in lift, using the designer friendly approach. I kept on stumbling on the same kind of error:
error: value #> is not a member of java.lang.String
"#name" #> SHtml.text(name, name = _) &

and I could not figure out what was wrong with the code that I had practically copied from the simply lift web site. I kept trimming statements from my code till I had practically nothing left but a simple statement.
Read more…

Access control for dwr with spring

With dwr it is possible to define methods on a bean that can be invoked from dwr-ajax calls. That much is easily to achieve following all the default tutorials on dwr on the web. However I wanted to restrict access to certain methods based on the roles that the users have. Otherwise it would be possible for certain users to call ajax methods that are not allowed for that user. But how can I change that behaviour. Looking at the documentation it is not obvious how that can be done, so I started the debugger to see how the integration really works. After some researching I came upon the DwrController that has a method call to ContainerUtil.setupDefaults. In here you can change the default behaviour classes, like the DefaultAccessControl class. So after having changed my own AccessControl implementation all I had to do is add it as parameter to the dwrController tag like so:
Read more…

%d bloggers like this: