Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...