Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre-populating a Spring MVC form

I am looking for a way to pre-populate a Spring MVC form from values stored in a session-scoped bean. (using this namespace: http://www.springframework.org/tags/form).

For example, say I have added a queryInfo object to the uiModel.

How do I display the name instance variable from the queryInfo object?

<form:input path="queryInfo.name" />

Is this possible? If so how?

like image 959
balteo Avatar asked Feb 20 '23 19:02

balteo


1 Answers

In your request mapping, add the bean to the model:

model.addAttribute("queryInfo", queryInfo);

Then use modelAttribute in the form tag to bind it to the form:

<form:form id="some-form" modelAttribute="queryInfo">

... 

Now name will display (provided there is a getter in your object appropriately named) when you do this:

<form:input path="name" />

Keep in mind form:input is a child tag of form:form. It is not meant to be used on its own.

like image 106
skel625 Avatar answered Feb 26 '23 18:02

skel625