Two dimensional arrays in actions

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Two dimensional arrays in actions

Nathan Robertson
Hi,

I'm working on a problem at the moment where a form has an arbitrary
number of questions, each of which has an arbitrary number of answers.
In some cases, it's ok for the customer to select zero or more of the
options, in other cases they must select exactly one.

I've got a form that looks like this (simplified with values hard
coded and loops removed for clarity):

[...]
<s:form beanclass="${actionBean.class.name}" name="frmExam">
 <p>(Q1). In which country were you born?</p>
 <p><s:radio name="answer[1]" value="1"/>Australia</p>
 <p><s:radio name="answer[1]" value="2"/>Other</p>
 <p>&nbsp;</p>

 <p>(Q2). Do you speak any of these languages?</p>
 <p><s:checkbox name="answer[2]" value="1"/>English</p>
 <p><s:checkbox name="answer[2]" value="2"/>French</p>
 <p>&nbsp;</p>
[...]


And an action that looks like this:

public class SomeAction {
 public String[][] answer = null;

 @DefaultHandler
 public Resolution submit() {
  if (answer == null)
   logger.debug("No answers provided by customer");
  else
   logger.debug(String.format("answer[][] first dimension is %d items
long", answer.length));
 }
}


When I answer the questions and submit, my debugging output says
"answer[][] first dimension is 0 items long". So answer gets set to a
non-null, but of zero length. My reading of the section of the
(excellent) stripes book on indexed properties suggests that I might
be able to do this, but doesn't give an example that maps to exactly
what I'm doing.

My experience when dealing with one dimensional arrays (changing
answer[][] to answer1[], answer2[]) is that you get an array of
answers propagated for you. eg. If you speak both English and French,
you get answer2[] = {"1", "2"}; if you live in Australia you get
answer1[] = {"1"}

So, I would have thought that with the example I list the code for, if
you live in Australia and speak English and French you should get
answer[][] = {{}, {"1"}, {"1", "2"}} (my arrays are one indexed, so
the zero index should be null or an empty array). Is this not the
case?

Thanks for any help you can give.

Nathan.

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure
contains a definitive record of customers, application performance,
security threats, fraudulent activity, and more. Splunk takes this
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
Stripes-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/stripes-users
Reply | Threaded
Open this post in threaded view
|

Re: Two dimensional arrays in actions

Freddy D.
Hi Nathan,

A few comments below. Hope that helps.

> [...]
> <s:form beanclass="${actionBean.class.name}" name="frmExam">
>  <p>(Q1). In which country were you born?</p>
>  <p><s:radio name="answer[1]" value="1"/>Australia</p>
>  <p><s:radio name="answer[1]" value="2"/>Other</p>
>  <p>&nbsp;</p>
>
>  <p>(Q2). Do you speak any of these languages?</p>
>  <p><s:checkbox name="answer[2]" value="1"/>English</p>
>  <p><s:checkbox name="answer[2]" value="2"/>French</p>
>  <p>&nbsp;</p>
> [...]

Not a big deal, but note that you can use just
beanclass="${actionBean.class}" in <s:form>.

> And an action that looks like this:
>
> public class SomeAction {
>  public String[][] answer = null;
>
>  @DefaultHandler
>  public Resolution submit() {
>   if (answer == null)
>    logger.debug("No answers provided by customer");
>   else
>    logger.debug(String.format("answer[][] first dimension is %d items
> long", answer.length));
>  }
> }

I don't think this affects your code, but I would really
advise against using an event handler method named "submit".
I can't remember why exactly at the moment, but I remember that
it's a Really Bad Idea to have an HTML submit button named
submit.

> When I answer the questions and submit, my debugging output says
> "answer[][] first dimension is 0 items long". So answer gets set to a
> non-null, but of zero length. My reading of the section of the
> (excellent) stripes book on indexed properties suggests that I might
> be able to do this, but doesn't give an example that maps to exactly
> what I'm doing.

Thank you for your kind comment about the book--I'm glad you are
enjoying it. Sorry that I didn't have an example that matches your
use case..

> My experience when dealing with one dimensional arrays (changing
> answer[][] to answer1[], answer2[]) is that you get an array of
> answers propagated for you. eg. If you speak both English and French,
> you get answer2[] = {"1", "2"}; if you live in Australia you get
> answer1[] = {"1"}
>
> So, I would have thought that with the example I list the code for, if
> you live in Australia and speak English and French you should get
> answer[][] = {{}, {"1"}, {"1", "2"}} (my arrays are one indexed, so
> the zero index should be null or an empty array). Is this not the
> case?

I would have thought so too. I'll admit I am surprised. I'm not sure
why it doesn't work, but I can tell you that if you change your
property to

public List<List<String>> answer;

you will get what you expect, i.e.

answer = [null, [1], [1, 2]]

I'm hoping you don't mind working with List<List<String>> instead of
String[][]..

If it's an issue, I'll look further into why it doesn't work with
the two-dimensional array.

Cheers,
Freddy

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure
contains a definitive record of customers, application performance,
security threats, fraudulent activity, and more. Splunk takes this
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
Stripes-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/stripes-users
Reply | Threaded
Open this post in threaded view
|

Re: Two dimensional arrays in actions

Nathan Robertson
Hi Freddy,

Thanks for the help. Further comments inline.

On 30 November 2011 15:12, Freddy Daoud <[hidden email]> wrote:
[...]
> Not a big deal, but note that you can use just
> beanclass="${actionBean.class}" in <s:form>.

Noted. Thank you.

[...]
> I don't think this affects your code, but I would really
> advise against using an event handler method named "submit".
> I can't remember why exactly at the moment, but I remember that
> it's a Really Bad Idea to have an HTML submit button named
> submit.

Noted. Thank you. I've got a fair bit of code that uses "submit()", so
I'll be doing a fair bit of refactoring. :-)


>> My experience when dealing with one dimensional arrays (changing
>> answer[][] to answer1[], answer2[]) is that you get an array of
>> answers propagated for you. eg. If you speak both English and French,
>> you get answer2[] = {"1", "2"}; if you live in Australia you get
>> answer1[] = {"1"}
>>
>> So, I would have thought that with the example I list the code for, if
>> you live in Australia and speak English and French you should get
>> answer[][] = {{}, {"1"}, {"1", "2"}} (my arrays are one indexed, so
>> the zero index should be null or an empty array). Is this not the
>> case?
>
> I would have thought so too. I'll admit I am surprised. I'm not sure
> why it doesn't work, but I can tell you that if you change your
> property to
>
> public List<List<String>> answer;
>
> you will get what you expect, i.e.
>
> answer = [null, [1], [1, 2]]
>
> I'm hoping you don't mind working with List<List<String>> instead of
> String[][]..

Making that one change solved the problem. List<List<String>> works
perfectly, String[][] doesn't. So I'm guessing that's a bug? At least
there's a workaround, and it's not a bad one.

Thanks again for your help. Should I be opening a bug report for this one?

Regards,
Nathan.

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure
contains a definitive record of customers, application performance,
security threats, fraudulent activity, and more. Splunk takes this
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
Stripes-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/stripes-users
Reply | Threaded
Open this post in threaded view
|

Re: Two dimensional arrays in actions

Ben Gunter
My guess as to why it might not be good to name a form button "submit" is that form elements can be referenced with formNode.formElementName, but each form node already has a property named "submit" that references the submit function. I guess the function would win, but I'm not sure.

-Ben

On Tue, Nov 29, 2011 at 11:29 PM, Nathan Robertson <[hidden email]> wrote:
Hi Freddy,

Thanks for the help. Further comments inline.

On 30 November 2011 15:12, Freddy Daoud <[hidden email]> wrote:
[...]
> Not a big deal, but note that you can use just
> beanclass="${actionBean.class}" in <s:form>.

Noted. Thank you.

[...]
> I don't think this affects your code, but I would really
> advise against using an event handler method named "submit".
> I can't remember why exactly at the moment, but I remember that
> it's a Really Bad Idea to have an HTML submit button named
> submit.

Noted. Thank you. I've got a fair bit of code that uses "submit()", so
I'll be doing a fair bit of refactoring. :-)


>> My experience when dealing with one dimensional arrays (changing
>> answer[][] to answer1[], answer2[]) is that you get an array of
>> answers propagated for you. eg. If you speak both English and French,
>> you get answer2[] = {"1", "2"}; if you live in Australia you get
>> answer1[] = {"1"}
>>
>> So, I would have thought that with the example I list the code for, if
>> you live in Australia and speak English and French you should get
>> answer[][] = {{}, {"1"}, {"1", "2"}} (my arrays are one indexed, so
>> the zero index should be null or an empty array). Is this not the
>> case?
>
> I would have thought so too. I'll admit I am surprised. I'm not sure
> why it doesn't work, but I can tell you that if you change your
> property to
>
> public List<List<String>> answer;
>
> you will get what you expect, i.e.
>
> answer = [null, [1], [1, 2]]
>
> I'm hoping you don't mind working with List<List<String>> instead of
> String[][]..

Making that one change solved the problem. List<List<String>> works
perfectly, String[][] doesn't. So I'm guessing that's a bug? At least
there's a workaround, and it's not a bad one.

Thanks again for your help. Should I be opening a bug report for this one?

Regards,
Nathan.

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure
contains a definitive record of customers, application performance,
security threats, fraudulent activity, and more. Splunk takes this
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
Stripes-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/stripes-users


------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure
contains a definitive record of customers, application performance,
security threats, fraudulent activity, and more. Splunk takes this
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
Stripes-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/stripes-users