2013-03-04 73 views
0

我不確定如何在Spring MVC表單標籤中定義值屬性。我正在查詢數據庫,我想將數據返回給jsp。我以列表的形式將對象返回給視圖。我想知道如何爲選項列表和輸入框編寫屬性值。在是我的代碼:Spring MVC 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");      
} 

回答

1

的價值來自於你的窗體的commandName屬性定義的(在你的情況citizens)的模型對象。 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> 
+0

爲什麼我的回報listOfCitizens不是值返回到屬性。該函數的定義如下public List getListOfCitizens(Citizens citizen){......從數據庫獲取數據..... return listOfCitizens;} – devdar 2013-03-04 20:07:38

+0

我做了一個從數據庫中檢查並且列表不是NULL的數據,一條記錄 – devdar 2013-03-04 20:08:22

+1

由您的控制器方法爲JSP提供模型。你的是否這樣做? – GriffeyDog 2013-03-04 20:13:49