Spring Thymeleaf th:selected on EnumList with multiple selection












0














I have this enum



enum Types{
A, B
}


I have a form class



public class MyForm {
private Types types;
//getter setters
}


here is my form with select



<form th:action="${#httpServletRequest.requestURI}" th:object="${myForm}" method="POST" id="form">

<select name="types" multiple="" id="testSelect"
th:each="type : ${T(com.test.Types).values()}"
th:value="${type}"
th:text="${type}"
th:selected="*{types != null AND #arrays.contains(types, type)}"
>
</select>

</form>


here is the error i am getting.



 Property or field 'type' cannot be found on object of type 'com.test.MyForm' - maybe not public or not valid?









share|improve this question





























    0














    I have this enum



    enum Types{
    A, B
    }


    I have a form class



    public class MyForm {
    private Types types;
    //getter setters
    }


    here is my form with select



    <form th:action="${#httpServletRequest.requestURI}" th:object="${myForm}" method="POST" id="form">

    <select name="types" multiple="" id="testSelect"
    th:each="type : ${T(com.test.Types).values()}"
    th:value="${type}"
    th:text="${type}"
    th:selected="*{types != null AND #arrays.contains(types, type)}"
    >
    </select>

    </form>


    here is the error i am getting.



     Property or field 'type' cannot be found on object of type 'com.test.MyForm' - maybe not public or not valid?









    share|improve this question



























      0












      0








      0







      I have this enum



      enum Types{
      A, B
      }


      I have a form class



      public class MyForm {
      private Types types;
      //getter setters
      }


      here is my form with select



      <form th:action="${#httpServletRequest.requestURI}" th:object="${myForm}" method="POST" id="form">

      <select name="types" multiple="" id="testSelect"
      th:each="type : ${T(com.test.Types).values()}"
      th:value="${type}"
      th:text="${type}"
      th:selected="*{types != null AND #arrays.contains(types, type)}"
      >
      </select>

      </form>


      here is the error i am getting.



       Property or field 'type' cannot be found on object of type 'com.test.MyForm' - maybe not public or not valid?









      share|improve this question















      I have this enum



      enum Types{
      A, B
      }


      I have a form class



      public class MyForm {
      private Types types;
      //getter setters
      }


      here is my form with select



      <form th:action="${#httpServletRequest.requestURI}" th:object="${myForm}" method="POST" id="form">

      <select name="types" multiple="" id="testSelect"
      th:each="type : ${T(com.test.Types).values()}"
      th:value="${type}"
      th:text="${type}"
      th:selected="*{types != null AND #arrays.contains(types, type)}"
      >
      </select>

      </form>


      here is the error i am getting.



       Property or field 'type' cannot be found on object of type 'com.test.MyForm' - maybe not public or not valid?






      spring thymeleaf






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 14:00







      cool cool

















      asked Nov 12 '18 at 9:46









      cool coolcool cool

      33




      33
























          2 Answers
          2






          active

          oldest

          votes


















          0














          First, I believe you have a typo, it should be type != and not types !=. Also, you are using * in your selected, instead of $. Also, I believe you are using #list.contains() of Thymeleaf in a way that shouldn't work. You should use the whole function, just like this #list.contains(types, type). One last thing, the selected, value and text tags should should go in the option element, not the select. In the end your code should look like the following one.



          <select name="types" multiple="" id="testSelect">
          <option th:each="type : ${T(com.test.Types).values()}"
          th:value="${type}" th:text="${type}"
          th:selected="${types != null AND #arrays.contains(types, type)}">
          </option>
          </select>


          One last thing, I am not sure where did the variable types came from, I am assuming you initialized it somewhere.






          share|improve this answer























          • types is coming from form th:object="${myForm}" please look at the MyForm class.
            – cool cool
            Nov 12 '18 at 13:50










          • it still not resolved by issue
            – cool cool
            Nov 12 '18 at 13:50










          • What error did you get? Some types is actually a field from your form? Is this a newly created object? Or are you sending an already existing object?
            – Alain Cruz
            Nov 12 '18 at 13:52










          • i am submiting my form
            – cool cool
            Nov 12 '18 at 13:53










          • Ok, but what error did you get?
            – Alain Cruz
            Nov 12 '18 at 13:54



















          0














          The best would be to change your form backing bean to have some collection of your enumerations, instead of array such as:



          public class MyForm {
          private List<Types> types = new ArrayList<Types>();
          //getter setters
          }


          Then before you render the form, you can just fill this array with types, which you want to be preselected in the controller just by adding them to the list.



          Then should be able to simply skip th:selected logic...



          <select th:field="*{types}" multiple="multiple" id="testSelect">
          <option th:each="type : ${T(com.test.Types).values()}"
          th:value="${type}" th:text="${type}">
          </option>
          </select>


          Thymeleaf will do the magic for you ;-)






          share|improve this answer





















          • i would like to use th:selected not thymeleaf auto setting.
            – cool cool
            Nov 14 '18 at 7:34











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259490%2fspring-thymeleaf-thselected-on-enumlist-with-multiple-selection%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          First, I believe you have a typo, it should be type != and not types !=. Also, you are using * in your selected, instead of $. Also, I believe you are using #list.contains() of Thymeleaf in a way that shouldn't work. You should use the whole function, just like this #list.contains(types, type). One last thing, the selected, value and text tags should should go in the option element, not the select. In the end your code should look like the following one.



          <select name="types" multiple="" id="testSelect">
          <option th:each="type : ${T(com.test.Types).values()}"
          th:value="${type}" th:text="${type}"
          th:selected="${types != null AND #arrays.contains(types, type)}">
          </option>
          </select>


          One last thing, I am not sure where did the variable types came from, I am assuming you initialized it somewhere.






          share|improve this answer























          • types is coming from form th:object="${myForm}" please look at the MyForm class.
            – cool cool
            Nov 12 '18 at 13:50










          • it still not resolved by issue
            – cool cool
            Nov 12 '18 at 13:50










          • What error did you get? Some types is actually a field from your form? Is this a newly created object? Or are you sending an already existing object?
            – Alain Cruz
            Nov 12 '18 at 13:52










          • i am submiting my form
            – cool cool
            Nov 12 '18 at 13:53










          • Ok, but what error did you get?
            – Alain Cruz
            Nov 12 '18 at 13:54
















          0














          First, I believe you have a typo, it should be type != and not types !=. Also, you are using * in your selected, instead of $. Also, I believe you are using #list.contains() of Thymeleaf in a way that shouldn't work. You should use the whole function, just like this #list.contains(types, type). One last thing, the selected, value and text tags should should go in the option element, not the select. In the end your code should look like the following one.



          <select name="types" multiple="" id="testSelect">
          <option th:each="type : ${T(com.test.Types).values()}"
          th:value="${type}" th:text="${type}"
          th:selected="${types != null AND #arrays.contains(types, type)}">
          </option>
          </select>


          One last thing, I am not sure where did the variable types came from, I am assuming you initialized it somewhere.






          share|improve this answer























          • types is coming from form th:object="${myForm}" please look at the MyForm class.
            – cool cool
            Nov 12 '18 at 13:50










          • it still not resolved by issue
            – cool cool
            Nov 12 '18 at 13:50










          • What error did you get? Some types is actually a field from your form? Is this a newly created object? Or are you sending an already existing object?
            – Alain Cruz
            Nov 12 '18 at 13:52










          • i am submiting my form
            – cool cool
            Nov 12 '18 at 13:53










          • Ok, but what error did you get?
            – Alain Cruz
            Nov 12 '18 at 13:54














          0












          0








          0






          First, I believe you have a typo, it should be type != and not types !=. Also, you are using * in your selected, instead of $. Also, I believe you are using #list.contains() of Thymeleaf in a way that shouldn't work. You should use the whole function, just like this #list.contains(types, type). One last thing, the selected, value and text tags should should go in the option element, not the select. In the end your code should look like the following one.



          <select name="types" multiple="" id="testSelect">
          <option th:each="type : ${T(com.test.Types).values()}"
          th:value="${type}" th:text="${type}"
          th:selected="${types != null AND #arrays.contains(types, type)}">
          </option>
          </select>


          One last thing, I am not sure where did the variable types came from, I am assuming you initialized it somewhere.






          share|improve this answer














          First, I believe you have a typo, it should be type != and not types !=. Also, you are using * in your selected, instead of $. Also, I believe you are using #list.contains() of Thymeleaf in a way that shouldn't work. You should use the whole function, just like this #list.contains(types, type). One last thing, the selected, value and text tags should should go in the option element, not the select. In the end your code should look like the following one.



          <select name="types" multiple="" id="testSelect">
          <option th:each="type : ${T(com.test.Types).values()}"
          th:value="${type}" th:text="${type}"
          th:selected="${types != null AND #arrays.contains(types, type)}">
          </option>
          </select>


          One last thing, I am not sure where did the variable types came from, I am assuming you initialized it somewhere.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 '18 at 18:01

























          answered Nov 12 '18 at 13:28









          Alain CruzAlain Cruz

          1,7681818




          1,7681818












          • types is coming from form th:object="${myForm}" please look at the MyForm class.
            – cool cool
            Nov 12 '18 at 13:50










          • it still not resolved by issue
            – cool cool
            Nov 12 '18 at 13:50










          • What error did you get? Some types is actually a field from your form? Is this a newly created object? Or are you sending an already existing object?
            – Alain Cruz
            Nov 12 '18 at 13:52










          • i am submiting my form
            – cool cool
            Nov 12 '18 at 13:53










          • Ok, but what error did you get?
            – Alain Cruz
            Nov 12 '18 at 13:54


















          • types is coming from form th:object="${myForm}" please look at the MyForm class.
            – cool cool
            Nov 12 '18 at 13:50










          • it still not resolved by issue
            – cool cool
            Nov 12 '18 at 13:50










          • What error did you get? Some types is actually a field from your form? Is this a newly created object? Or are you sending an already existing object?
            – Alain Cruz
            Nov 12 '18 at 13:52










          • i am submiting my form
            – cool cool
            Nov 12 '18 at 13:53










          • Ok, but what error did you get?
            – Alain Cruz
            Nov 12 '18 at 13:54
















          types is coming from form th:object="${myForm}" please look at the MyForm class.
          – cool cool
          Nov 12 '18 at 13:50




          types is coming from form th:object="${myForm}" please look at the MyForm class.
          – cool cool
          Nov 12 '18 at 13:50












          it still not resolved by issue
          – cool cool
          Nov 12 '18 at 13:50




          it still not resolved by issue
          – cool cool
          Nov 12 '18 at 13:50












          What error did you get? Some types is actually a field from your form? Is this a newly created object? Or are you sending an already existing object?
          – Alain Cruz
          Nov 12 '18 at 13:52




          What error did you get? Some types is actually a field from your form? Is this a newly created object? Or are you sending an already existing object?
          – Alain Cruz
          Nov 12 '18 at 13:52












          i am submiting my form
          – cool cool
          Nov 12 '18 at 13:53




          i am submiting my form
          – cool cool
          Nov 12 '18 at 13:53












          Ok, but what error did you get?
          – Alain Cruz
          Nov 12 '18 at 13:54




          Ok, but what error did you get?
          – Alain Cruz
          Nov 12 '18 at 13:54













          0














          The best would be to change your form backing bean to have some collection of your enumerations, instead of array such as:



          public class MyForm {
          private List<Types> types = new ArrayList<Types>();
          //getter setters
          }


          Then before you render the form, you can just fill this array with types, which you want to be preselected in the controller just by adding them to the list.



          Then should be able to simply skip th:selected logic...



          <select th:field="*{types}" multiple="multiple" id="testSelect">
          <option th:each="type : ${T(com.test.Types).values()}"
          th:value="${type}" th:text="${type}">
          </option>
          </select>


          Thymeleaf will do the magic for you ;-)






          share|improve this answer





















          • i would like to use th:selected not thymeleaf auto setting.
            – cool cool
            Nov 14 '18 at 7:34
















          0














          The best would be to change your form backing bean to have some collection of your enumerations, instead of array such as:



          public class MyForm {
          private List<Types> types = new ArrayList<Types>();
          //getter setters
          }


          Then before you render the form, you can just fill this array with types, which you want to be preselected in the controller just by adding them to the list.



          Then should be able to simply skip th:selected logic...



          <select th:field="*{types}" multiple="multiple" id="testSelect">
          <option th:each="type : ${T(com.test.Types).values()}"
          th:value="${type}" th:text="${type}">
          </option>
          </select>


          Thymeleaf will do the magic for you ;-)






          share|improve this answer





















          • i would like to use th:selected not thymeleaf auto setting.
            – cool cool
            Nov 14 '18 at 7:34














          0












          0








          0






          The best would be to change your form backing bean to have some collection of your enumerations, instead of array such as:



          public class MyForm {
          private List<Types> types = new ArrayList<Types>();
          //getter setters
          }


          Then before you render the form, you can just fill this array with types, which you want to be preselected in the controller just by adding them to the list.



          Then should be able to simply skip th:selected logic...



          <select th:field="*{types}" multiple="multiple" id="testSelect">
          <option th:each="type : ${T(com.test.Types).values()}"
          th:value="${type}" th:text="${type}">
          </option>
          </select>


          Thymeleaf will do the magic for you ;-)






          share|improve this answer












          The best would be to change your form backing bean to have some collection of your enumerations, instead of array such as:



          public class MyForm {
          private List<Types> types = new ArrayList<Types>();
          //getter setters
          }


          Then before you render the form, you can just fill this array with types, which you want to be preselected in the controller just by adding them to the list.



          Then should be able to simply skip th:selected logic...



          <select th:field="*{types}" multiple="multiple" id="testSelect">
          <option th:each="type : ${T(com.test.Types).values()}"
          th:value="${type}" th:text="${type}">
          </option>
          </select>


          Thymeleaf will do the magic for you ;-)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 21:59









          jayjay

          6318




          6318












          • i would like to use th:selected not thymeleaf auto setting.
            – cool cool
            Nov 14 '18 at 7:34


















          • i would like to use th:selected not thymeleaf auto setting.
            – cool cool
            Nov 14 '18 at 7:34
















          i would like to use th:selected not thymeleaf auto setting.
          – cool cool
          Nov 14 '18 at 7:34




          i would like to use th:selected not thymeleaf auto setting.
          – cool cool
          Nov 14 '18 at 7:34


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259490%2fspring-thymeleaf-thselected-on-enumlist-with-multiple-selection%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Full-time equivalent

          Bicuculline

          さくらももこ