0

I'm trying to get the row index of a JSF data table in a scriplet.

With a JSF component, it works like a charm:

<h:outputText value="#{Operations.postsDataTable.rowIndex}" /> 

But when I use a scriptlet like this:

<% out.println(Operations.getPostsDataTable().getRowIndex()); %>

Then it will print -1 for each row. What am I doing wrong?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Myth
  • 1

1 Answers1

2

What am I doing wrong?

To the point, using scriptlets while you shouldn't. Their use is discouraged since JSP 2.0 a decade ago.

Your concrete problem is caused because they run during view build time, not during view render time, exactly like as JSTL and all other taghandlers. This is explained in detail in this answer: JSTL in JSF2 Facelets... makes sense? Just substitute "JSTL" with "scriptlets" to understand what's going on. Summarized: they doesn't run "in sync" with JSF components. They get executed during building the JSF component tree, not during generating of the HTML output by the JSF component tree as you seemed to expect.

By the way, in JSP's successor Facelets (XHTML), it's already impossible to use scriptlets, so you're forced to do things "the right way" already.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • thanks for the fast answer... i want to use anchor points in outputtexts, so it will go something like this: < h:outputText id="#{Operations.postsDataTable.rowIndex}"/ > , but it gives me the error "according to the tdl, the attribute id is not deferred value or deferred method, but the specify value contains a # expression"... any ideas how to make it work? i replaced the # with $ but it gives me other error (java.lang.IllegalArgumentException: -1) – Myth Aug 12 '12 at 00:03
  • The `id` attribute is also evaluated during view build time (you should also have figured that if you have clicked the "JSTL in JSF2" link...), with the simple reason that there's physically only one output text component there in the JSF component tree, not multiple as you seemed to expect. This one and same component is reused multiple times during view render time. Your concrete functional requirement is not exactly clear, but I believe you're looking for a `` instead. – BalusC Aug 12 '12 at 00:40
  • it doesn't let me use the code you wrote.. without # it doesn't work either... any other suggestions? so you can understand better what i want to do: i'm making a forum, and each post should have an anchor point, so when the link will be "...index.jsp#5", the scroll will move to post number 5. – Myth Aug 12 '12 at 04:47
  • Just use `` then. – BalusC Aug 12 '12 at 10:29