user1073494
user1073494

Reputation:

How to call an external url from JSF 2 passing GET parameter generated inside the backing bean

I have something like this inside a p:dataTable

<h:commandLink action="#{myMB.connect}" icon="ui-icon-person" title="Connect" target="_blank">  
<f:setPropertyActionListener value="#{site.name}" target="#{myMB.siteId}"  />
Connect 
</h:commandLink>

connect() calculates a unique ID that I would like to add to the request, like this

UUID id = UUID.randomUUID();
//do some things here with the id
return("./terminal.jsp?id="+id.toString());

this fails because id arrives as null at terminal.jsp.

Upvotes: 1

Views: 1719

Answers (3)

Zak
Zak

Reputation: 327

Instead of

return("./terminal.jsp?id="+id.toString());

you want

FacesContext.getCurrentInstance().getExternalContext().redirect("./terminal.jsp?id="+id.toString())

Returning a String from an action method can only navigate to a page within the current webapp. If you want to navigate to an external webapp you have to use redirect() method of the external context and just return null.

Upvotes: 1

Cesar Loachamin
Cesar Loachamin

Reputation: 2738

Since JSF 2.0 you have the h:link control to make a get request so you can use in this way:

<h:link outcome="terminal.jsp">
    <f:param name="id" value="#{myMB.siteId}" />
</h:link>

If you really want to make a get request you can send a redirect to the jsp, you need to change the action method to an action listener so:

public void makeRequest(ActionEvent event) throws IOException {
        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        response.sendRedirect("terminal.jsp?id="+id.toString());
}

<h:commandLink actionListener="#{myMB.makeRequest}" icon="ui-icon-person" title="Connect" target="_blank">

Upvotes: 1

Ernesto Campohermoso
Ernesto Campohermoso

Reputation: 7371

Maybe you must write a simple servlet and use RequestDispatcher. And instead of h:commandLink you can use a h:outputLink. Then:

Create a servlet.

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
@WebServlet("/myservlet")
public class MyServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
                                 throws ServletException, IOException {
        UUID id = UUID.randomUUID();
        RequestDispatcher rd=request.getRequestDispatcher("terminal.jsp");
        request.setParameter("id", id.toString());
        rd.forward(request, response);
    }
}

Change the commandLink by

<h:outputLink value="#{request.contextPath}/myservlet" title="Connect" target="_blank">  
    <f:param name="siteName" value="#{myMB.siteId}" />
</h:commandLink>

Upvotes: 0

Related Questions