I am trying my hands on spring MVC by developing a simple StudentController to add delete view student details.
I have maintained a list of students defined in my controller where I am storing the student objects.
I am able to add the student.
I want to view the llist of students for which my code looks as below:
My understanding is: I am setting the modelattribute to the student arraylist I am maintianing. And passing that to my view. And accessing the model in view to display students.
My printList.jsp looks like
It is not printing anything.
What am I doing wrong?
I want a clear understanding...please help.
1) do not put stateful objects as instance variables in a spring bean(i.e @Controller). There is only a single instance of this class in the bean factory and doing that is not thread safe. Things are going to get really ugly if multiple people start requesting this page.
2)When you call /printList it is a GET request. There is no data being posted in. Therefore spring is creating a new empty arraylist of Students for you. Go ahead and print it out you will see it is empty. Typically you would populate this list there possibly through repository call before returning the view.