struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From victor sosa <victorns...@gmail.com>
Subject Problem developing a tag using Struts 2 Arch for Tags
Date Mon, 03 Dec 2007 20:58:06 GMT

I am developing a tag, see tag example below. This tag should produce a json
object using the elements into the value stack.

I need to know how to repeat the execution of the content in this case
<jsongl:jsonproperty/> , also every execution need to push a new item into
the valuestack.

So I develop the tag using Component#start() and Component#end() methods,
the tag work but it isn't iterate the list of elements, and only produce
this output with the last element:

"array3": ["m2"]

correct output:

"array3": ["m1","m2"]

tag example:
		<jsongl:jsonarray name="machines" items="%{machine}">
			<jsongl:jsonproperty value="%{code}"/>
		</jsongl:jsonarray>

source code:
==================
//start
public boolean start(Writer writer) {
        LOG.debug("starting");
        boolean result = super.start(writer);

        try{         
            if (mItemsPropertySet) {
                coerceItemsToIterator();
                
                // An items property has been set - if there's anything in
it, then iterate over the items
                if (iterator != null && iterator.hasNext()){
                    iterateOverItems();
                }      
            } else {
                // No collection, so just process the body which should add
items to the array
                // Writer passed here is used to prevent any body content
being written to output stream
                
            }
        } catch (JspException e){
            // Just rethrow if we got a JspException - it's probably from a
nested tag - no new
            // info for us to add here.
            LOG.error("Just rethrow if we got a JspException - it's probably
from a nested tag");
        } catch (Exception e){
            LOG.error("Unable to create JSON array");
        }

        LOG.debug("finish");
        return result;
    }

// end method
public boolean end(Writer writer, String body) {
        LOG.debug("starting");
        
        boolean result = false; 
        ValueStack stack = getStack();
        
        if (iterator != null) {
            stack.pop();
        }

        if (iterator != null && iterator.hasNext()) {
            Object currentItem = iterator.next();
            stack.push(currentItem);
            
            LOG.debug("repeat with next element on array body");
            return true;            
        } //else {
            try {
                LOG.debug("Adding a new Json array object to parent
JsonObject");
                
                // Pop the array jsonEntity off of the stack
                this.getEntityStack().pop();
                
                // add the child array object to the parent jsonObject
                this.getCurrentEntity().add(jsonEntity.getWrappedObject(),
this.name);
            } catch (JSONException e) {
                LOG.error("Unable to create JSON array object", e);
                // TODO: handle exception
            }
//        }

// helper method
    private void iterateOverItems() throws JspException, IOException {
        ValueStack valueStack = getStack();
        
        // Iterator over 'items' collection
        while (iterator.hasNext()){
            Object currentItem = iterator.next();
            
            valueStack.push(currentItem);
            
            LOG.debug("item: " + currentItem);
        }
    }

-- 
View this message in context: http://www.nabble.com/Problem-developing-a-tag-using-Struts-2-Arch-for-Tags-tf4939292.html#a14138771
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message