0

I have a java class named Test which has an attribute Integer id (auto-increment). It's initialized with value 1.

In my xhtml file I want to give every column I create, the id from the java bean class.

And in another class (java bean class) named AnotherClass with annotation :

@ManagedBean(name = "myClass")
@ApplicationScoped

And I have a list of Test in this class.

This is what I tried to do, but it doesn't recognize integers as value for id attributes. In the documentation it says that only strings are allowed. How can I proceed ?

<c:forEach items="#{myclass.list}" var="test">
    <p:column id="#{test.id}" headerText="#{test.header}">
</c:forEach>
Gatsby
  • 365
  • 1
  • 5
  • 17
  • 1
    In html an ID cannot start with a number. JSF honors this... https://stackoverflow.com/questions/5972433/what-are-the-rules-for-a-jsf-id – Kukeltje Aug 24 '17 at 12:07
  • 2
    Possible duplicate of [What are the rules for a JSF id?](https://stackoverflow.com/questions/5972433/what-are-the-rules-for-a-jsf-id) – Kukeltje Aug 24 '17 at 12:11
  • Next time also try with a simple plain `h:inputText` or whatever. If that fails, you **know** it is not column related.... Simplify... [mcve] – Kukeltje Aug 24 '17 at 12:13
  • @Kukeltje I see, I will edit the title then. So there aren't any way to do what I want ? – Gatsby Aug 24 '17 at 12:17
  • For now I've just added a caracter before the integer : id="t#{test.id}", but it's kind of ugly. – Gatsby Aug 24 '17 at 12:24
  • It is not ugly... it is working within html4 (and css) limitations... You could also make it `"col#{test.id}"` html5 allows it but css still does not – Kukeltje Aug 24 '17 at 12:37
  • html5 allows to begin the id with an integer ? – Gatsby Aug 24 '17 at 21:03
  • It seems so but css still does not really so it is uncommon. https://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html – Kukeltje Aug 25 '17 at 07:54

1 Answers1

0

You can implement a method called String getIdAsString() then use it instead of id getter:

<c:forEach items="#{myclass.list}" var="test">
    <p:column id="#{test.idAsString}" headerText="#{test.header}">
</c:forEach>

The getIdAsString() method should give some prefix from letters [a-z|A-Z] becuse an html id is not allowed to start with digit. So the implementation could be:

public String getIdAsString() {
  return "id"+id.toString();
}
Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
  • I've tried this before and it says : java.lang.IllegalArgumentException: 2 (2 is the value of id) – Gatsby Aug 24 '17 at 11:54
  • @Gatsby checkout my answer again – Krzysztof Cichocki Aug 24 '17 at 12:00
  • @Kukeltje from where do you infer that the implementation of `getIdAsString` method is returning an invalid html id? It is up to the @Gatsby how He will implement it. I didn't say that He should (or not) return just digits. So your downvote is somehow inadequate. – Krzysztof Cichocki Aug 24 '17 at 12:17
  • @KrzysztofCichocki As mentionned, you're solution isn't working but thanks anyway. – Gatsby Aug 24 '17 at 12:19
  • @Gatsby please do check again with the suggested implementation of `getIdAsString()` – Krzysztof Cichocki Aug 24 '17 at 12:24
  • @KrzysztofCichocki yes this is what I finally decided to do. But directly in the id attribute. – Gatsby Aug 24 '17 at 12:26
  • You did miss a lot of stating things explicitly AND trying... and relying on my help... I still think this is not the right solution and the one @Gatsby used (and what I mentioned in my previous comment) is the better way to go... – Kukeltje Aug 24 '17 at 12:33
  • @Kukeltje this is SO, we all rely here on helping each other. – Krzysztof Cichocki Aug 24 '17 at 12:52