1

I was going through spring MVC tutorials and came across ModelAndView.

My JSP view looks like this,

<%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<html>
   <head>
      <title>Spring MVC </title>
   </head>
   <body>

      <h2>Submitted Student Information</h2>
      <table>
         <tr>
            <td>Name</td>
            <td>${student.getName()}</td>
         </tr>
         <tr>
            <td>Age</td>
            <td>${student.getAge()}</td>
         </tr>
         <tr>
            <td>ID</td>
            <td>${student.getBranch()}</td>
         </tr>
      </table>  
   </body>
</html>

It worked when I set both attribute name and attribute value in controller like the following,

ModelAndView mv = new ModelAndView();
        mv.setViewName("result");
        Student student = new Student("arun2", "CSE", 22);
        mv.addObject("student",student);
        return mv;

Then I came across other overloaded method ModelAndView.addObject(Object attributeValue) and I tried setting only attribute value mv.addObject(student); But this time it doesn't show student details in the browser.

My questions:

  1. Is it possible to access those values in JSP by just setting attribute value as i did second-time mv.addObject(student);.
  2. If yes, How? If not, Why do we have such overloaded method? Why do we need to set only value if we can't access it in the view (JSP)?

I went through the javadocs, But didn't find anything that could make me understand.

Arun Gowda
  • 2,721
  • 5
  • 29
  • 50

1 Answers1

2

Yes, you can do it like that and access the parameters,but need to pay some attention.

Check the API for addObject at ModelAndView,it will shows method as below: enter image description here

Then,let's look at the definition of ModelMap.addAttribute(Object) enter image description here

It shows using a generated name,the definition of generated name listed as below: enter image description here

So,if you just want to use the mv.addObject(student) method,you can access data in your jsp page as below:

<%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<html>
   <head>
      <title>Spring MVC </title>
   </head>
   <body>

      <h2>Submitted Student Information</h2>
      <table>
         <tr>
            <td>Name</td>
            <td>${student.name}</td>
         </tr>
         <tr>
            <td>Age</td>
            <td>${student.age}</td>
         </tr>
         <tr>
            <td>ID</td>
            <td>${student.branch}</td>
         </tr>
      </table>  
   </body>
</html>

If the object is a Group class, then you can get value via ${group.name}

And pay attention that the ModelAndView is from org.springframework.web.servlet.ModelAndView

Also,I suggest you use the EL Expressions to access data in your jsp page directly.

flyingfox
  • 13,414
  • 3
  • 24
  • 39
  • But what if I add multiple attributes of the same class? Is it allowed? say I added two attributes `student1` `student2` of class `Student`.What now? – Arun Gowda Mar 31 '18 at 07:47
  • I just tested add one object of same class yesterday and it worked, you can try it youself,maybe there would be some exception – flyingfox Mar 31 '18 at 08:22
  • I tried too. It only works the way you have explained it. If we set 2 objects `student1` and `student2`of same class `Student`, It only takes the first object we set in the controller. – Arun Gowda Mar 31 '18 at 14:47
  • That's weird,in most case the latter one will override the previous one – flyingfox Mar 31 '18 at 14:54
  • I thought the same! But that's how it worked when I tested. – Arun Gowda Apr 01 '18 at 14:29