How to pass innerHTML content to th:field in thymeleaf?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?Formatting th:field in ThymeleafHow to configure port for a Spring Boot applicationHow to set thymeleaf th:field value from other variableThymeleaf - The difference between th:field=“$” and th:field=“*”Naming variables/fields?? Not accepting names - Thymeleaf and Spring Boot MVC(Spring, Thymeleaf) How to request to controller 'POST' with list of model inside a model?Thymeleaf th:field is not accepting in <label> tag,it does not showing dataNot able to use th:field of thymeleafthymeleaf th:field returns erros when i use the th:field

What's the term for a group of people who enjoy literary works?

A conjectural trigonometric identity

Is it really a problem to declare that a visitor to the UK is my "girlfriend", in terms of her successfully getting a Standard Visitor visa?

How do I safety check that there is no light in Darkroom / Darkbag?

Being told my "network" isn't PCI Complaint. I don't even have a server! Do I have to comply?

Should 2FA be enabled on service accounts?

Export economy of Mars

Applied Meditation

Cross out words with TikZ: line opacity

Reasons for using monsters as bioweapons

Overprovisioning SSD on ubuntu. How? Ubuntu 19.04 Samsung SSD 860

Meaning of 誰かの代わりに

Why have both: BJT and FET transistors on IC output?

Map vs. Table for index-specific operations on 2D arrays

Accurately recalling the key - can everyone do it?

Can I say "Gesundheit" if someone is coughing?

How to draw twisted cuves?

Flat maps and Zariski tangent spaces

How can a class have multiple methods without breaking the single responsibility principle

Sci-fi movie featuring a fat man running sideways on a wall

The grades of the students in a class

How to power down external drive safely

Checking whether the win-loss standings of a league are possible

Ernie and the Superconducting Boxes



How to pass innerHTML content to th:field in thymeleaf?


How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?Formatting th:field in ThymeleafHow to configure port for a Spring Boot applicationHow to set thymeleaf th:field value from other variableThymeleaf - The difference between th:field=“$” and th:field=“*”Naming variables/fields?? Not accepting names - Thymeleaf and Spring Boot MVC(Spring, Thymeleaf) How to request to controller 'POST' with list of model inside a model?Thymeleaf th:field is not accepting in <label> tag,it does not showing dataNot able to use th:field of thymeleafthymeleaf th:field returns erros when i use the th:field






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am creating a Spring Boot e-commerce website, and I have a span tag containing the total price so far in cart, and update with JavaScript behind the scenes:



<form method="POST" th:object="$chargeRequest">
<span class="total">
Total: <span class="total-price">$0</span>
</span>
</form>


and I pass a model attribute model.addAttribute("chargeRequest", new ChargeRequest()); to this page, and ChargeRequest class is defined as:



public class ChargeRequest 
private int amount;

// constructor, setter/getter
// ...



The question is: how can update amount in chargeRequest, and pass it back to the controller using Thymeleaf?



th:field is only valid for <input>, <select>, <textarea>, I cannot directly put it to <span> tag



I have tried to pass 2 attributes:



model.addAttribute("amount", new String("$0"));
model.addAttribute("chargeRequest", new ChargeRequest());


<span class="total">
Total: <span class="total-price" th:text="$amount">$0</span>
<input type="hidden" th:value="$amount, id="amount", name="amount">
</span>


But I don't know how to update $amount such that amount in chargeReqeust can be updated automatically?










share|improve this question






























    0















    I am creating a Spring Boot e-commerce website, and I have a span tag containing the total price so far in cart, and update with JavaScript behind the scenes:



    <form method="POST" th:object="$chargeRequest">
    <span class="total">
    Total: <span class="total-price">$0</span>
    </span>
    </form>


    and I pass a model attribute model.addAttribute("chargeRequest", new ChargeRequest()); to this page, and ChargeRequest class is defined as:



    public class ChargeRequest 
    private int amount;

    // constructor, setter/getter
    // ...



    The question is: how can update amount in chargeRequest, and pass it back to the controller using Thymeleaf?



    th:field is only valid for <input>, <select>, <textarea>, I cannot directly put it to <span> tag



    I have tried to pass 2 attributes:



    model.addAttribute("amount", new String("$0"));
    model.addAttribute("chargeRequest", new ChargeRequest());


    <span class="total">
    Total: <span class="total-price" th:text="$amount">$0</span>
    <input type="hidden" th:value="$amount, id="amount", name="amount">
    </span>


    But I don't know how to update $amount such that amount in chargeReqeust can be updated automatically?










    share|improve this question


























      0












      0








      0








      I am creating a Spring Boot e-commerce website, and I have a span tag containing the total price so far in cart, and update with JavaScript behind the scenes:



      <form method="POST" th:object="$chargeRequest">
      <span class="total">
      Total: <span class="total-price">$0</span>
      </span>
      </form>


      and I pass a model attribute model.addAttribute("chargeRequest", new ChargeRequest()); to this page, and ChargeRequest class is defined as:



      public class ChargeRequest 
      private int amount;

      // constructor, setter/getter
      // ...



      The question is: how can update amount in chargeRequest, and pass it back to the controller using Thymeleaf?



      th:field is only valid for <input>, <select>, <textarea>, I cannot directly put it to <span> tag



      I have tried to pass 2 attributes:



      model.addAttribute("amount", new String("$0"));
      model.addAttribute("chargeRequest", new ChargeRequest());


      <span class="total">
      Total: <span class="total-price" th:text="$amount">$0</span>
      <input type="hidden" th:value="$amount, id="amount", name="amount">
      </span>


      But I don't know how to update $amount such that amount in chargeReqeust can be updated automatically?










      share|improve this question














      I am creating a Spring Boot e-commerce website, and I have a span tag containing the total price so far in cart, and update with JavaScript behind the scenes:



      <form method="POST" th:object="$chargeRequest">
      <span class="total">
      Total: <span class="total-price">$0</span>
      </span>
      </form>


      and I pass a model attribute model.addAttribute("chargeRequest", new ChargeRequest()); to this page, and ChargeRequest class is defined as:



      public class ChargeRequest 
      private int amount;

      // constructor, setter/getter
      // ...



      The question is: how can update amount in chargeRequest, and pass it back to the controller using Thymeleaf?



      th:field is only valid for <input>, <select>, <textarea>, I cannot directly put it to <span> tag



      I have tried to pass 2 attributes:



      model.addAttribute("amount", new String("$0"));
      model.addAttribute("chargeRequest", new ChargeRequest());


      <span class="total">
      Total: <span class="total-price" th:text="$amount">$0</span>
      <input type="hidden" th:value="$amount, id="amount", name="amount">
      </span>


      But I don't know how to update $amount such that amount in chargeReqeust can be updated automatically?







      spring spring-boot spring-mvc thymeleaf






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 26 at 23:50









      RainSugarRainSugar

      112 bronze badges




      112 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          0














          Well unfortunately for you, you won't be able to do that with a simple controller. You will need a @ControllerAdvice that will serve as a global controller to be able to update the cart whenever someone adds a product into their cart or remove products from their cart.



          In other word, you need a GLOBAL CONTROLLER. This global controller should check the SESSION attribute to allow you to change the amount of total cart and update it every time a change occurs. For example, check this out below.



          @ControllerAdvice
          public class GlobalCartController{
          @Autowired
          private HttpSession session;
          @ModelAttribute("cartModel")
          public CartModel getCartTotal()
          if(session.getAttribute("cart")==null)
          //Here you create cart attribute for the session
          // then
          session.setAttribute("cartModel", cartObjectWithUpdatedTotal);

          return (CartObjectWithUpdatedTotal) session.getAttribute("cartModel");




          Note that global controller will be always checked at every time at all routes and don't require a routing, this allows you to use the HttpSession autowired class which allows you to




          identify a user across more than one-page request or visit a Web site and to store information about that user.




          Cheers






          share|improve this answer
























            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%2f55367802%2fhow-to-pass-innerhtml-content-to-thfield-in-thymeleaf%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Well unfortunately for you, you won't be able to do that with a simple controller. You will need a @ControllerAdvice that will serve as a global controller to be able to update the cart whenever someone adds a product into their cart or remove products from their cart.



            In other word, you need a GLOBAL CONTROLLER. This global controller should check the SESSION attribute to allow you to change the amount of total cart and update it every time a change occurs. For example, check this out below.



            @ControllerAdvice
            public class GlobalCartController{
            @Autowired
            private HttpSession session;
            @ModelAttribute("cartModel")
            public CartModel getCartTotal()
            if(session.getAttribute("cart")==null)
            //Here you create cart attribute for the session
            // then
            session.setAttribute("cartModel", cartObjectWithUpdatedTotal);

            return (CartObjectWithUpdatedTotal) session.getAttribute("cartModel");




            Note that global controller will be always checked at every time at all routes and don't require a routing, this allows you to use the HttpSession autowired class which allows you to




            identify a user across more than one-page request or visit a Web site and to store information about that user.




            Cheers






            share|improve this answer





























              0














              Well unfortunately for you, you won't be able to do that with a simple controller. You will need a @ControllerAdvice that will serve as a global controller to be able to update the cart whenever someone adds a product into their cart or remove products from their cart.



              In other word, you need a GLOBAL CONTROLLER. This global controller should check the SESSION attribute to allow you to change the amount of total cart and update it every time a change occurs. For example, check this out below.



              @ControllerAdvice
              public class GlobalCartController{
              @Autowired
              private HttpSession session;
              @ModelAttribute("cartModel")
              public CartModel getCartTotal()
              if(session.getAttribute("cart")==null)
              //Here you create cart attribute for the session
              // then
              session.setAttribute("cartModel", cartObjectWithUpdatedTotal);

              return (CartObjectWithUpdatedTotal) session.getAttribute("cartModel");




              Note that global controller will be always checked at every time at all routes and don't require a routing, this allows you to use the HttpSession autowired class which allows you to




              identify a user across more than one-page request or visit a Web site and to store information about that user.




              Cheers






              share|improve this answer



























                0












                0








                0







                Well unfortunately for you, you won't be able to do that with a simple controller. You will need a @ControllerAdvice that will serve as a global controller to be able to update the cart whenever someone adds a product into their cart or remove products from their cart.



                In other word, you need a GLOBAL CONTROLLER. This global controller should check the SESSION attribute to allow you to change the amount of total cart and update it every time a change occurs. For example, check this out below.



                @ControllerAdvice
                public class GlobalCartController{
                @Autowired
                private HttpSession session;
                @ModelAttribute("cartModel")
                public CartModel getCartTotal()
                if(session.getAttribute("cart")==null)
                //Here you create cart attribute for the session
                // then
                session.setAttribute("cartModel", cartObjectWithUpdatedTotal);

                return (CartObjectWithUpdatedTotal) session.getAttribute("cartModel");




                Note that global controller will be always checked at every time at all routes and don't require a routing, this allows you to use the HttpSession autowired class which allows you to




                identify a user across more than one-page request or visit a Web site and to store information about that user.




                Cheers






                share|improve this answer













                Well unfortunately for you, you won't be able to do that with a simple controller. You will need a @ControllerAdvice that will serve as a global controller to be able to update the cart whenever someone adds a product into their cart or remove products from their cart.



                In other word, you need a GLOBAL CONTROLLER. This global controller should check the SESSION attribute to allow you to change the amount of total cart and update it every time a change occurs. For example, check this out below.



                @ControllerAdvice
                public class GlobalCartController{
                @Autowired
                private HttpSession session;
                @ModelAttribute("cartModel")
                public CartModel getCartTotal()
                if(session.getAttribute("cart")==null)
                //Here you create cart attribute for the session
                // then
                session.setAttribute("cartModel", cartObjectWithUpdatedTotal);

                return (CartObjectWithUpdatedTotal) session.getAttribute("cartModel");




                Note that global controller will be always checked at every time at all routes and don't require a routing, this allows you to use the HttpSession autowired class which allows you to




                identify a user across more than one-page request or visit a Web site and to store information about that user.




                Cheers







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 27 at 4:07









                EyoelDEyoelD

                5571 gold badge5 silver badges18 bronze badges




                5571 gold badge5 silver badges18 bronze badges





















                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55367802%2fhow-to-pass-innerhtml-content-to-thfield-in-thymeleaf%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

                    How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

                    155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

                    Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?