0

我不确定如何在 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> 

爪哇道

函数返回:..........................

   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");                    
}   
4

1 回答 1

1

该值来自表单属性citizens定义的模型对象(在您的情况下) 。commandNameSpring 使用该属性和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>
于 2013-03-04T20:00:47.737 回答