aspose file tools*
The moose likes Struts and the fly likes How to define String array in the <s:param/> tag? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to define String array in the <s:param/> tag?" Watch "How to define String array in the <s:param/> tag?" New topic
Author

How to define String array in the <s:param/> tag?

Dmitriy Kuznetsov
Greenhorn

Joined: Feb 05, 2010
Posts: 5
Hi, guys!

The question seems very simple, but i've red manuals and googled for answer with no success. Looks like i'm stuck with it...
Well, there is a need to invoke some Struts 2 action from a JSP, so i use <s:action> tag. The action class has a String[] property, which acts as a parameter(let's call it "actionParameter"):

I need a very simple thing: to define new String[]{"String1", "String2"} inside the <s:param/> tag. What should i write in the tag(probably, on the place of questions marks)?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You'd use multiple parameter tags with the same name.
Dmitriy Kuznetsov
Greenhorn

Joined: Feb 05, 2010
Posts: 5
David Newton wrote:You'd use multiple parameter tags with the same name.

Good idea! Would be great, if it work... but i've debugged code and found, that only the last one value is available in the action context parameters map (= ActionContext.getContext().getParameters()). It seems, that the reason is inside the Struts2(i use v. 2.1.8.1) Component class(ActionComponent is it's subclass):it just puts the parameters into a LinkedHashMap.
It seems, the only way is to feed the <s:param> tag with proper String array declaration. Any ideas?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Oh. bummer. (I tried with <s:url>, too, and only the last param tag value shows up in the url--personally, I think that's broken.)

In any case--you can't just send an "array" via a request, just strings: the <s:action> tag (which I generally recommend not be used) basically simulates a complete request invocation, so you won't be able to do it by passing an object. (I think--I could actually be wrong, because I really don't like the <s:action> tag or believe it has a good reason to exist, so I don't use it.)

It looks as though you'll need to either change how you're doing what you're trying to do (recommended, at least by me ;) or pass in a string representation and parse it in the action.

(It also looks like you can't use a param tag inside an iterator, which... oh, wait, I understand why. Dang. I suppose you could build up a URL string, but that's yucky.)

What, specifically, are you trying to accomplish with the action tag?
Dmitriy Kuznetsov
Greenhorn

Joined: Feb 05, 2010
Posts: 5
David Newton wrote:Oh. bummer. (I tried with <s:url>, too, and only the last param tag value shows up in the url--personally, I think that's broken.)

In any case--you can't just send an "array" via a request, just strings: the <s:action> tag (which I generally recommend not be used) basically simulates a complete request invocation, so you won't be able to do it by passing an object. (I think--I could actually be wrong, because I really don't like the <s:action> tag or believe it has a good reason to exist, so I don't use it.)

It looks as though you'll need to either change how you're doing what you're trying to do (recommended, at least by me ;) or pass in a string representation and parse it in the action.

(It also looks like you can't use a param tag inside an iterator, which... oh, wait, I understand why. Dang. I suppose you could build up a URL string, but that's yucky.)

What, specifically, are you trying to accomplish with the action tag?

Well, the main thing here is that when Struts receives POST, created by e.g. <select multiple=true> element(there is "paramName=value1;paramName=value2;paramName=value3;..." in it), it just sets value of "paramName" property with String array, containing {"value1", "value2", "value3"}. Specially for these requests there is an action with String[] property in our code. And i want to reuse it as it is in <action> tag.

The background of all this stuff is that we have a web page, which is loaded once and being populated with data(grids, lists, etc.) by ajax requests when user asks for it. This concept fails when user hits "Refresh" or closes page and opes it again - page appears empty. To handle this, i use <action> tag on JSP render time(if there is something in user's session) to get result from populating action(it responds with JSON object), and include it into page javascript code. So, in the result of all this tambourine dances user after refresh gets page fully populated.

Some time ago our pages were issuing ajax requests immediately on load. But i don't like this logic, since server side has everything it needs to render JSP, populated with data. So i started to use <action> tag on JSP render time instead of ajax requests on page load time. In all cases, except this one(this one is the only requiring String array parameter), it works fine.

Thank you for your time and ideas.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I guess I'd either just use the Ajax requests and have the page's action just be a page loader, or create a rendering JSP.

The action tag has a few other potential "gotchas" as well--I'm really not a big fan of it at all; it smells of action chaining, too.

Sorry I can't be more helpful! (The other option, creating a delimited string and parsing it, should also work.)
Dmitriy Kuznetsov
Greenhorn

Joined: Feb 05, 2010
Posts: 5
David Newton wrote:The action tag has a few other potential "gotchas" as well--I'm really not a big fan of it at all; it smells of action chaining, too.
Sorry I can't be more helpful! (The other option, creating a delimited string and parsing it, should also work.)

Agree, action chaining(even by invoking actions from inside JSPs) is not good idea. It seems i should extract data-providing logic into e.g. struts bean and use it in action that renders JSP and in another action that serves ajax requests.

BTW, i found the solution to the initial question. Such stupid question has really simple answer:
(note: it requires full name 'java.lang.String[]', saying just 'String[]' is not enough)
it could be helpful for somebody trying to pass through the same wall...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to define String array in the <s:param/> tag?