java - Populate an html select with spring, thymeleaf, and mongo -


i'm trying populate html select data mongo collection using spring , thymeleaf. i've tried following advice these questions: "how populate drop down list using thymeleaf , spring", "how bind object list thymeleaf", , "spring app not finding property on object", haven't had luck.

when use syntax in html,

<div class="form-group">         <label for="commoditiesselect"> commodities: </label>         <select multiple="commodities" id="commoditiesselect" class="form-control" th:field="*{commodities}">            <option th:each="commodity : ${possiblecommoditieslist}"                    th:text="${possiblecommodities}"                    th:value="${possiblecommodities}"            />         </select>     </div> 

i blank list select on webpage. however, know list send model has multiple objects information in (via debugging). i've messed around syntax, closest i've gotten pointer object displaying on webpage. know i've gone wrong? included below controller, service implementation, possiblecommodities class, , possiblecommoditiesrepository class.

controller:

@getmapping("/welcomematform") public string welcomematform(model model) {     list<possiblecommodities> possiblecommoditieslist = welcomematservice.getpossiblecommodities();     model.addattribute("possiblecommoditieslist", possiblecommoditieslist);     model.addattribute("welcomematform", new welcomematform());     return "welcomematform"; } 

service implementation class:

    public list<possiblecommodities> getpossiblecommodities(){     list<possiblecommodities> listofcommodities = possiblecommoditiesrepository.findall();     return listofcommodities; } 

possiblecommodities class:

package com.sensus.analytics.welcomemat.core.model;  import org.springframework.data.annotation.typealias; import org.springframework.data.mongodb.core.mapping.document;  @document(collection = "possible_commodities") @typealias("possiblecommodities") public class possiblecommodities extends basicmongodocument{  public string commodityname; private int commodityid;  public string getcommodityname(int commodityid) {     return commodityname; }  public void setcommodityname(string commodityname) {     this.commodityname = commodityname; }  public int getcommodityid(string commodityname){     return commodityid; }  public void setcommodityid(int commodityid) {     this.commodityid = commodityid; } } 

possiblecommoditiesrepository class:

package com.sensus.analytics.welcomemat.core.repository;  import com.sensus.analytics.welcomemat.core.model.possiblecommodities; import org.springframework.data.mongodb.repository.mongorepository; import java.util.list;  public interface possiblecommoditiesrepository extends mongorepository<possiblecommodities, string>  { public list<possiblecommodities> findall(); } 

edit: i've tried common syntax html well:

<div class="form-group">         <label for="commoditiesselect"> commodities: </label>         <select multiple="commodities" id="commoditiesselect" class="form-control" th:field="*{commodities}">            <option th:each="commodity : ${possiblecommoditieslist}"                    th:text="${possiblecommodities.commodityname}"                    th:value="${possiblecommodities.commodityname}"            />          </select>     </div> 

but gives me error: el1007e:(pos 0): property or field 'commodityname' cannot found on null

in line this, have know variables being defined:

<option th:each="commodity : ${possiblecommoditieslist}" th:text="${possiblecommodities}" th:value="${possiblecommodities}" /> 

you have 3 variables here

  • commodity -- defined th:each expression
  • possiblecommoditieslist - defined adding model (in controller)
  • possiblecommodities - not defined anywhere. resolves null, , list of blank options. in case of second example, error caused thymleaf attempting call getcommodityname() on null object.

since in th:each, designated variable commodity, need this:

<option th:each="commodity : ${possiblecommoditieslist}" th:text="${commodity.commodityname}" th:value="${commodity.commodityname}" /> 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -