File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Trying to access model attribute in view. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Trying to access model attribute in view." Watch "Trying to access model attribute in view." New topic
Author

Trying to access model attribute in view.

Antraa Sethi
Greenhorn

Joined: Apr 13, 2012
Posts: 19
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

Joined: Mar 07, 2010
Posts: 1680
    
    7

Where are you adding Students to the Student list in your controller? It looks like you model contains only an empty Student list.


[How To Ask Questions][Read before you PM me]
Antraa Sethi
Greenhorn

Joined: Apr 13, 2012
Posts: 19

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

Joined: Apr 13, 2012
Posts: 19
I tried this too.


Not working
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

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

Joined: Mar 07, 2010
Posts: 1680
    
    7

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

Joined: Apr 13, 2012
Posts: 19
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
 
subject: Trying to access model attribute in view.