You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Alexander Dimitrov <so...@gmail.com> on 2009/03/12 15:46:23 UTC

How can I process form with custom index-like properties

Hello,

I'm working on complicated data generated form

<code>

<s:form action="NewOrdItm" theme="simple">
    <table>
        <tr>
            <td>name</td>
            <td>params</td>
            <td>comb1</td>
            <td>comb2</td>
            <td>comb3</td>
        </tr>

        <s:iterator value="itemColl">
            <s:if test="%{!hidden}">
                <tr>
                    <td nowrap="true">
                        <s:if test="%{formRadio}">
                            <s:radio
name="radio[%{formRadioGroupName}]" list="{item.id}"/>
                            <s:property value="item.name"/>
                        </s:if>
                        <s:elseif test="%{formCheckBox}">
                            <s:checkbox name="checkBox[%{item.id}]"
label="item.name"/>
                            <s:property value="item.name"/>
                        </s:elseif>
                        <s:else>
                            <s:property value="item.name"/>
                        </s:else>
                    </td>
                    <td>
                        <s:if test="%{int}">
                            <s:textfield name="intParam[%{item.id}]"/>
                        </s:if>
                        <s:elseif test="%{float}">
                            <s:textfield name="floatParam[%{item.id}]"/>
                        </s:elseif>
                        <s:elseif test="%{area}">
                            X:<s:textfield name="areaXParam[%{item.id}]"/>
                            Y:<s:textfield name="areaYParam[%{item.id}]"/>
                        </s:elseif>
                        <s:elseif test="%{areaCm}">
                            Col:<s:textfield name="areaColParam[%{item.id}]"/>
                            h:<s:textfield name="areaMmParam[%{item.id}]"/>
                        </s:elseif>
                        <s:else>
                            &nbsp;
                        </s:else>
                    </td>
                    <td>
                        <s:if test="%{item.comb1 != null}">
                            <s:property value="item.comb1.name"/>:
                            <s:select name="comb1[%{item.id}]"
list="item.comb1.items" listKey="id" listValue="name" />
                        </s:if>
                        <s:else>
                            &nbsp;
                        </s:else>
                    </td>
                    <td>
                        <s:if test="%{item.comb2 != null}">
                            <s:property value="item.comb2.name"/>:
                            <s:select name="comb2[%{item.id}]"
list="item.comb2.items" listKey="id" listValue="name" />
                        </s:if>
                        <s:else>
                            &nbsp;
                        </s:else>
                    </td>
                    <td>
                        <s:if test="%{item.comb3 != null}">
                            <s:property value="item.comb3.name"/>:
                            <s:select name="comb3[%{item.id}]"
list="item.comb3.items" listKey="id" listValue="name" />
                        </s:if>
                        <s:else>
                            &nbsp;
                        </s:else>
                    </td>
                </tr>
            </s:if>
        </s:iterator>
    </table>
    <s:submit value="Create"/>
</s:form>

</code>

I have defined getters and setters with additional parameter on the
receiving action class:

<code>

public class OrdItm extends ActionSupport implements ParameterAware {
...
    public int getRadio(String radioGroupName) {
...
    }

    public void setRadio(String radioGroupName, int value) {
...
    }

    public boolean getCheckBox(int id) {
...
    }

    public void setCheckBox(int id, boolean value) {
...
    }
}

</code>

Too bad that none of the getters/setters are called when form is posted

I included parametersAware interceptor to see what I receive from post
Here is list the list of received values

<code>

key=checkBox[226] value=[true]
key=checkBox[223] value=[false]
key=itemDate value=[]
key=checkBox[221] value=[false]
key=checkBox[222] value=[true]
key=comb2[212] value=[100]
key=areaMmParam[212] value=[]
key=areaMmParam[211] value=[]
key=checkBox[225] value=[false]
key=checkBox[224] value=[true]
key=areaMmParam[210] value=[25]
key=areaColParam[211] value=[]
key=comb1[211] value=[400]
key=itemText value=[]
key=comb1[212] value=[400]
key=radio[RG201] value=[210]
key=comb1[210] value=[401]
key=areaColParam[212] value=[]
key=areaColParam[210] value=[1]

</code>


Thanks in advance for your help

Regards,
Alexander

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


Re: How can I process form with custom index-like properties

Posted by dusty <du...@yahoo.com>.
The [] syntax is used to define collections or maps of values.   I don't
think your setters are going to work that way.  OGNL does not know how to
handle arbitrary new setter parameters.  

I think what you are trying to do is create a form with an arbitrary number
of fields, probably driven from some database Form definition and you are
trying to figure out a model to "catch" the data that comes in.

We could go down the route of discussing the patterns associated with this
common problem.  One of the approaches involves a "long and skinny" database
schema.  This means that your values are all stored in a single table as
key/value pairs as Strings, potentially with some type information you can
use for casting later.   Some have created different skinny tables for
different types to avoid the casting issue but that causes more complex
query mapping to your models......but where was I....

I went there because you can use a Map in your action to "catch" all of the
key/value pairs you send through.  In the webwork days this was a popular
patterned called Map Backed Actions??I think or something like that.  You
could google it to see how it works.  You can use one map or several maps
and even store collections (checkbox select many) as map values.

But you will not be able to use the setters the way you are.   That syntax
is for collections and maps.



Alexander Dimitrov-2 wrote:
> 
> Hello,
> 
> I'm working on complicated data generated form
> 
> <code>
> 
> <s:form action="NewOrdItm" theme="simple">
>     <table>
>         <tr>
>             <td>name</td>
>             <td>params</td>
>             <td>comb1</td>
>             <td>comb2</td>
>             <td>comb3</td>
>         </tr>
> 
>         <s:iterator value="itemColl">
>             <s:if test="%{!hidden}">
>                 <tr>
>                     <td nowrap="true">
>                         <s:if test="%{formRadio}">
>                             <s:radio
> name="radio[%{formRadioGroupName}]" list="{item.id}"/>
>                             <s:property value="item.name"/>
>                         </s:if>
>                         <s:elseif test="%{formCheckBox}">
>                             <s:checkbox name="checkBox[%{item.id}]"
> label="item.name"/>
>                             <s:property value="item.name"/>
>                         </s:elseif>
>                         <s:else>
>                             <s:property value="item.name"/>
>                         </s:else>
>                     </td>
>                     <td>
>                         <s:if test="%{int}">
>                             <s:textfield name="intParam[%{item.id}]"/>
>                         </s:if>
>                         <s:elseif test="%{float}">
>                             <s:textfield name="floatParam[%{item.id}]"/>
>                         </s:elseif>
>                         <s:elseif test="%{area}">
>                             X:<s:textfield name="areaXParam[%{item.id}]"/>
>                             Y:<s:textfield name="areaYParam[%{item.id}]"/>
>                         </s:elseif>
>                         <s:elseif test="%{areaCm}">
>                             Col:<s:textfield
> name="areaColParam[%{item.id}]"/>
>                             h:<s:textfield
> name="areaMmParam[%{item.id}]"/>
>                         </s:elseif>
>                         <s:else>
>                             &nbsp;
>                         </s:else>
>                     </td>
>                     <td>
>                         <s:if test="%{item.comb1 != null}">
>                             <s:property value="item.comb1.name"/>:
>                             <s:select name="comb1[%{item.id}]"
> list="item.comb1.items" listKey="id" listValue="name" />
>                         </s:if>
>                         <s:else>
>                             &nbsp;
>                         </s:else>
>                     </td>
>                     <td>
>                         <s:if test="%{item.comb2 != null}">
>                             <s:property value="item.comb2.name"/>:
>                             <s:select name="comb2[%{item.id}]"
> list="item.comb2.items" listKey="id" listValue="name" />
>                         </s:if>
>                         <s:else>
>                             &nbsp;
>                         </s:else>
>                     </td>
>                     <td>
>                         <s:if test="%{item.comb3 != null}">
>                             <s:property value="item.comb3.name"/>:
>                             <s:select name="comb3[%{item.id}]"
> list="item.comb3.items" listKey="id" listValue="name" />
>                         </s:if>
>                         <s:else>
>                             &nbsp;
>                         </s:else>
>                     </td>
>                 </tr>
>             </s:if>
>         </s:iterator>
>     </table>
>     <s:submit value="Create"/>
> </s:form>
> 
> </code>
> 
> I have defined getters and setters with additional parameter on the
> receiving action class:
> 
> <code>
> 
> public class OrdItm extends ActionSupport implements ParameterAware {
> ...
>     public int getRadio(String radioGroupName) {
> ...
>     }
> 
>     public void setRadio(String radioGroupName, int value) {
> ...
>     }
> 
>     public boolean getCheckBox(int id) {
> ...
>     }
> 
>     public void setCheckBox(int id, boolean value) {
> ...
>     }
> }
> 
> </code>
> 
> Too bad that none of the getters/setters are called when form is posted
> 
> I included parametersAware interceptor to see what I receive from post
> Here is list the list of received values
> 
> <code>
> 
> key=checkBox[226] value=[true]
> key=checkBox[223] value=[false]
> key=itemDate value=[]
> key=checkBox[221] value=[false]
> key=checkBox[222] value=[true]
> key=comb2[212] value=[100]
> key=areaMmParam[212] value=[]
> key=areaMmParam[211] value=[]
> key=checkBox[225] value=[false]
> key=checkBox[224] value=[true]
> key=areaMmParam[210] value=[25]
> key=areaColParam[211] value=[]
> key=comb1[211] value=[400]
> key=itemText value=[]
> key=comb1[212] value=[400]
> key=radio[RG201] value=[210]
> key=comb1[210] value=[401]
> key=areaColParam[212] value=[]
> key=areaColParam[210] value=[1]
> 
> </code>
> 
> 
> Thanks in advance for your help
> 
> Regards,
> Alexander
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/How-can-I-process-form-with-custom-index-like-properties-tp22477937p22479073.html
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