如何从JAX-RS方法重定向到JSF页面? [英] How to redirect to JSF page from JAX-RS method?

查看:104
本文介绍了如何从JAX-RS方法重定向到JSF页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JAX-RS资源,解决了业务逻辑后,我想在JSF页面中显示结果.我该怎么办?

I have a JAX-RS resource and after resolve business logic I want to show the results in a JSF page. How can I do that?

@Path("/rest")
public class PaymentServiceRest {

    @GET
    @Path("/status")
    public String method()  {

        // Business logic...

        return "results.xhtml"; // how to return a jsf page? 
    }
}

客户端第一次访问该应用程序是使用url,即http://myApp/rest/status,然后执行一些逻辑并基于此进行重定向.

The first time the client access the app is using the url, i.e: http://myApp/rest/status, then do some logic and based on that do a redirection.

推荐答案

好吧,我找到了一种方法,可以从JAX-RS方法转发到JSF页面:

Well, I've found a way to forward from a JAX-RS method to a JSF page:

@GET
@Path("/test")
@Produces("text/html")
public Response test(@Context ServletContext context,
        @Context HttpServletRequest request,
        @Context HttpServletResponse response) {

    try {
        String myJsfPage = "/response.xhtml";
        context.getRequestDispatcher(myJsfPage).forward(request, response);
    } catch (ServletException | IOException ex) {
        return Response.status(NOT_FOUND).build();
    }
    return null;
}

如此处所述: https://www .java.net//forum/topic/glassfish/glassfish/forwarding-jsf-jax-rs

通过该方法进行注入也可以通过在字段中进行注入,这是首选项"的问题

The injection via the method can also be done via injection in fields, that is a matter of 'preference'

这已在TomEE(Apache CXF)中进行了测试.我只是有点好奇,如果这只是一个肮脏的"hack",或者是否有更好的方法可以做到这一点.

This have been tested in TomEE (Apache CXF). I'm just a little curious if this is just a dirty "hack" or if there is a better way to do it.

更新

我找到了一种重定向的更好的方法,该方法可以呈现JSF标记而没有任何问题(在我的情况下,诸如<p:graphicImage/>之类的标记无法正确呈现)

I found a better way to redirect that renders the JSF tags without any issues (in my case tags such as <p:graphicImage/> were not rendering properly)

@GET
@Path("/test")
@Produces("text/html")
public Response test(@Context HttpServletRequest request, @Context HttpServletResponse response)
        throws IOException {
    String myJsfPage = "/response.xhtml";
    String contextPath = request.getContextPath();
    response.sendRedirect(contextPath + myJsfPage);
    return Response.status(Status.ACCEPTED).build();
}

这篇关于如何从JAX-RS方法重定向到JSF页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆