0

I've developed an entire JSF project with the redirect mechanism and it works quite well. Since I want to improve the performance, I think forward mechanism is a good way to do it. Since I'm facing some problem, I want some help doing this. I'm going to show how I'm using the navigation in my app:

    @Named(value = "customersView")
@RequestScoped
public class CustomersView implements Serializable {

public String onNewElement() {
        return "create?faces-redirect=true";
    }

    public String onEditElement(Customer seleceted) {
        this.selected = seleceted;
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("customer", seleceted);
        return "create?faces-redirect=true";
    }

This is my list.xhtml file:

<p:menuButton style="float:right;" icon="ui-icon-person-add"
                                value="Add customer">
                                <p:menuitem value="New" icon="ui-icon-create"
                                    outcome="#{customersView.onNewElement()}" />

Then I've the components that handle the creation of a Customer:

@Named
@javax.faces.view.ViewScoped
public class CreateCustomerView implements Serializable {

    @Inject
    CustomerController customerController;
    private Customer customer;
    private boolean edit;

@PostConstruct
    public void init() {
        edit = FacesContext.getCurrentInstance().getExternalContext().getFlash().containsKey("customer");
        if (edit) {
            this.customer = (Customer) FacesContext.getCurrentInstance().getExternalContext().getFlash()
                    .get("customer");
            FacesContext.getCurrentInstance().getExternalContext().getFlash().keep("customer");
        } else {
            this.customer = new Customer();
        }
        this.municipalities = new MunicipalityParser().parse();
        this.provinces = new ArrayList<>();
        for (Municipality municipality : municipalities) {
            if (!provinces.contains(municipality.getProvince())) {
                provinces.add(municipality.getProvince());
            }
        }
    }

    public String onCompletedSave() {
        customerController.store(customer);
        try {
            new CardCreator().createCard(customer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_INFO,
                        "The customer " + customer.getSurnameAndName() + " has been saved correctly",
                        "Coorect!"));
        FacesContext.getCurrentInstance().getExternalContext().getFlash().setKeepMessages(true);
        return "list?faces-redirect=true";
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

}

How Can I switch to forward mechanism in the simplest way?

This is the main menu:

<pu:menu widgetVar="me">
                    <p:menuitem id="um_dashboard" value="Home" icon="&#xe8d1;"
                        outcome="/s/dashboard" />
                    <p:submenu id="registries" label="My menu" icon="&#xe7fd;">
                        <p:menuitem id="customers" value="Customers"
                            outcome="/s/customers/list" />
                        <p:menuitem id="children" value="Children"
                            outcome="/s/children/list" />
                        <p:menuitem id="schools" value="Scuole" outcome="/s/schools/list" />
                    </p:submenu>

Is it possible to implement this mechanism also for this?

I love coding
  • 1,183
  • 3
  • 18
  • 47
  • I'm not sure what you want to achieve or what your observed/expected behavior is. Did you try `outcome="/s/dashboard?faces-redirect=true"`? I suppose this is a good starting point for reading (don't skip the **see also** sections): https://stackoverflow.com/questions/8459903/creating-master-detail-pages-for-entities-how-to-link-them-and-which-bean-scope – Selaron Nov 03 '19 at 21:16
  • The scope is use the forward mechanism to move within my JSF project, since it is faster respect the redirect one. Hope now you get the point! – I love coding Nov 03 '19 at 21:21
  • So you want e.g. your `outcome="#{customersView.onNewElement()}"` menuItem to render a simple link that does not trigger a POST at all but is a simple bookmarkable reference? Did you try to use the `url` attribute? – Selaron Nov 03 '19 at 21:37
  • Nope. The idea is to avoid to load the CSS and JS when I move to one page to another one. As mu knowledge, the mechanism should be the forward, whereas I’using the reditect. – I love coding Nov 04 '19 at 05:20
  • 2
    'Forward' does in no way improve the performance by not loading css and js. In normally configured servers these should be cached in the browser. The only thing that you save with not doing a redirect is the redirect. But that is very quick, so the net gain is next to none. If you want to really improve things, learn about ajax and do partial page updates where possible. – Kukeltje Nov 04 '19 at 07:55
  • 3
    Navigation should absolutely not be chosen based on "performance". If you change from redirect for URLs for which it apparently works perfect back to forward, then you'll run into the same trouble as the asker of this question: https://stackoverflow.com/q/15521451 – BalusC Nov 04 '19 at 09:44
  • @Ilovecoding: Any feedback about the last 2 comments? – Kukeltje Nov 07 '19 at 08:47
  • The answer doesn't fully reply to my question. The idea is just to improve the performance, by reducing the load of css and js during each navigation. @Kukeltje – I love coding Nov 07 '19 at 13:32
  • 1: there is no answer to your question 2: the link posted by BalusC shows what new 'problem' you might run into when switching, 3: there is **NO LOAD** of css and js on each navigation on a correctly configured environment (remember the browser cache comments)... And with a forward you still have this... See https://stackoverflow.com/questions/11277366/what-is-the-difference-between-redirect-and-navigation-forward-and-when-to-use-w where the NEW PAGE is always retrieved, including css/js references. – Kukeltje Nov 07 '19 at 14:21
  • Thank you for your deeper comment! – I love coding Nov 07 '19 at 15:46

0 Answers0