0

I have this backing bean:

@ManagedBean(name="testController")
public class TestController {
    private String foo = "fooTest";
    private List<A> alist;

    public A fetchAlist(int index)  {
        alist = ListInflater.get(alist, A.class, index); //only used for incrementing list

        return alist.get(index);
    }

    public String getFoo() {
        return foo;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }
}

I'd like to invoke a property from class A by accessing fetchAlist(x) as propery in the page like this:

<html xmlns="http://www.w3.org/1999/xhtml"  
    xmlns:h="http://java.sun.com/jsf/html"  
    xmlns:f="http://java.sun.com/jsf/core"  
    xmlns:p="http://primefaces.org/ui"> 

 <h:head><title>Test</title></h:head>
 <h:body>
  <h:form>
    Test List
    <p:inputText value="#{testController.fetchAlist(2).aparam}" /><br /><br />
  </h:form>
</h:body>
</html>

Unfortunately that doesn't work yet, because EL understands this list as property but it isn't a property but a method. Is there any possibility to achieve such an invocation?

[UPDATE]
I figured out that all this works with h:inputText, so maybe this is a PrimeFaces bug?

Bevor
  • 8,396
  • 15
  • 77
  • 141

1 Answers1

1

You can just use the brace notation to reference a list item by index:

@ManagedBean
public class TestController {

    private List<A> alist;

    @PostConstruct
    public void init() {
        alist = createItSomehow();
    }

    public List<A> getAlist()  {
        return alist;
    }

}

with:

<p:inputText value="#{testController.alist[2].aparam}" />

If you prefer to perform lazy loading of each list item for some reason, then your best bet is to provide a custom List implementation wherein you do the desired job in List#get(int) method.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I don't understand how this should work (and it doesn't because it tries to access a null list. The application does actually do nothing). If I use standard getters and setters fpr the list, it is never instantiated. Moreover the usual getter doesn't provide my ListInflater which instantiates and generates as much entries within a list the index specifies. Maybe I can pass somehow the index of you notation to my list, but how? – Bevor Oct 18 '12 at 19:49
  • You need to preinitialize the list in (post)constructor. I've edited the answer, sorry I thought it was obvious enough. – BalusC Oct 18 '12 at 19:50
  • On a related note, I have no idea why you're trying to design it like that, but this is after all a smell given the fact that you apparently didn't had the need to preinitialize the list yourself. Perhaps you're actually looking for something like this? http://stackoverflow.com/questions/3409053/jsf2-can-i-add-jsf-components-dynamically/3409511#3409511 – BalusC Oct 18 '12 at 19:52
  • Sorry, this doesn't work either. Just for curiosity I added an ArrayList initialization in postConstruct as you said, the getter and setter for the list too, but when I access it like #{testController.alist[0].aparam}, nothing happens, the fiels stays blank (It should show a prefilled text, because aparam is initialized with a string). If I omit the [0], I get a "NumberFormatException"(?) for the input string aparam. If this solution would work, does it actually add as many entries as indices I write into the brackets? Because I'm not sure if this is so sophisticated. – Bevor Oct 19 '12 at 05:52
  • Ok, I see that JSF is not smart enough to add the values I need, so the values have already to exist. I think I understand what you mean now by "providing a custom list implementation". I will try this solution when I'm at home tonight and let you know then. – Bevor Oct 19 '12 at 06:16
  • Why should JSF/EL create the model? It has just to set the model values, as always. This has nothing to do with smartness. – BalusC Oct 19 '12 at 11:03
  • Yes, but we have some cases where we can't use an "add" button, so we need such a mechanism. Nevertheless we probably use h:inputText, because standard jsf can deal with this syntax, PrimFaces obviously not. Otherwise we will make our own list, as you proposed. – Bevor Oct 19 '12 at 17:36