Scriptlet iterating over a bean

I would like to access a bean:message through a scriptlet.

<%
String beanname = "";
for (int i=0;i<10;i++) {
      beanname = "beanmessage" + i;
      %><bean:message key="<%=beanname%>"/><%
}
%>

This works perfectly over 1-10.  However, the number of messages is held in the bean too.
I would like to do:

<%
String beanname = "";
int beanNumber = <bean:message key="numberofbeanmessages"/>;
for (int i=0;i<beanNumber;i++) {
      beanname = "beanmessage" + i;
      %><bean:message key="<%=beanname%>"/><%
}
%>

But of course this does not work.

How would I do this in a scriptlet?

kennethkuflukAsked:
Who is Participating?
 
bloodredsunCommented:
Assuming the bean is in the request object and is called myBean, you could do this. It's a bit of a hack and I suspect you'd be better of with the <nested> tag, or better yet, JSTL.

<%
String beanname = "";
MyBean myBean = (MyBean) request.getAttribute("myBean");//get bean from correct scope
int beanNumber = myBean.getNumberMessages() ;//if there was a method that returned the number of messages
for (int i=0;i<beanNumber;i++) {
     beanname = "beanmessage" + i;
     %><bean:message key="<%=beanname%>"/><%
}
%>
0
 
kennethkuflukAuthor Commented:
There's no method returning the number of messages, it's simply recorded as another value in the properties file, which the bean reads:

numberofbeanmessages=3
beanmessage0=hello
beanmessage1=hello
beanmessage2=hello

Are you suggesting:
MyBean myBean = (MyBean) request.getAttribute("numberofbeanmessages");//get bean from correct scope

???

0
 
bloodredsunCommented:
>>There's no method returning the number of messages, it's simply recorded as another value in the properties file, which the bean reads:

Then you can easily put one in the bean. If the messages are stored in the bean in a String array called "messageArray", you could just write somehting like...

public int getNumberOfBeanMessages(){
    return messageArray.length ;
}

>>Are you suggesting:
>>MyBean myBean = (MyBean) request.getAttribute("numberofbeanmessages");//get bean from correct scope

No. You are getting the bean from the request, not the property "numberofbeanmessages". If the bean has been saved in the request by the name "aBean" like this,

request.setAttribute("aBean" , myBean) ;//bean created earlier

You would get it back like this:

MyBean myBean = (MyBean) request.getAttribute("aBean");//get bean by corrcet name.

If you are using struts, the name of the bean will be the name that you specify in the struts-config.xml in the actionForm section.
0
 
ManishLeadCommented:
<%
String beanname = "";
int beanNumber = %><bean:message key="numberofbeanmessages"/>;
<% for (int i=0;i<beanNumber;i++) {
     beanname = "beanmessage" + i;
     %><bean:message key="<%=beanname%>"/>
<%
}
%>
0
 
kennethkuflukAuthor Commented:
karanw:  no, don't think so.

Does anyone here know about messageresources in struts?
That's where the bean:message tag comes from.

I know I could get the MessageResources bundle in the servlet, and use that, but can I access the same object in the JSP page?


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.