2

When I open jsf page then catalog bean creates two times. So why does it happen? jsf page:

<h:dataTable value="#{catalog.products}" var="p">
    <h:column>
        <h:form>
            <h:commandLink action="detail?faces-redirect=true"
                value="#{p.brand} #{p.model}">
                <f:setPropertyActionListener value="#{p}"
                    target="#{ph.currentProduct}" />
            </h:commandLink>
        </h:form>
    </h:column>
</h:dataTable>

bean:

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.inject.Inject;
import javax.inject.Named;

import dataLayer.Dao;

@Named
@RequestScoped
public class Catalog implements Serializable {

    @Inject
    private Dao dao;

    private List<Product> products;
    HtmlDataTable dataTable;

    public Catalog() {
    }

    @PostConstruct
    private void init() {
        System.out.println("catalog bean created");
        dataTable = new HtmlDataTable();
        products = dao.getShavers();
    }
...
rozerro
  • 5,787
  • 9
  • 46
  • 94
  • Which server do you use? – sinclair Mar 06 '16 at 11:13
  • @sinclair JBoss AS 7.1 – rozerro Mar 06 '16 at 11:14
  • I'm just guessing, but I think the second bean is created to handle the following request faster by initializing the bean before the request. Take a look at the Jboss documentation about "bean pooling". – sinclair Mar 06 '16 at 11:21
  • 1
    `HtmlDataTable` property doesn't belong there. Get rid of it. The `` there is also strange. I'd expect to see a `` there. This is perhaps helpful as to guidelines for this kind of master-detail approach: http://stackoverflow.com/q/8459903 As to the concrete problem, just put a breakpoint in `init()` method and check call stack who/what/why called it. – BalusC Mar 06 '16 at 12:20
  • @BalusC `HtmlDataTable` property is there for pagination purposes so if you mean the thouble is from that HtmlDataTable then I need to find another pagination aproach. – rozerro Mar 06 '16 at 19:22
  • It wasn't an answer but a comment. Problem is not visible in information provided so far. – BalusC Mar 06 '16 at 19:50
  • @BalusC Actualy the reason was in `commandLink's`. From any page where was `commandLink` to `catalog.xhtml` the catalog bean created twice so replacement with `h:link` solved the issue. But how actualy the issue is related to `commandLink` behaviour? . – rozerro Mar 06 '16 at 23:07
  • 1
    Because you're sending a redirect after postback which destroys the current request and creates a new request. As the bean is request scoped, it will be affected. – BalusC Mar 07 '16 at 06:20
  • @BalusC I used `?faces-redirect=true` with `commandLink's` mostly to get right `url's` in browser so when not using redirects the links will remains old so how to preserv right links in this case when using commanLinks without redirect? – rozerro Mar 07 '16 at 09:22
  • 1
    Food for read: http://stackoverflow.com/questions/15521451/how-to-navigate-in-jsf-how-to-make-url-reflect-current-page-and-not-previous-o This is also linked as 1st "See also" link in the duplicate. Try reading them as well, in my answers, they usually already answer spinoff questions :) – BalusC Mar 07 '16 at 09:25

0 Answers0