0

I am trying to load drop down list using ArrayList in Springboot as i am new to springboot.

I have tried but not working as expected.

Please find the code which i have tried as below:

Java Code:

@Controller
public class DemoController {

@GetMapping("/create")
     public String create(Model model) {

      model.addAttribute("create", new Demo());

      return "create";

     }

     public void countriesList(Model model) {

          List<String> countryList = new ArrayList<>();

          countryList.add("US");
          countryList.add("UK");

          model.addAttribute("countries", countryList);

       }
}

HTML:

<form action="#" th:action="@{/create}" th:object="${create}" method="post">

        <select th:field="*{country}">
             <option value=""> -- </option>
             <option th:each="country : ${countries}" th:value="${country}" th:text="${country}"></option>
        </select>
</form>

Finally no errors but only loading in dropdown list with -- and not loading the countries.

Please help me on this.

Krishna
  • 233
  • 2
  • 6
  • 20
  • please add the error you are getting – Syed Mehtab Hassan May 07 '19 at 06:07
  • *I have tried but not working as expected and getting errors.* And why do you think that not including those errors in your question is good idea? – Antoniossss May 07 '19 at 06:07
  • 2
    Besides, you are not adding anything to the model. – Antoniossss May 07 '19 at 06:08
  • How to add model..Please help me as i am new to Springboot.Thanks @Antoniossss – Krishna May 07 '19 at 06:12
  • the `countryList` have to be added to the model so it can be accessed by thymeleaf. `model.addAttribute("countries", countryList);` –  May 07 '19 at 06:12
  • I have added as suggested but still getting error. I have updated my post with new code.. please check and help me. thanks @Valentin Carnu – Krishna May 07 '19 at 06:23
  • @Chassu for `th:field` try replacing `${countries}` with `*{countries}` (details on [thymeleaf documentation](https://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#inputs)) –  May 07 '19 at 06:50
  • @Chassu maybe the accepted answer from [How do I populate a drop down with a list using thymeleaf and spring](https://stackoverflow.com/a/37723208/10386912) thread helps –  May 07 '19 at 06:54
  • I have already seen that but no full controller class code and as i trying with arraylist. now no errors but countries are not loading in drop down. Updated new code in post.. please check. Thanks @Valentin Carnu – Krishna May 07 '19 at 07:12
  • @Chassu you arent calling the `countriesList()` method in `create()` ? – Nodir Rashidov May 07 '19 at 07:25

2 Answers2

1

You never called your function for adding countries to the model...

@Controller
public class DemoController {

 @GetMapping("/create")
 public String create(Model model) {

  model.addAttribute("create", new Demo());
  countriesList(model); // this line is needed
  return "create";

 }

 private void countriesList(Model model) {

  List < String > countryList = new ArrayList < > ();

  countryList.add("US");
  countryList.add("UK");

  model.addAttribute("countries", countryList);

 }
}
Nodir Rashidov
  • 692
  • 9
  • 32
  • After adding this line - `countriesList(model);` working without any issues. thank you @Nodir Rashidov – Krishna May 07 '19 at 07:36
  • Please help me that how to invoke a method when selected value in dropdown list? thanks @Nodir Rashidov – Krishna May 07 '19 at 07:44
0

try with this code block

 <span th:each="country:${countries}" th:remove="tag">
          <option th:value="${country}" th:text="${country}"></option>
    </span>
Istiaque Hossain
  • 2,157
  • 1
  • 17
  • 28
  • No errors , but nothing on page as dropdown list is not showing on page. Please help me.Thanks @pantha istiaque – Krishna May 07 '19 at 06:51