tags:

views:

167

answers:

1

Hi there!

im trying to get a controller to return a view through a Expression Language-Filter, but have no idea on how to get jersey to use EL for filtering a view.

View with EL-tags:

<html>
    <title>%{msg}</title>
</html>

Controller:

@GET
@Produces("text/html")
public Response viewEventsAsHtml(){
    String view=null;
    try {
        view=getViewAsString("events");
    }catch(IOException e){
        LOG.error("unable to load view from file",e);
        return null;
    }
    Response.ResponseBuilder builder=Response.ok(view, MediaType.TEXT_HTML);
    return builder.build();
}

How would one go about in order to get the controller to replace the ${msg} part in the view by some arbitrary value?

+1  A: 

If you are using Jersey then it provides the ability to return a Viewable from the Resource which will process jsp by default.

Example Jersey Resource

@Path("/patient")
public class PatientResource {
    @GET @Path("/{patientId}") @Produces(MediaType.TEXT_HTML)
    public Viewable view(@PathParam("patientId") int patientId) {
        return new Viewable("/patient.jsp", Integer.toString(patientId));
    }
}

Example patient.jsp

<span>${it}</span>

NOTE: Jersey maps the object you pass to the Viewable as "it" in the jsp.

Once you have Jersey forwarding to the jsp then you just need to add an EL implementation to your application server or servlet container.

Alex Winston