Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 678255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:07:46+00:00 2026-05-14T01:07:46+00:00

I have something like this in my Spring Application: public class Book{ public Book(){

  • 0

I have something like this in my Spring Application:

public class Book{ 
    public Book(){
       sheets = new LinkedList<Sheet>();
    }
    protected List<Sheet> sheets;
    //getter and setter 
}

I add several Sheets to the sheet list and I print a form in a JSP like this:

<form:form modelAttribute="book"  action="${dest_url}" method="POST">   
    <c:forEach items="${mybook.sheets}" var="sheet" varStatus="status">
        <form:hidden path="sheet[${status.count -1}].header"/>
        <form:hidden path="sheet[${status.count -1}].footer"/>
        <form:hidden path="sheet[${status.count -1}].operador"/>
        <form:hidden path="sheet[${status.count -1}].number"/>
        <form:hidden path="sheet[${status.count -1}].lines"/>
    </c:forEach>
    ...
</form:form>

I need to get back this list in the controller when the form is submitted. So in my controller I have a method with a parameter like this:

public String myMethod (@ModelAttribute("book") Book book, Model model){
    ...
}

The problem is that it doesn’t fill the sheets list unless in the constructor of Book I add as much Sheet’s as I want to get. The problem is that I don’t know in advance the number of Sheets the book is going to have.

I think the problem is that in my method it instantiates Book which has a list of sheets with 0 elements. When it tries to access to sheets[0] the list is empty and it doen’t add a Sheet. I’ve tried to create a getter method for the list with an index parameter (so it can create the element if it doesn’t exists in the list like in Struts framework) like this one:

public Sheet getSheets(int index){
    if(sheets.size() <= index){
        Sheet sheet = new Sheet();
        sheets.add(index, sheet);
    }
    Sheet sheetToReturn = sheets.get(index);
    if(sheetToReturn == null){
        sheetToReturn = new Sheet();
        sheets.add(index, sheetToReturn);
    }
    return sheetToReturn;
}

but with this method the JSP doesn’t work because sheets has an invalid getter.

What’s the proper way of filling a list when you don’t know the number of items in advanced?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:07:46+00:00Added an answer on May 14, 2026 at 1:07 am

    If you make your list a lazy list (Apache Commons Collections LazyList or Spring’s AutoPopulatingList) it will instantiate a new Sheet as a given list element at the moment you bind to it. Then you don’t have to worry about how many items you’ve prepopulated the list with — you can call list[17] on an empty list and it will be created on the fly and bound.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have something like this: barProgress.BeginAnimation(RangeBase.ValueProperty, new DoubleAnimation( barProgress.Value, dNextProgressValue, new Duration(TimeSpan.FromSeconds(dDuration))); Now, how
I have something like this: <div class=content> <a href=#>A</a> </div> <div class=content> <a href=#>B</a>
In a bunch o' places in my code, I have something like this: public
So I have something like this: var xmlStatement:String = xmlObject.node[3].@thisValue; What mystery function do
I am writing an XML document in C#. I have something like this... string
I have something like this: <node TEXT= txt A /> <node TEXT= txt X
It is typical to have something like this in your cshrc file for setting
Supposing to have something like this: #include <map> int main(){ std::map<int,int> m; m[1] =
In our code we used to have something like this: *(controller->bigstruct) = ( struct
Sounds like a weird question, but say I have something like this: $.post( /myajax.php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.