8

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? 
    }
}

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Sergio
  • 3,317
  • 5
  • 32
  • 51

3 Answers3

8

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

As described here: 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'

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.


UPDATE

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();
}
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Sergio
  • 3,317
  • 5
  • 32
  • 51
0

You can choose between those two solutions:

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();
    }
}
madx
  • 6,723
  • 4
  • 55
  • 59
  • And how is this different from the update in the accepted and highly upvoted answer? Just the 'injection' part? That is 'trivial' and not really related to the problem – Kukeltje Jun 14 '16 at 18:31
-1

java class file

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

in faces-config.xml

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

visit this link

Mohsin AR
  • 2,998
  • 2
  • 24
  • 36