2

How do I convert a JSP variable to a Struts2 variable?

I've tried the following:

<%=scoredDocument%>
<s:push value="scoredDocument"/>
<s:push value="#scoredDocument"/>
<s:push value="%{scoredDocument}"/>
<s:push value="${scoredDocument}"/>
<s:push value="#page.scoredDocument"/>
<s:push value="%{#page.scoredDocument}"/>

<display:column title="Study Code" sortable="true">
    <s:property value="id"/>

The most frequent error is

Caused by: tag 'push', field 'value': You must specify a value to push on the stack. Example: person - [unknown location]

Roman C
  • 49,761
  • 33
  • 66
  • 176
Chloe
  • 25,162
  • 40
  • 190
  • 357
  • 1
    You need to put the value on the value stack. But why are you trying to do this? What's the point? You shouldn't be calculating anything in the view layer; once you're doing it where it belongs, it's trivial. – Dave Newton Nov 27 '13 at 03:13
  • 1
    @DaveNewton I'm doing it for a good reason. That's all you need to know. – Chloe Nov 27 '13 at 03:15

2 Answers2

1

<s:push> must enclose the <s:property> tag. Also

<s:push value="#attr.scoredDocument">
    <display:column title="Study Code" sortable="true">
        <s:property value="id"/>
    </display:column>
</s:push>

#attr? WTF Struts? It's not even documented! https://struts.apache.org/release/2.0.x/docs/jsp.html

Chloe
  • 25,162
  • 40
  • 190
  • 357
  • 3
    Yes, it is, even in a version of S2 ~7 years old: https://struts.apache.org/release/2.0.x/docs/ognl.html. Oh look, `#attr`. And no, there's no good reason: scriptlets were considered a bad practice a decade ago. WTF indeed. – Dave Newton Nov 27 '13 at 03:19
1

The push tag requires the value attribute and it has to be initialized, otherwise it shows the JSP error. Unlike other tags where the value could be preinitialized with empty string, the JSP doesn't complain.

The OGNL expression is evaluated in this attribute, like in many other attributes of Struts tags, and if can't resolve the value it returns null. This value is unacceptable for the push tag and it throws exception. The error mislead to make a different tries to access the scriptlet variable value via the OGNL expression, unfortunately none of that methods worked.

Scriptlet expressions has a one pitfall when used with the Struts tags that you can use only in the body of the tag and it's converted to string. Like in this example

<s:set var="scoredDocument"><%=scoredDocument%></s:set>

you can't do the same with the push tag. After that you can use the stringified version of #scoredDocument in OGNL expressions.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Interesting, I didn't know it could do that, however scoredDocument is an object. I don't need the String version. It's already initialized, and I verified it is not null with the <%= tag %>. creates JSP variables for iterator values, and I need to be able to access those via Struts2. The table is already laid out with Struts2 and I don't want to convert it to JSP tags or . My solution is working now. – Chloe Nov 28 '13 at 02:50
  • Those variables created in the page scope where you can use `#attr` or JSP EL to access them. Scriptlet variable is local to servlet compiled from the JSP and it doesn't have any scope, thus not accessible using that methods. To access that variable you need to put it into any scope or `valueStack`. – Roman C Nov 28 '13 at 09:39
  • No, Struts2 won't accept JSP EL ${} syntax. I tried that and it gave an error. Of course I need to put it in valueStack. That is what my question is about! I used #attr which worked. – Chloe Nov 29 '13 at 18:51
  • It depends where you use them, if you use in attributes then yes, outside it works but not in your case, **no `#attr` or `${}` or %{} can access it**. – Roman C Nov 29 '13 at 18:55