2013-10-20 19 views
7

私はJAX-RSリソースを持っており、ビジネスロジックを解決した後、その結果をJSFページに表示したいと考えています。どうやってやるの?JAX-RSメソッドからJSFページにリダイレクトする方法は?

@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は、その行うリダイレクトに基づいていくつかのロジックを行います。

答えて

7

まあ、私はJAX-から前方への道を見つけましたRSメソッドからJSFページへ:

@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

注入はフィールドに注射を介してもを行うことができ、それは「好み」の問題である

このTomEE(ApacheのCXF)でテストされています。私はこれが単なる汚れた "ハック"か、それを行うためのより良い方法があるなら、ちょっと好奇心旺盛です。


UPDATE

私は何の問題もなくJSFタグをレンダリングすることに良い方法はをリダイレクトした(私の場合には、そのような<p:graphicImage/>などのタグが正しくレンダリングされませんでした)

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

のfaces-config.xmlでのJavaクラスファイル

public String processPage1(){ 
     return "page"; 
    } 

<navigation-case> 
     <from-action>#{navigationController.processPage1}</from-action> 
     <from-outcome>page</from-outcome> 
     <to-view-id>page1.jsf</to-view-id> 
    </navigation-case> 

訪問はこのlink

+0

リダイレクトのためにこれを行う: '' page1?faces-redirect = true '' –

+0

この[リンク]を実行する(http://www.mkyong.com/jsf2/jsf-page-forward-vs-page-redirect /) –

+0

これはちょうどjsfリダイレクトです。jax-rsメソッドの中には、返されたStringが表示されます。 – Sergio

0

これらの2つのソリューションを選択できます。

import java.io.IOException; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.Context; 
import javax.ws.rs.core.Response; 
import javax.ws.rs.core.Response.Status; 

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

    @Context 
    private HttpServletRequest request; 
    @Context 
    private HttpServletResponse response; 

    /** 
    * This method uses the injected request/response in the PaymentServiceRest class 
    * @return 
    * @throws IOException 
    */ 
    @GET 
    @Path("/status1") 
    @Produces("text/html") 
    public Response method1() throws IOException { 
     String myJsfPage = "/views/index.xhtml"; 
     String contextPath = request.getContextPath(); 
     response.sendRedirect(contextPath + myJsfPage); 
     return Response.status(Status.ACCEPTED).build(); 
    } 

    /** 
    * This method uses the injected request/response passed as parameters 
    * @return 
    * @throws IOException 
    */ 
    @GET 
    @Path("/status2") 
    @Produces("text/html") 
    public Response method2(@Context HttpServletRequest request, @Context HttpServletResponse response) 
      throws IOException { 
     String myJsfPage = "/views/index.xhtml"; 
     String contextPath = request.getContextPath(); 
     response.sendRedirect(contextPath + myJsfPage); 
     return Response.status(Status.ACCEPTED).build(); 
    } 
} 
+0

これは、受け入れられていて、非常に上書きされた回答の更新とどう違うのですか?ちょうど「注射」部分?それは「些細な」ものであり、実際には問題に関係していない – Kukeltje