You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Nuwan Chandrasoma <my...@gmail.com> on 2008/07/01 06:57:10 UTC

Re: [S2] Pre-populating Checkboxes

Hi,

This is how i do it, i have implemented the prepreable interface in my 
action and in prepare method, i populate the list i wanted to display in 
as check box values.

eg:-

    private List skills;  

    public List getSkills() {
        return skills;
    }

    public void prepare() throws Exception {
        skills = new ArrayList();
        skills.add("Java");
        skills.add("C#");
        skills.add("Spring");
        skills.add("Struts 2");
    }

in jsp i have this tag and which will render 4 check boxes.

<s:checkboxlist name="skill" list="skills"/>

Thanks,

Nuwan



David Ogasawara wrote:
> Hello,
>
> I'm new to Struts 2 and I was wondering how to pre-populate checkboxes
> with values from a database (or even hard-coded values).  I am iterating
> through an arraylist of hashmaps (description, type_cd) to create the
> checkboxes in the form.  I then create a string array with the values
> from a database to pre-populate, but it's not working.  
>
> * When the checkboxes are checked and the form is submitted, I am able
> to capture the values (String array) properly.
>
>
> JSP:
> <s:iterator value="all_type_codes">
> 	<s:checkbox name="type_cd" fieldValue="%{type_cd}"/>
> 	<s:property value="description"/>
>       <br>
> </s:iterator>
>
>
>
> Action Page:
> public String[] getType_cd() {
>     return type_cd;
> }
> public void setType_cd(String[] type_cd) {
>     this.type_cd = type_cd;
> }
>
> // this is a test
> type_cd = new String[3];
> type_cd[0] = "EQ";
> type_cd[1] = "SF";
> type_cd[2] = "SS";
>
>
>
> Any help would be greatly appreciated!
>
> -Dave
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>   


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


Re: [S2] Pre-populating Checkboxes

Posted by Nuwan Chandrasoma <my...@gmail.com>.
Hi,

I just add a <br> at line 51 of checkboxlist.ftl

<label for="${parameters.name?html}-${itemCount}" 
class="checkboxLabel">${itemValue?html}</label><br>

Thanks,

Nuwan


Jim Kiley wrote:
> It is remarkably easy to extend the 'simple' or 'xhtml' themes to make
> s:checkboxlist display vertically.  Here's how I did it:
>
> In the root of my web code I have a 'template' folder.  Within 'template' I
> have a folder named 'my'.  Inside 'my' I have checkboxlist.ftl.
>
> checkboxlist.ftl looks like this (minus the license boilerplate):
>
> <#assign itemCount = 0/>
> <#if parameters.list?exists>
>     <@s.iterator value="parameters.list">
>         <#assign itemCount = itemCount + 1/>
>         <#if parameters.listKey?exists>
>             <#assign itemKey = stack.findValue(parameters.listKey)/>
>         <#else>
>             <#assign itemKey = stack.findValue('top')/>
>         </#if>
>         <#if parameters.listValue?exists>
>             <#assign itemValue = stack.findString(parameters.listValue)/>
>         <#else>
>             <#assign itemValue = stack.findString('top')/>
>         </#if>
> <#assign itemKeyStr=itemKey.toString() />
> <tr><td><input type="checkbox" name="${parameters.name?html}"
> value="${itemKeyStr?html}" id="${parameters.name?html}-${itemCount}"<#rt/>
>         <#if tag.contains(parameters.nameValue, itemKey)>
>  checked="checked"<#rt/>
>         </#if>
>         <#if parameters.disabled?default(false)>
>  disabled="disabled"<#rt/>
>         </#if>
>         <#if parameters.title?exists>
>  title="${parameters.title?html}"<#rt/>
>         </#if>
>         <#include "/${parameters.templateDir}/simple/scripting-events.ftl"
> />
>         <#include "/${parameters.templateDir}/simple/common-attributes.ftl"
> />
> />
> </td><td><label for="${parameters.name?html}-${itemCount}"
> class="checkboxLabel">${itemValue?html}</label></td></tr>
>     </...@s.iterator>
> <#else>
>   &nbsp;
> </#if>
>
> This is probably not the ideal way to do this, but it works great for me.
>
> jk
>
>
> On Tue, Jul 1, 2008 at 12:33 PM, David Ogasawara <DO...@rgs.uci.edu>
> wrote:
>
>   
>> This is exactly what I needed.  I was avoiding checkboxlist because I
>> wanted the options to be displayed vertically, but it looks to be the
>> only option unless I add more code to determine if a checkbox was
>> selected.
>>
>> Thank you, everyone, for your help!!!
>>
>>
>>
>>
>> -----Original Message-----
>> From: Nuwan Chandrasoma [mailto:mymailnot@gmail.com]
>> Sent: Tuesday, July 01, 2008 9:16 AM
>> To: Struts Users Mailing List
>> Subject: Re: [S2] Pre-populating Checkboxes
>>
>> Hi,
>>
>> I have done it like this.
>>
>> // code in my action
>>
>>    private String[] skill;
>>
>>    public String[] getSkill() {
>>        return skill;
>>    }
>>
>>    public void setSkill(String[] skill) {
>>        this.skill = skill;
>>    }
>>
>>    public void prepare() throws Exception {
>>        skills = new ArrayList();
>>        skills.add("Java");
>>        skills.add("C#");
>>        skills.add("Spring");
>>        skills.add("Struts 2");
>>
>>        skill = new String [2];
>>
>>        skill[0] = "Java";
>>        skill[1] = "Spring";
>>    }
>> //jsp
>>
>> <s:checkboxlist name="skill" list="skills"/>
>>
>> Java and Spring check boxes will be selected.
>>
>> Thanks,
>>
>> Nuwan
>>
>>
>>
>>
>> David Ogasawara wrote:
>>     
>>> Hi,
>>>
>>> Thank you very much for your assistance, but I am already able to
>>> display the checkboxes.  I guess my question wasn't very clear.  I
>>>       
>> need
>>     
>>> to know how to redisplay the checked boxes a user selected in a
>>>       
>> previous
>>     
>>> session.  So if there are five check boxes and the user selects three
>>>       
>> of
>>     
>>> them and submits the form, how do I automatically check the three
>>>       
>> boxes
>>     
>>> when the user returns the next day?  It appears that returning a
>>>       
>> string
>>     
>>> array with the users selections is not working.
>>>
>>> Thanks,
>>> Dave
>>>
>>>
>>>
>>> -----Original Message-----
>>> From: Nuwan Chandrasoma [mailto:mymailnot@gmail.com]
>>> Sent: Monday, June 30, 2008 9:57 PM
>>> To: Struts Users Mailing List
>>> Subject: Re: [S2] Pre-populating Checkboxes
>>>
>>> Hi,
>>>
>>> This is how i do it, i have implemented the prepreable interface in my
>>>       
>>> action and in prepare method, i populate the list i wanted to display
>>>       
>> in
>>     
>>> as check box values.
>>>
>>> eg:-
>>>
>>>     private List skills;
>>>
>>>     public List getSkills() {
>>>         return skills;
>>>     }
>>>
>>>     public void prepare() throws Exception {
>>>         skills = new ArrayList();
>>>         skills.add("Java");
>>>         skills.add("C#");
>>>         skills.add("Spring");
>>>         skills.add("Struts 2");
>>>     }
>>>
>>> in jsp i have this tag and which will render 4 check boxes.
>>>
>>> <s:checkboxlist name="skill" list="skills"/>
>>>
>>> Thanks,
>>>
>>> Nuwan
>>>
>>>
>>>
>>> David Ogasawara wrote:
>>>
>>>       
>>>> Hello,
>>>>
>>>> I'm new to Struts 2 and I was wondering how to pre-populate
>>>>         
>> checkboxes
>>     
>>>> with values from a database (or even hard-coded values).  I am
>>>>
>>>>         
>>> iterating
>>>
>>>       
>>>> through an arraylist of hashmaps (description, type_cd) to create the
>>>> checkboxes in the form.  I then create a string array with the values
>>>> from a database to pre-populate, but it's not working.
>>>>
>>>> * When the checkboxes are checked and the form is submitted, I am
>>>>         
>> able
>>     
>>>> to capture the values (String array) properly.
>>>>
>>>>
>>>> JSP:
>>>> <s:iterator value="all_type_codes">
>>>>      <s:checkbox name="type_cd" fieldValue="%{type_cd}"/>
>>>>      <s:property value="description"/>
>>>>     <br>
>>>> </s:iterator>
>>>>
>>>>
>>>>
>>>> Action Page:
>>>> public String[] getType_cd() {
>>>>     return type_cd;
>>>> }
>>>> public void setType_cd(String[] type_cd) {
>>>>     this.type_cd = type_cd;
>>>> }
>>>>
>>>> // this is a test
>>>> // these are the options the user selected in a previous session
>>>> type_cd = new String[3];
>>>> type_cd[0] = "EQ";
>>>> type_cd[1] = "SF";
>>>> type_cd[2] = "SS";
>>>>
>>>>
>>>>
>>>> Any help would be greatly appreciated!
>>>>
>>>> -Dave
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>>> For additional commands, e-mail: user-help@struts.apache.org
>>>>
>>>>
>>>>
>>>>
>>>>         
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: user-help@struts.apache.org
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: user-help@struts.apache.org
>>>
>>>
>>>
>>>       
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>>     
>
>
>   


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


Re: [S2] Pre-populating Checkboxes

Posted by Jim Kiley <jh...@summa-tech.com>.
It is remarkably easy to extend the 'simple' or 'xhtml' themes to make
s:checkboxlist display vertically.  Here's how I did it:

In the root of my web code I have a 'template' folder.  Within 'template' I
have a folder named 'my'.  Inside 'my' I have checkboxlist.ftl.

checkboxlist.ftl looks like this (minus the license boilerplate):

<#assign itemCount = 0/>
<#if parameters.list?exists>
    <@s.iterator value="parameters.list">
        <#assign itemCount = itemCount + 1/>
        <#if parameters.listKey?exists>
            <#assign itemKey = stack.findValue(parameters.listKey)/>
        <#else>
            <#assign itemKey = stack.findValue('top')/>
        </#if>
        <#if parameters.listValue?exists>
            <#assign itemValue = stack.findString(parameters.listValue)/>
        <#else>
            <#assign itemValue = stack.findString('top')/>
        </#if>
<#assign itemKeyStr=itemKey.toString() />
<tr><td><input type="checkbox" name="${parameters.name?html}"
value="${itemKeyStr?html}" id="${parameters.name?html}-${itemCount}"<#rt/>
        <#if tag.contains(parameters.nameValue, itemKey)>
 checked="checked"<#rt/>
        </#if>
        <#if parameters.disabled?default(false)>
 disabled="disabled"<#rt/>
        </#if>
        <#if parameters.title?exists>
 title="${parameters.title?html}"<#rt/>
        </#if>
        <#include "/${parameters.templateDir}/simple/scripting-events.ftl"
/>
        <#include "/${parameters.templateDir}/simple/common-attributes.ftl"
/>
/>
</td><td><label for="${parameters.name?html}-${itemCount}"
class="checkboxLabel">${itemValue?html}</label></td></tr>
    </...@s.iterator>
<#else>
  &nbsp;
</#if>

This is probably not the ideal way to do this, but it works great for me.

jk


On Tue, Jul 1, 2008 at 12:33 PM, David Ogasawara <DO...@rgs.uci.edu>
wrote:

> This is exactly what I needed.  I was avoiding checkboxlist because I
> wanted the options to be displayed vertically, but it looks to be the
> only option unless I add more code to determine if a checkbox was
> selected.
>
> Thank you, everyone, for your help!!!
>
>
>
>
> -----Original Message-----
> From: Nuwan Chandrasoma [mailto:mymailnot@gmail.com]
> Sent: Tuesday, July 01, 2008 9:16 AM
> To: Struts Users Mailing List
> Subject: Re: [S2] Pre-populating Checkboxes
>
> Hi,
>
> I have done it like this.
>
> // code in my action
>
>    private String[] skill;
>
>    public String[] getSkill() {
>        return skill;
>    }
>
>    public void setSkill(String[] skill) {
>        this.skill = skill;
>    }
>
>    public void prepare() throws Exception {
>        skills = new ArrayList();
>        skills.add("Java");
>        skills.add("C#");
>        skills.add("Spring");
>        skills.add("Struts 2");
>
>        skill = new String [2];
>
>        skill[0] = "Java";
>        skill[1] = "Spring";
>    }
> //jsp
>
> <s:checkboxlist name="skill" list="skills"/>
>
> Java and Spring check boxes will be selected.
>
> Thanks,
>
> Nuwan
>
>
>
>
> David Ogasawara wrote:
> > Hi,
> >
> > Thank you very much for your assistance, but I am already able to
> > display the checkboxes.  I guess my question wasn't very clear.  I
> need
> > to know how to redisplay the checked boxes a user selected in a
> previous
> > session.  So if there are five check boxes and the user selects three
> of
> > them and submits the form, how do I automatically check the three
> boxes
> > when the user returns the next day?  It appears that returning a
> string
> > array with the users selections is not working.
> >
> > Thanks,
> > Dave
> >
> >
> >
> > -----Original Message-----
> > From: Nuwan Chandrasoma [mailto:mymailnot@gmail.com]
> > Sent: Monday, June 30, 2008 9:57 PM
> > To: Struts Users Mailing List
> > Subject: Re: [S2] Pre-populating Checkboxes
> >
> > Hi,
> >
> > This is how i do it, i have implemented the prepreable interface in my
>
> > action and in prepare method, i populate the list i wanted to display
> in
> >
> > as check box values.
> >
> > eg:-
> >
> >     private List skills;
> >
> >     public List getSkills() {
> >         return skills;
> >     }
> >
> >     public void prepare() throws Exception {
> >         skills = new ArrayList();
> >         skills.add("Java");
> >         skills.add("C#");
> >         skills.add("Spring");
> >         skills.add("Struts 2");
> >     }
> >
> > in jsp i have this tag and which will render 4 check boxes.
> >
> > <s:checkboxlist name="skill" list="skills"/>
> >
> > Thanks,
> >
> > Nuwan
> >
> >
> >
> > David Ogasawara wrote:
> >
> >> Hello,
> >>
> >> I'm new to Struts 2 and I was wondering how to pre-populate
> checkboxes
> >> with values from a database (or even hard-coded values).  I am
> >>
> > iterating
> >
> >> through an arraylist of hashmaps (description, type_cd) to create the
> >> checkboxes in the form.  I then create a string array with the values
> >> from a database to pre-populate, but it's not working.
> >>
> >> * When the checkboxes are checked and the form is submitted, I am
> able
> >> to capture the values (String array) properly.
> >>
> >>
> >> JSP:
> >> <s:iterator value="all_type_codes">
> >>      <s:checkbox name="type_cd" fieldValue="%{type_cd}"/>
> >>      <s:property value="description"/>
> >>     <br>
> >> </s:iterator>
> >>
> >>
> >>
> >> Action Page:
> >> public String[] getType_cd() {
> >>     return type_cd;
> >> }
> >> public void setType_cd(String[] type_cd) {
> >>     this.type_cd = type_cd;
> >> }
> >>
> >> // this is a test
> >> // these are the options the user selected in a previous session
> >> type_cd = new String[3];
> >> type_cd[0] = "EQ";
> >> type_cd[1] = "SF";
> >> type_cd[2] = "SS";
> >>
> >>
> >>
> >> Any help would be greatly appreciated!
> >>
> >> -Dave
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> For additional commands, e-mail: user-help@struts.apache.org
> >>
> >>
> >>
> >>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


-- 
Jim Kiley
Technical Consultant | Summa
[p] 412.258.3346 [m] 412.445.1729
http://www.summa-tech.com

RE: [S2] Pre-populating Checkboxes

Posted by Dave Newton <ne...@yahoo.com>.
--- On Tue, 7/1/08, David Ogasawara <DO...@rgs.uci.edu> wrote:
> I was avoiding checkboxlist because I wanted the options to be 
> displayed vertically, but it looks to be the only option unless 
> I add more code to determine if a checkbox was selected.

Or use the "css_xhtml" theme, which IIRC just spits out checkboxes and labels with no other markup.

Or modify <s:checkboxlist...>'s template, which might be more appropriate if you need the functionality across your application.

http://struts.apache.org/2.x/docs/themes-and-templates.html

Dave


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


RE: [S2] Pre-populating Checkboxes

Posted by David Ogasawara <DO...@rgs.uci.edu>.
This is exactly what I needed.  I was avoiding checkboxlist because I
wanted the options to be displayed vertically, but it looks to be the
only option unless I add more code to determine if a checkbox was
selected.

Thank you, everyone, for your help!!!




-----Original Message-----
From: Nuwan Chandrasoma [mailto:mymailnot@gmail.com] 
Sent: Tuesday, July 01, 2008 9:16 AM
To: Struts Users Mailing List
Subject: Re: [S2] Pre-populating Checkboxes

Hi,

I have done it like this.

// code in my action

    private String[] skill;

    public String[] getSkill() {
        return skill;
    }

    public void setSkill(String[] skill) {
        this.skill = skill;
    }

    public void prepare() throws Exception {
        skills = new ArrayList();
        skills.add("Java");
        skills.add("C#");
        skills.add("Spring");
        skills.add("Struts 2");

        skill = new String [2];

        skill[0] = "Java";
        skill[1] = "Spring";
    }
//jsp

<s:checkboxlist name="skill" list="skills"/>

Java and Spring check boxes will be selected.

Thanks,

Nuwan




David Ogasawara wrote:
> Hi,
>
> Thank you very much for your assistance, but I am already able to
> display the checkboxes.  I guess my question wasn't very clear.  I
need
> to know how to redisplay the checked boxes a user selected in a
previous
> session.  So if there are five check boxes and the user selects three
of
> them and submits the form, how do I automatically check the three
boxes
> when the user returns the next day?  It appears that returning a
string
> array with the users selections is not working.
>
> Thanks,
> Dave
>
>
>
> -----Original Message-----
> From: Nuwan Chandrasoma [mailto:mymailnot@gmail.com] 
> Sent: Monday, June 30, 2008 9:57 PM
> To: Struts Users Mailing List
> Subject: Re: [S2] Pre-populating Checkboxes
>
> Hi,
>
> This is how i do it, i have implemented the prepreable interface in my

> action and in prepare method, i populate the list i wanted to display
in
>
> as check box values.
>
> eg:-
>
>     private List skills;  
>
>     public List getSkills() {
>         return skills;
>     }
>
>     public void prepare() throws Exception {
>         skills = new ArrayList();
>         skills.add("Java");
>         skills.add("C#");
>         skills.add("Spring");
>         skills.add("Struts 2");
>     }
>
> in jsp i have this tag and which will render 4 check boxes.
>
> <s:checkboxlist name="skill" list="skills"/>
>
> Thanks,
>
> Nuwan
>
>
>
> David Ogasawara wrote:
>   
>> Hello,
>>
>> I'm new to Struts 2 and I was wondering how to pre-populate
checkboxes
>> with values from a database (or even hard-coded values).  I am
>>     
> iterating
>   
>> through an arraylist of hashmaps (description, type_cd) to create the
>> checkboxes in the form.  I then create a string array with the values
>> from a database to pre-populate, but it's not working.  
>>
>> * When the checkboxes are checked and the form is submitted, I am
able
>> to capture the values (String array) properly.
>>
>>
>> JSP:
>> <s:iterator value="all_type_codes">
>> 	<s:checkbox name="type_cd" fieldValue="%{type_cd}"/>
>> 	<s:property value="description"/>
>>     <br>
>> </s:iterator>
>>
>>
>>
>> Action Page:
>> public String[] getType_cd() {
>>     return type_cd;
>> }
>> public void setType_cd(String[] type_cd) {
>>     this.type_cd = type_cd;
>> }
>>
>> // this is a test
>> // these are the options the user selected in a previous session
>> type_cd = new String[3];
>> type_cd[0] = "EQ";
>> type_cd[1] = "SF";
>> type_cd[2] = "SS";
>>
>>
>>
>> Any help would be greatly appreciated!
>>
>> -Dave
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>>   
>>     
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>   


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


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


Re: [S2] Pre-populating Checkboxes

Posted by Nuwan Chandrasoma <my...@gmail.com>.
Hi,

I have done it like this.

// code in my action

    private String[] skill;

    public String[] getSkill() {
        return skill;
    }

    public void setSkill(String[] skill) {
        this.skill = skill;
    }

    public void prepare() throws Exception {
        skills = new ArrayList();
        skills.add("Java");
        skills.add("C#");
        skills.add("Spring");
        skills.add("Struts 2");

        skill = new String [2];

        skill[0] = "Java";
        skill[1] = "Spring";
    }
//jsp

<s:checkboxlist name="skill" list="skills"/>

Java and Spring check boxes will be selected.

Thanks,

Nuwan




David Ogasawara wrote:
> Hi,
>
> Thank you very much for your assistance, but I am already able to
> display the checkboxes.  I guess my question wasn't very clear.  I need
> to know how to redisplay the checked boxes a user selected in a previous
> session.  So if there are five check boxes and the user selects three of
> them and submits the form, how do I automatically check the three boxes
> when the user returns the next day?  It appears that returning a string
> array with the users selections is not working.
>
> Thanks,
> Dave
>
>
>
> -----Original Message-----
> From: Nuwan Chandrasoma [mailto:mymailnot@gmail.com] 
> Sent: Monday, June 30, 2008 9:57 PM
> To: Struts Users Mailing List
> Subject: Re: [S2] Pre-populating Checkboxes
>
> Hi,
>
> This is how i do it, i have implemented the prepreable interface in my 
> action and in prepare method, i populate the list i wanted to display in
>
> as check box values.
>
> eg:-
>
>     private List skills;  
>
>     public List getSkills() {
>         return skills;
>     }
>
>     public void prepare() throws Exception {
>         skills = new ArrayList();
>         skills.add("Java");
>         skills.add("C#");
>         skills.add("Spring");
>         skills.add("Struts 2");
>     }
>
> in jsp i have this tag and which will render 4 check boxes.
>
> <s:checkboxlist name="skill" list="skills"/>
>
> Thanks,
>
> Nuwan
>
>
>
> David Ogasawara wrote:
>   
>> Hello,
>>
>> I'm new to Struts 2 and I was wondering how to pre-populate checkboxes
>> with values from a database (or even hard-coded values).  I am
>>     
> iterating
>   
>> through an arraylist of hashmaps (description, type_cd) to create the
>> checkboxes in the form.  I then create a string array with the values
>> from a database to pre-populate, but it's not working.  
>>
>> * When the checkboxes are checked and the form is submitted, I am able
>> to capture the values (String array) properly.
>>
>>
>> JSP:
>> <s:iterator value="all_type_codes">
>> 	<s:checkbox name="type_cd" fieldValue="%{type_cd}"/>
>> 	<s:property value="description"/>
>>     <br>
>> </s:iterator>
>>
>>
>>
>> Action Page:
>> public String[] getType_cd() {
>>     return type_cd;
>> }
>> public void setType_cd(String[] type_cd) {
>>     this.type_cd = type_cd;
>> }
>>
>> // this is a test
>> // these are the options the user selected in a previous session
>> type_cd = new String[3];
>> type_cd[0] = "EQ";
>> type_cd[1] = "SF";
>> type_cd[2] = "SS";
>>
>>
>>
>> Any help would be greatly appreciated!
>>
>> -Dave
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>>   
>>     
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>   


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


RE: [S2] Pre-populating Checkboxes

Posted by Dave Newton <ne...@yahoo.com>.
In your original code you don't do anything to specify whether or not the current value matches on of the values in the user's current selections--not sure what magic you were expecting.

The <s:checkboxlist...> tag [1] accepts both value and selection choices; it may be sufficient for your needs. If you're going to do the value/checkbox iteration by hand then you also need to implement the logic for determining if it should be checked initially.

Dave

[1] http://struts.apache.org/2.x/docs/checkboxlist.html

--- On Tue, 7/1/08, David Ogasawara <DO...@rgs.uci.edu> wrote:
> Thank you very much for your assistance, but I am already
> able to
> display the checkboxes.  I guess my question wasn't
> very clear.  I need
> to know how to redisplay the checked boxes a user selected
> in a previous
> session.  So if there are five check boxes and the user
> selects three of
> them and submits the form, how do I automatically check the
> three boxes
> when the user returns the next day?  It appears that
> returning a string
> array with the users selections is not working.
> 
> Thanks,
> Dave
> 
> 
> 
> -----Original Message-----
> From: Nuwan Chandrasoma [mailto:mymailnot@gmail.com] 
> Sent: Monday, June 30, 2008 9:57 PM
> To: Struts Users Mailing List
> Subject: Re: [S2] Pre-populating Checkboxes
> 
> Hi,
> 
> This is how i do it, i have implemented the prepreable
> interface in my 
> action and in prepare method, i populate the list i wanted
> to display in
> 
> as check box values.
> 
> eg:-
> 
>     private List skills;  
> 
>     public List getSkills() {
>         return skills;
>     }
> 
>     public void prepare() throws Exception {
>         skills = new ArrayList();
>         skills.add("Java");
>         skills.add("C#");
>         skills.add("Spring");
>         skills.add("Struts 2");
>     }
> 
> in jsp i have this tag and which will render 4 check boxes.
> 
> <s:checkboxlist name="skill"
> list="skills"/>
> 
> Thanks,
> 
> Nuwan
> 
> 
> 
> David Ogasawara wrote:
> > Hello,
> >
> > I'm new to Struts 2 and I was wondering how to
> pre-populate checkboxes
> > with values from a database (or even hard-coded
> values).  I am
> iterating
> > through an arraylist of hashmaps (description,
> type_cd) to create the
> > checkboxes in the form.  I then create a string array
> with the values
> > from a database to pre-populate, but it's not
> working.  
> >
> > * When the checkboxes are checked and the form is
> submitted, I am able
> > to capture the values (String array) properly.
> >
> >
> > JSP:
> > <s:iterator value="all_type_codes">
> > 	<s:checkbox name="type_cd"
> fieldValue="%{type_cd}"/>
> > 	<s:property value="description"/>
> >     <br>
> > </s:iterator>
> >
> >
> >
> > Action Page:
> > public String[] getType_cd() {
> >     return type_cd;
> > }
> > public void setType_cd(String[] type_cd) {
> >     this.type_cd = type_cd;
> > }
> >
> > // this is a test
> > // these are the options the user selected in a
> previous session
> > type_cd = new String[3];
> > type_cd[0] = "EQ";
> > type_cd[1] = "SF";
> > type_cd[2] = "SS";
> >
> >
> >
> > Any help would be greatly appreciated!


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


RE: [S2] Pre-populating Checkboxes

Posted by David Ogasawara <DO...@rgs.uci.edu>.
Hi,

Thank you very much for your assistance, but I am already able to
display the checkboxes.  I guess my question wasn't very clear.  I need
to know how to redisplay the checked boxes a user selected in a previous
session.  So if there are five check boxes and the user selects three of
them and submits the form, how do I automatically check the three boxes
when the user returns the next day?  It appears that returning a string
array with the users selections is not working.

Thanks,
Dave



-----Original Message-----
From: Nuwan Chandrasoma [mailto:mymailnot@gmail.com] 
Sent: Monday, June 30, 2008 9:57 PM
To: Struts Users Mailing List
Subject: Re: [S2] Pre-populating Checkboxes

Hi,

This is how i do it, i have implemented the prepreable interface in my 
action and in prepare method, i populate the list i wanted to display in

as check box values.

eg:-

    private List skills;  

    public List getSkills() {
        return skills;
    }

    public void prepare() throws Exception {
        skills = new ArrayList();
        skills.add("Java");
        skills.add("C#");
        skills.add("Spring");
        skills.add("Struts 2");
    }

in jsp i have this tag and which will render 4 check boxes.

<s:checkboxlist name="skill" list="skills"/>

Thanks,

Nuwan



David Ogasawara wrote:
> Hello,
>
> I'm new to Struts 2 and I was wondering how to pre-populate checkboxes
> with values from a database (or even hard-coded values).  I am
iterating
> through an arraylist of hashmaps (description, type_cd) to create the
> checkboxes in the form.  I then create a string array with the values
> from a database to pre-populate, but it's not working.  
>
> * When the checkboxes are checked and the form is submitted, I am able
> to capture the values (String array) properly.
>
>
> JSP:
> <s:iterator value="all_type_codes">
> 	<s:checkbox name="type_cd" fieldValue="%{type_cd}"/>
> 	<s:property value="description"/>
>     <br>
> </s:iterator>
>
>
>
> Action Page:
> public String[] getType_cd() {
>     return type_cd;
> }
> public void setType_cd(String[] type_cd) {
>     this.type_cd = type_cd;
> }
>
> // this is a test
> // these are the options the user selected in a previous session
> type_cd = new String[3];
> type_cd[0] = "EQ";
> type_cd[1] = "SF";
> type_cd[2] = "SS";
>
>
>
> Any help would be greatly appreciated!
>
> -Dave
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>   


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


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