• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trying to access model attribute in view.

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this too.


Not working
 
Bill Gorder
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic