0

I have two pages, customer.xhtml and detailsCustomer.xhtml. Each one has its own Controller. I'm passing a parameter from customer to detailsCustomer.

The button in customer.xhtml:

<p:commandButton process="@this" update="@form" title="Button" 
 actionListener="#{customerController.showDetails(register)}"
 icon="ui-icon-suitcase" /> 

The function in CustomerController:

public void showDetails(Customer c){
  redirect("/customer/detailsCustomer.faces?customerId=" + c.getCustomerId());
}

In DetailsCustomerController

public DetailsCustomerController(){
  ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
  strCustomer = ((HttpServletRequest) context.getRequest()).getParameter("customerId");
}

@PostConstruct
public void Init(){
  registerCustomer = customerService.loadCustomer(Long.parseLong(strCustomer));
}

The first time works perfect, but if I close detailsCustomer (redirecting to customer) and select a new customer it keeps showing the last customer. Because the page is already built. I need that each time I press Button the page loads with the customer I selected, but when I debug I see that it's only going through the constructor and Init the first time I pressed Button.

Is it possible? Any idea on how to do it? I'll appreciate any help.

Malincy Montoya
  • 87
  • 1
  • 13
  • What is this supposed to do? The ajax `update="@form"` suggests you only want to update part of the page (thus, stay on `customer.xhtml`) but your action indicates a page change. For this, you better use a simple ``. – mabi Apr 14 '14 at 22:07
  • For GET vs POST navigation see for example this answer: http://stackoverflow.com/a/4317723/785663 – mabi Apr 14 '14 at 22:10
  • @mabi I edited my question with more code. I tried the h:button but I'm not getting the parameter. I did this on my constructor `public DetailsCustomerController(){ Map params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap(); strCustomer = params.get("customerId"); }` – Malincy Montoya Apr 14 '14 at 23:02
  • Just drop the idea. What you are trying to do makes no sense in JSF (mixing ajax with redirect, with GET, with partial update...). Try to describe the effect you wish to achieve, there surely is a simpler, more JSF-friendly way. – fdreger Apr 14 '14 at 23:11
  • @fdreger what I want to do is: every time I press a button redirect to a new page, managed by another bean, but I need it to have info of a customer. That's why I was trying to pass the id – Malincy Montoya Apr 15 '14 at 13:46

2 Answers2

0

Two options:

a) Just make a session bean with the info you want to persist, and include it as a managed property in your specific beans, or reference it directly from your view. For example, I have a session-scoped login bean which manages the user info and permissions.

After all, nobody says you have to only use ONE bean in your jsf pages :-)

b) If you're making a catalog/editor, perhaps you just need two views and one controller.

This is what I do. It may not be the best solution, but it works for me:

Have a customers.xhtml for listing, and a customerdetails.xhtml for edition (and newcustomer.xhtml for adding new customers), and have a managedproperty Customer object in your controller for the edition, resetting it whenever your customerId changes.

(Add a preRenderView handler to your controller to compare the customer's id with the GET parameter, so you'll load the details for a new customer in case the parameter changes. Chances are your customer data is null if you didn't edit a customer previously, so you also need to check on that).

So to edit a customer, redirect by passing the customer id as a GET parameter to customerdetails. Maybe you can just add a link for edition in each row of your customers list.

After updating the customer in your controller, issue a redirect to the same url so it won't be postback and have the user info refreshed.

Finally, don't forget to include a hidden input field (plain html input, NOT inputHidden) with your customer id and the same name as your GET param inside your customerdetails.xhtml.

I.e. if you pass a get parameter custId, use input name="custId" value="#{param.custId}. This way you can be sure that your parameter is always present in your editing pages, whether it's postback or not.

Good luck.

Rick Garcia
  • 167
  • 1
  • 4
0

I concluded that I need my page to reload each time, so I used @ViewScoped annotation in DetailsCustomerController. For my specific problem and workflow, it works.

Malincy Montoya
  • 87
  • 1
  • 13