开发者

RESTeasy and Returning to a JSP page with a model

Is there an easy, not using spring, way to have RESTeasy return a jsp or html page with a model? I want to do something similar to the spring ModelAndView where I have a request to say /contacts/loomer and have it return a mocked up object in a jsp template. All of the examples I see are for JSON/XML. I know in Jersey you can use the viewable, but I need to use only RESTeasy stuff.

Thanks!

I want something like this (but withou开发者_如何学编程t the spring modelandview):

   @POST
   @PUT
   @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
   @Produces(MediaType.TEXT_HTML)
   public ModelAndView saveContactForm(@Form Contact contact)
         throws URISyntaxException
   {
      service.save(contact);
      return viewAll();
   }


Okay, I figured it out for anyone who is interested. It was actually fairly trivial once I found an example.

@GET
@Path("{eventid}")
@Produces("text/html")
public void getEvent(@Context HttpServletResponse response,
        @Context HttpServletRequest request,
        @PathParam("eventid") Long eventid) throws ServletException,
        IOException {

    EventDao eventdao = DaoFactory.getEventDao();
    Event event = eventdao.find(eventid);

    request.setAttribute("event", event);
    request.getRequestDispatcher("eventView.jsp").forward(request, response);

    }


Using org.jboss.resteasy.resteasy-html version 3.0.6.Final you can directly access the HttpServletRequest and inject your own attributes before directing output to a RESTEasy View.

@GET
@Path("{eventid}")
@Produces("text/html")
public View getEvent(@Context HttpServletResponse response,
                     @Context HttpServletRequest request,
                     @PathParam("eventid") Long eventid){

    EventDao eventdao = DaoFactory.getEventDao();
    Event event = eventdao.find(eventid);

    request.setAttribute("event", event);
    return new View("eventView.jsp");
}

This emulates some behavior of the Htmleasy plugin without having to rewire your web.xml.


I have voted up the above answer but it seems to work OK with RestEasy up to 2.3.2.Final, latest is 2.3.5.Final (for today). It seems to work OK with Jersey bundled with Glassfish 3.1.2.2 too.

This doesn't work with the RestEasy above 2.3.2.Final when I tried. I thought to share this observation as it took me a while to figure out what caused 'java.lang.ClassCastException: $Proxy262 cannot be cast to org.apache.catalina.core.ApplicationHttpRequest'

However I am not trying to dive deep how to solve it, I came across some thoughts https://stackoverflow.com/a/5149950/1398360

Cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜