[aerogear-dev] HTML View Responder

Daniel Bevenius daniel.bevenius at gmail.com
Fri Dec 21 09:34:36 EST 2012


Hi,

I'm working on AEROGEAR-693 <https://issues.jboss.org/browse/AEROGEAR-693> and
trying to figure out how to implement this in the best way.

At the moment, the default type of ViewResponder is the MvcResponder which
forwards to a JSP page. By default, this means that if you do not specify a
*produces()* method on a route, this is what you'll get.

Now, the media types that are passed to the *produces()* method determine
two things: 1. How to match to Route that is the target of a request,
matching the HTTP *Accept* header. 2. Determining the ViewResponder to be
used.

The issue here is that for both a HTMLResponder and a JSPResponder the
media type would in both cases be*text/html*.
How do we determine which one the users actually has created a view for
(jsp or html page)?

Well, we need some way to differantiate between a route that will respond
with a html view, but which uses a ceratain technique to render the html.

How about a introduce a type, MediaType that looks something like this:

public class MediaType {

    public static final String HTML = "text/html";
    public static final String ANY = "*/*";
    public static final String JSON = "application/json";

    private final String mediaType;
    private final Class<? extends Responder> responderClass;

    private MediaType(final String mediaType, Class<? extends
Responder> responderClass) {
        this.mediaType = mediaType;
        this.responderClass = responderClass;
    }

    public String getMediaType() {
        return mediaType;
    }

    public Class<? extends Responder> getResponderClass() {
        return responderClass;
    }

    @Override
    public String toString() {
        return "MediaType[type=" + mediaType + ", responderClass=" +
responderClass + "]";
    }

    public static MediaType html() {
        return new MediaType(HTML, HtmlViewResponder.class);
    }

    public static MediaType jsp() {
        return new MediaType(HTML, JspViewResponder.class);
    }

    public static MediaType json() {
        return new MediaType(JSON, JsonResponder.class);
    }
}

And we could add *html()*, *jsp()*, and *json()* methods to
AbstractRoutingModule, so a route that should be handled by a
HtmlViewResponder would look like this:

route()
      .from("/plain")
      .on(RequestMethod.GET)
      .produces(html())
      .to(Html.class).index();

To be backward compatible, if a *produces()* method was not specified it
would be the same as specifying:

      .produces(json())
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/aerogear-dev/attachments/20121221/d2ed0eab/attachment.html 


More information about the aerogear-dev mailing list