1

I am calling an action via foo.action?error=bar

In struts.xml, I have configured the action chain as follows:

<action name="foo">
  <interceptor-ref name="defaultStack"/>
  <!-- custom interceptors -->
  <result name="success">/jsp/foo.jsp</result>
</action>

In the JSP, I'm running a test:

<s:if test="#parameters.error[0] == 'bar'">

It's legacy code; this works.
However, the following does not, and I don't understand why:

<s:if test="#parameters.error == 'bar'">

Why do I need to pretend the error parameter is a collection?

Judging from the docs:

I shouldn't have to - but then I haven't used JSPs much...

I have added the following to the JSP, in order to help me understand what's going on:

<s:property value="#parameters.error"/>                          // bar
<s:property value="parameters.error"/>                           // <nothing>
<s:property value="parameters.error[0]"/>                        // <nothing>
<%= pageContext.getRequest().getParameter("error") %>            // bar
<%= pageContext.getRequest().getParameter("error").getClass() %> // class java.lang.String
<s:property value="#parameters.error=='bar'"/>                   // false
<s:property value="'token'.getClass()"/>                         // <nothing>

That output confuses me even more. Can someone please explain what's going on?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Christian
  • 6,070
  • 11
  • 53
  • 103

1 Answers1

1

You don't need to pretend, it's a collection. The new implementation uses HttpParameters and OGNL expression doesn't know that, a tag is using toString() value on object that is not a string.

You can read more about parameters in this answer.

HttpParameters. The later class implements a Map<String,Parameter>, so you can use this to get/put parameters to the map.

Also see:

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • How would you then get the first map element? `` works, but `` doesn't, but both are tags...? – Christian Oct 11 '17 at 14:04
  • Just looked at ["What's the difference between # , % and $ signs in Struts tags?"](https://stackoverflow.com/a/11620838/2018047) and realised I might have to force evaluation as OGNL, and indeed, it appears to work: `` – Christian Oct 11 '17 at 14:05
  • This is an old post, it might not applicable to the new version. To have an explicit explanation of your problem I'd recommend to read https://stackoverflow.com/a/17001858/573032 or https://stackoverflow.com/a/44986848/573032 – Roman C Oct 11 '17 at 14:16
  • Your answer goes into detail about `HttpParameters` implementing `Map` - which doesn't answer why what looks like an array indexation was needed. Your post contains useful hints, but in 2017, when I asked my question, it wasn't the answer. (`Parameter` also doesn't seem to implement `Collection`.) What did help was the link I subsequently dug out myself and left as a comment. So, thanks for your reminder what an Accepted Answer is, but your replies, helpful as they were, weren't the answer I was looking for - which is why I didn't accept yours back then. :) – Christian Oct 25 '21 at 22:37