javajspspring-mvcmodelandview

How to access a ModelAndView object added without attribute name within the jsp page


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.


Solution

  • 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.