pavel_kazlou
pavel_kazlou

Reputation: 2046

struts2: dynamic block page structure

I want to render a list of complex objects on one page: tables, images, plots, etc. Each element will reside in its own block (for example <div>). There are several types of such complex objects so I don't want to implement rendering logic in one place, but rather modularize this somehow (separate JSP for each type of object as well as separate code, preparing data for JSP). What is the best way I can do this?

My own intend was to use <s:action> tag. So I could iterate over my list and invoke actions based on some condition:

<s:iterator value="objectList" var="element">
        <s:if test="#element.type == 'TABLE'">
            <s:action name="renderTable"><s:param name="elementValue" value="#element.value"/></s:action>
        </s:if>
        ...
</s:iterator>

Obviously in order to render table or plot or any other element I need to pass data to rendering action - you can see I'm trying to pass entries to my renderTable action. And here comes the problem - Struts casts this parameter to array of Strings and tries to find setElementValue(String[]) in my action. So I get exception:

java.lang.NoSuchMethodException: edu.stackoverflow.RenderTable.setElementValue([Ljava.lang.String;)

I have no idea why the implementation of org.apache.struts2.components.ActionComponent handles parameters in such a way, here is the code from the class:

    for (Iterator i = parameters.entrySet().iterator(); i.hasNext(); ) {
        Map.Entry entry = (Map.Entry) i.next();
        String key = (String) entry.getKey();
        Object val = entry.getValue();
        if (val.getClass().isArray() && String.class == val.getClass().getComponentType()) {
            params.put(key, (String[])val);
        } else {
            params.put(key, new String[]{val.toString()});
        }
    }

So the approach becomes impossible.

Any alternatives?

Upvotes: 0

Views: 420

Answers (2)

Umesh Awasthi
Umesh Awasthi

Reputation: 23587

I believe that in place of template based component,creating custom tags in jsp is best way to go that will give you more control about handling the overall structure.

Alternatively you can create custom UI Component.

creating_a_ui_component

Upvotes: 1

Dave Newton
Dave Newton

Reputation: 160271

Why not just use a normal JSP-based custom tag?

Alternatively, you could create a template-based component.

Upvotes: 2

Related Questions