Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Scriptlet iterating over a bean

Posted on 2005-04-25
5
Medium Priority
?
1,320 Views
Last Modified: 2008-02-01
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?

0
Comment
Question by:kennethkufluk
  • 2
  • 2
5 Comments
 
LVL 29

Accepted Solution

by:
bloodredsun earned 1500 total points
ID: 13859785
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
 

Author Comment

by:kennethkufluk
ID: 13859900
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
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13860082
>>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
 
LVL 11

Expert Comment

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

Author Comment

by:kennethkufluk
ID: 13865018
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article explains the process to deploy a Self-Service password reset portal I developed a few years ago. Hopefully, it will prove useful to someone.  Any comments, bug reports etc. are welcome...
A quick solution showing how to control and open a POS Cash Register Drawer using VBA with MS Access.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month11 days, 4 hours left to enroll

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question