File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to access model attribute in view.

 
Antraa Sethi
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

Thanks
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you adding Students to the Student list in your controller? It looks like you model contains only an empty Student list.
 
Antraa Sethi
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks so much for your prompt response.
Here is the complete code in controller .


is model.addAttribute("students", students) doesnt do that?
 
Antraa Sethi
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this too.


Not working
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it doesn't do that.

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.
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your second post this.students is most likely empty as well. Do not do this anyway because of my first point in my last post.

 
Antraa Sethi
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it.
Thank you so much.

Stateful object as instance variable I took only to concentrate on SpringMVC flow only and not to get innto DB calls.

I will not do that again.

Thanks alot once again.

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic