我不确定如何在Spring MVC表单标记中定义value属性。我正在查询数据库,我想将数据返回给jsp。我以列表的形式将对象返回到视图。我想知道如何为选项列表和输入框编写属性值。以下是我的代码:
JSP
<form:form id="citizenRegistration" name ="citizenRegistration" method="POST" commandName="citizens" action="citizen_registration.htm">
<li>
<label>Select Gender</label><form:select path="genderId" id="genderId" title="Select Your Gender"><form:options items = "${gender.genderList}" selected=???? itemValue="genderId" itemLabel="genderDesc" />
</form:select><form:errors path="genderId" class="errors"/>
</li>
<li><form:label for="weight" path="weight">Enter Weight <i>(lbs)</i></form:label>
<form:input path="weight" id="weight" title="Enter Weight" value= ???/><form:errors path="weight" class="errors"/>
</li>
JavaDao
该函数返回: ........................
List<Citizens> listOfCitizens = getJdbcTemplate().query(sql, new CitizensMapper());
return listOfCitizens;
控制器
if (user_request.equals("Query")){
logger.debug("about to preform query");
citizenManager.getListOfCitizens(citizen);
if(citizenManager.getListOfCitizens(citizen).isEmpty()){
model.addAttribute("icon","ui-icon ui-icon-circle-close");
model.addAttribute("results","Notice: Query Caused No Records To Be Retrived!");
}
//how do i return the List<Citizens> listOfCitizens
//or what should be done to send the user the data from the database
return new ModelAndView("citizen_registration");
}
答案 0 :(得分:1)
该值来自您的表单citizens
属性定义的模型对象(在您的案例中为commandName
)。 Spring使用它和path
属性来查找表单对象的值。
因此,例如,没有必要专门为value
属性提供值。
修改强>
这是一个简化的例子:
@RequestMapping(value = "/editCitizen", method = RequestMethod.GET)
public String editCitizen(@ModelAttribute("citizen") Citizen citizen, Model model) {
// set attributes of citizen
citizen.genderId = "M";
citizen.weight = 180;
// etc.
// set other model attributes like lists for <form:select>s
model.addAttribute("genderList", <list of genders>);
return "path.to.my.jsp";
}
<form:form id="citizenRegistration" name ="citizenRegistration" method="POST" commandName="citizen" action="citizen_registration.htm">
<form:select path="genderId" items="${genderList}" itemLabel="genderDesc" itemValue="genderId"></form:select>
<form:input path="weight" id="weight" title="Enter Weight"/>
</form:form>