Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Action class

public class ProductAction extends ActionSupport implements Preparable {
    private Document product;
}

Model

public class Document {
    private Map<String, Object> properties;
}

JSP

<s:textfield name="product.properties.PRODUCT_NAME" 
             value="%   {product.properties..PRODUCT_NAME}" 
             label="%{getText('label.PRODUCT_NAME')}" size="40" />

<s:textfield name="product.properties.SUPPLIER" 
             value="%{product.properties.SUPPLIER}" 
             label="%{getText('label.SUPPLIER')}" size="40" />

Product_name and Supplier are populated as Array in Map (Map<String, String[]>) properties.

PRODUCT_NAME : [Ljava.lang.String;@4e96ac47]
SUPPLIER     : [Ljava.lang.String;@1c90a278]

If I change the Document->properties to Map<String, String> it works fine.

But I want to retain Document->properties as Map<String, Object> because of other datatypes.

How to solve this issue, I want form data to be populated as String instead of String[].

I don't have multiple text fields with the same name.

share|improve this question
    
what do you want to display, the key or the value. –  Uchenna Nwanyanwu Jun 12 '13 at 9:01
    
Learn how to use indexed properties in struts2. –  Roman C Jun 12 '13 at 9:04
    
I want to pass private Map<String, Object> properties to dao layer for save, but Map<String, String[]> array is causing an issue where dao layer accepts only Map<String, String> –  Chethan C Jun 12 '13 at 10:24
    
Why are you holding different data types in the same map? –  Aleksandr M Jun 12 '13 at 10:41
    
Because it is the easiest way to process data, where I can have date, string, integer, etc –  Chethan C Jun 12 '13 at 10:42

3 Answers 3

Try this

<s:textfield name="product.properties.SUPPLIER" 
             value="%{product.properties.SUPPLIER[0]}" 
             label="%{getText('label.SUPPLIER')}" size="40" />

This will retrieve the first item in the Array

share|improve this answer
    
Thanks for the reply. I have no issues in jsp, but the actual problem is I don't want array in action Map<String, Object>. –  Chethan C Jun 12 '13 at 10:16

Modify the model and introduce methods to work with Map but save the data to the underlying Map like below

public class Document {
    private Map<String, Object> properties;


    public Map<String, String> getStringProperties() {
        //logic to populate Map<String, String>
        return new HashMap<String, String>();
    }

    public void setStringProperties(Map<String, String> stringProperties) {
        properties.putAll(stringProperties);
    }
}

In the jsp use stringProperties like below

<s:textfield name="product.stringProperties.SUPPLIER" 
             value="%{product.stringProperties.SUPPLIER}" 
             label="%{getText('label.SUPPLIER')}" size="40" />
share|improve this answer
    

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.