Pre-populate Spring MVC form

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

For example, let's say I added a queryInfo object to uiModel .

How to display instance variable name from queryInfo object?

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

Is it possible? If so, how?

+4
source share
3 answers

In your query mapping, add a bean to the model:

 model.addAttribute("queryInfo", queryInfo); 

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

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

Now, when you do this, the name will be displayed (if there is a tap in the corresponding name):

 <form:input path="name" /> 

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

+8
source

@ skel625 solution for the form: the input is fine, but in the case of the form: select, with another option, how do I set the attributes? because this way it only works for the form: input

My form: select:

 <form:select path="dolorefastidio"> <option value="1"><spring:message code="questionnaire.compile.label.paindiscomfort.one"/></option> <option value="2"><spring:message code="questionnaire.compile.label.paindiscomfort.two"/></option> <option value="3"><spring:message code="questionnaire.compile.label.paindiscomfort.three"/></option> <option value="4"><spring:message code="questionnaire.compile.label.paindiscomfort.four"/></option> <option value="5"><spring:message code="questionnaire.compile.label.paindiscomfort.five"/></option> </form:select> 
0
source

the solution is placed in the value attribute a bean attribute similar to this

Exam Registration Form

  <div class="form-group has-success"> <label for="code" class="col-lg-3 control-label">Exam Code</label> <div class="col-lg-9"> <form:input type="text" class="form-control" path="cod" placeholder="code" value="${editExam.cod}"/> <form:errors path="cod" cssClass="error" /> </div> </div> <div class="form-group has-success"> <label for="name" class="col-lg-3 control-label">Exam Name</label> <div class="col-lg-9"> <form:input type="text" class="form-control" path="name" placeholder="name" value="${editExam.name}"/> <form:errors path="name" cssClass="error" /> </div> </div> <div class="form-group has-success"> <label for="teachNme" class="col-lg-3 control-label">Teacher Name</label> <div class="col-lg-9"> <form:input type="text" class="form-control" path="teachName" placeholder="Teacher Name" value="${exams.teachName}" /> <form:errors path="teachName" cssClass="error" /> </div> </div> <div class="form-group has-success"> <label for="vote" class="col-lg-3 control-label">Final Grade</label> <div class="col-lg-9"> <form:input type="text" class="form-control" path="vote" placeholder="Vote" value="${exams.vote}" /> <form:errors path="vote" cssClass="error" /> </div> </div> <div class="form-group has-success"> <label for="cfu" class="col-lg-3 control-label">Credits (CFU)</label> <div class="col-lg-9"> <form:input type="text" class="form-control" path="cfu" placeholder="Credits" value="${editExam.cfu}" /> <form:errors path="cfu" cssClass="error" /> </div> </div> <input type="submit" value="Save" class="btn btn-primary pull-right"> </fieldset> </form:form> 
0
source

All Articles