0
votes

Doing a CRUD, I have a RequestScoped Ticket bean.

I have an XHTML page that until now I have been using to create the new Tickets; the ticket fields are populated directly (#{ticket.description} and so on)

Now I have a search form that lists the tickets, and with each ticket a link with the ticket id as a parameter. I want the backing bean to retrieve the ticket bean from EJB/JPA (already done) and put it into the request. I see 3 ways to do so:

  • Copy the data from the bean retrieved from JPA into the bean provided by injection. Ugly / prone to omissions.
  • Use ExternalContex#getRequestMap and put the bean there myself. Does not look very proper. Am I right?
  • Include my ticket bean into another bean class so I can do myNewBean.setTicket(ticketFromJpa);. Seems the best of the options, yet I do not like having to prefix all my EL in the page just for this.

There is out there any cleaner, more proper way of doing what I want?

Thanks in advance.

UPDATE: To reword what I want, with a little more information. I have a commandLink in page P1, that calls action A in backing bean B1. B1 does its logic and redirects to page P2. I wanted to set a (request scoped) bean B2 in action A, and that B2 would be available to draw P2.

From experiments, I have found that after leaving action A the framework creates a new B2 request scoped bean, so it looks like that the request scope is shorter than I expected.

The flash scope propesed by Damian looks like it works more like I want, but forces me to redesign the page around #{flash} and that (when I want to use it to create a new bean) I must also add the bean to the flash in an action method (currently it justs goes to the page and the managed bean is available)

I expected a Request scoped bean to be maintained since

1
why do you need to put the ticket in the request? what do you want to do next? - damian
I want to pass the ticket to the .xhtml page so it renders the ticket data. - SJuan76

1 Answers

4
votes

To view the ticket in another page, you could do one of the following:
1) use h:link with a f:param containing the ID of the ticket

<h:link value="#{ticket.description}" outcome="/viewTicket" >
    <f:param name="id" value="#{ticket.id}" /
</h:link>

Then in the viewTicket.xhtml (or whatever you called the page) you read the ID parameter, and get the ticket from JPA.

This is how the managed would basically look like:

@ManagedBean
@ViewScoped
public class ViewTicketMBean implements Serializable {

    private String ticketId;
    private Ticket ticket;

    @PostConstruct
    public void init() {
        ticketId = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");
        // read ticket from JPA by ID
    }

}

2) instead of h:link, if you don't want to expose ticket ID, you can use a commandLink, which before navigating to viewTicket.xhtml, gets the ticket from JPA and puts it in flash scope. Then, in viewTicket you get the ticket from the flash scope. This is how the action method of the commandLink couldl look like

Ticket ticket = null;
// get ticket from JPA    
FacesContext.getCurrentInstance().getExternalContext().getFlash().put("ticket", ticket);
return "/viewTicket";