alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (PathBindable.java)

This example Play Framework source code file (PathBindable.java) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

mvc, pathbindable, play, play framework, string, t

The PathBindable.java Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play.mvc;

/**
 * Binder for path parameters.
 *
 * Any type <code>T</code> that implements this class can be bound to/from a path parameter.  The only requirement is
 * that the class provides a noarg constructor.
 *
 * For example, the following type could be used to bind an Ebean user:
 *
 * <pre>
 * @Entity
 * class User extends Model implements PathBindable<User> {
 *     public String email;
 *     public String name;
 *
 *     public User bind(String key, String email) {
 *         User user = findByEmail(email);
 *         if (user != null) {
 *             user;
 *         } else {
 *             throw new IllegalArgumentException("User with email " + email + " not found");
 *         }
 *     }
 *
 *     public String unbind(String key) {
 *         return email;
 *     }
 *
 *     public String javascriptUnbind() {
 *         return "function(k,v) {\n" +
 *             "    return v.email;" +
 *             "}";
 *     }
 *
 *     // Other ebean methods here
 * }
 * </pre>
 *
 * Then, to match the URL <code>/user/bob@example.com</code>, you could define the following route:
 *
 * <pre>
 * GET  /user/:user     controllers.Users.show(user: User)
 * </pre>
 */
public interface PathBindable<T extends PathBindable<T>> {

    /**
     * Bind an URL path parameter.
     *
     * @param key Parameter key
     * @param txt The value as String (extracted from the URL path)
     * @return The object, may be this object
     * @throws RuntimeException if this object could not be bound
     */
    public T bind(String key, String txt);

    /**
     * Unbind a URL path parameter.
     *
     * @param key Parameter key
     */
    public String unbind(String key);

    /**
     * Javascript function to unbind in the Javascript router.
     *
     * @return The javascript function, or null if you want to use the default implementation.
     */
    public String javascriptUnbind();

}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework PathBindable.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.