Model view controller

Developers: Paul Sandoz
Status: ongoing

Jersey has support for pluggable view templates with a JSP implementation. See the bookstore example in the Jersey distribution for an example of how this works.

The API and implementation can be improved so that view templates can be picked up dynamically (no need to require the of use annotations) and the runtime model of a resource class modified accordingly.

Investigate how to improve the API/SPI to better support model/view/controller separation. The following presents an example of how views can be better connected to HTTP methods:

/**
 * View templates are placed in a directory that is the fully qualified
 * Class name of a resource class suitably converted to a file system path,
 * <class>.
 * The directory is relative to some base directory, <base>
 *
 * The type of view template is specified by the suffix <t>, for example "jsp",
 * "vel" (for velocity) "wick" (for wicket) etc.
 *
 * MIME media types may also be specified by a suffix, for example,
 * "html" "xhtml" "xml" after <t>
 *
 * The HTTP method may be declared before the suffix <t>.
 */
public class MyResource {
    /**
     * Look for template <base>/<class>/index.<t> if not found
     * look for template <base>/<class>/index.get.<t>
     */
    @View
    @GET
    public String get() {
        return "get-model";
    }

    /**
     * Look for template <base>/<class>/index.post.<t>
     */
    @View
    @POST
    public String post() {
        return "post-model";
    }
   
    /**
     * Look for template <base>/<class>/sub.<t> if not found
     * look for template <base>/<class>/sub.get.<t>
     *
     * Uri template is required to be a path segment with no template values.
     *
     * return the resource class as the model, so resource class =
     * controller+model
     */
    @View
    @Path("sub")
    @GET
    public MyResource getSub() {
        return this;
    }

    /**
     * Look for template <base>/<class>/sub.post.<t>
     */
    @View
    @Path("sub")
    @POST
    public String postSub() {
        return "post-sub-model";
    }
   
    /**
     * Look for an explicit template named "subEdit" at
     * <base>/<class>/subEdit.<t>
     */
    @View("subEdit")
    @POST("subEdit")
    @POST
    public String postSubEdit() {
        return "post-sub-edit-model";
    }
}


Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Sign up or Log in to add a comment or watch this page.


The individuals who post here are part of the extended Sun Microsystems community and they might not be employed or in any way formally affiliated with Sun Microsystems. The opinions expressed here are their own, are not necessarily reviewed in advance by anyone but the individual authors, and neither Sun nor any other party necessarily agrees with them.

Copyright 1994-2009 Sun Microsystems, Inc.
Powered by Atlassian Confluence
Sun Guidelines on Public Discourse Privacy Policy Terms of Use Trademarks Site Map Employment Investor Relations Contact