user1573503
user1573503

Reputation:

how to display model attribute in jsp using Spring MVC?

Actually my application have Spring MVC...

I have User.jsp, In this i'm creating some empty form (text boxes, textarea..) I'm Display the form using below method In my Controller class. Below code for add empty form on Front end jsp.

@RequestMapping(value = "user", method = RequestMethod.GET)
public String user(Model model) throws Exception {
    model.addAttribute("userForm", new UserForm());

    return "profile/user";
}

Now i'm getting UserForm in Database(3 rows).

So .. How to add Model attribute,if we add this one is their any override of model attribute?

How to display this model attribute into Jsp using JSTL?

Please suggest me i'm stuck this point..

Upvotes: 15

Views: 113914

Answers (2)

raj
raj

Reputation: 385

Sample Code

class UserForm {
    private String name;
    private String address;

    //setter and getter

}

In Your Controller

 @RequestMapper(value="/user")
    public ModelAndView user(){
        ModelAndView mav = new ModelAndView("userForm") ;
        List<UserForm> userForms = yourDatabaseCall();
        mav.addObject("userForms", userForms);  
        return mav;``
    }

in jsp page:

<c:forEach items="${userForms}" var="userForm">     
   <c:out value="${userForm.name}"/>
   <c:out value="${userForm.address}"/>
</c:forEach>

Upvotes: 15

Ajinkya
Ajinkya

Reputation: 22710

You can add list of userFomrs as a model attribute

List<UserForm> userForms = yourDatabaseCall();
model.addAttribute("userForms", userForms);     

In JSTL you can iterate over it

<c:forEach items="${userForms}" var="userForm">     
   // Do something
</c:forEach>

Upvotes: 14

Related Questions