RESTeasy 并返回带有模型的 JSP 页面

发布于 2024-10-01 01:20:27 字数 534 浏览 5 评论 0原文

有没有一种简单的、不使用 spring 的方法让 RESTeasy 返回带有模型的 jsp 或 html 页面?我想做一些类似于 spring ModelAndView 的事情,我有一个请求说 /contacts/loomer 并让它在 jsp 模板中返回一个模拟对象。我看到的所有示例都是针对 JSON/XML 的。我知道在泽西岛你可以使用可视的,但我只需要使用 RESTeasy 的东西。

谢谢!

我想要这样的东西(但没有弹簧模型和视图):

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

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 without 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();
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜夜流光相皎洁 2024-10-08 01:20:27

好吧,我已经为有兴趣的人弄清楚了。一旦我找到一个例子,这实际上是相当微不足道的。

@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);

    }

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);

    }
╭ゆ眷念 2024-10-08 01:20:27

使用org.jboss.resteasy.resteasy-html版本3.0.6.Final,您可以直接访问HttpServletRequest并在定向之前注入您自己的属性输出到 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");
}

这会模拟 Htmleasy 插件的某些行为,而无需重新连接 web.xml

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.

紫轩蝶泪 2024-10-08 01:20:27

我已经投票赞成上述答案,但它似乎可以与 RestEasy 一起使用,最高可达 2.3.2.Final,最新的是 2.3.5.Final(今天)。与 Glassfish 3.1.2.2 捆绑在一起的 Jersey 似乎也可以正常工作。

当我尝试时,这不适用于 2.3.2.Final 之上的 RestEasy。我想分享这个观察结果,因为我花了一段时间才弄清楚是什么导致了'java.lang.ClassCastException:$Proxy262无法转换为org.apache.catalina.core.ApplicationHttpRequest'

但是我是没有试图深入研究如何解决它,我遇到了一些想法 https://stackoverflow.com/a/5149950/1398360

干杯

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文