Spring [SOLVED]: Controller Not receiving value from span in HTML using Spring boot and Thymeleaf

Spring [SOLVED]: Controller Not receiving value from span in HTML using Spring boot and Thymeleaf

Home Forums Frameworks Spring Spring [SOLVED]: Controller Not receiving value from span in HTML using Spring boot and Thymeleaf

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #243791

    Cloudy Point
    Keymaster

    QuestionQuestion

    I have the following content in my HTML which is using Thymeleaf

    <form action="#" th:action="@{/shutDown}" th:object="${ddata}" method="post">
            <span>Domain</span>
            <span th:text="${domain}" th:field="*{domain}">domain</span>
            <input type="Submit" value="close" />
    </form>
    

    And I have the following in my Controller which is using Sprint Boot

    @RequestMapping(value = "/shutDown", method = RequestMethod.POST)
    public ModelAndView shutDownPage(ModelAndView modelAndView, Authentication authentication,
            @ModelAttribute("ddata") DInputBean dInputBean) {
        String domain = dInputBean.getdomain();
        return modelAndView;
    }
    

    I’m hoping I’d get value of domain from the HTML in the Controller but it’s always null. DInputBean has getters and setters for “domain” field.

    #243792

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    The th:field attribute can be used on <input>, <select>, or, <textarea>.

    A solution you could possibly replacing you second <span> with a hidden input element.

    <form action="#" th:action="@{/shutDown}" th:object="${ddata}" method="post">
        <span>Domain</span>
        <input type="hidden" th:field="*{domain}" th:value="${domain}" />
        <input type="Submit" value="close" />
    </form>
    

    If you wanted to keep the second div, just place the <input type="hidden"> inside the second <span> and remove the th:field attribute from the second <span>.

    Edit:

    If you wanted to add the value of domain in a span.

    <form action="#" th:action="@{/shutDown}" th:object="${ddata}" method="post">
        <span>Domain</span>
        <span th:text="${domain}">domain<span>
        <input type="hidden" th:field="*{domain}" th:value="${domain}" />
        <input type="Submit" value="close" />
    </form>
    

    http://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#inputs

    Source: https://stackoverflow.com/questions/47860432/controller-not-receiving-value-from-span-in-html-using-spring-boot-and-thymeleaf
    Author: jmw5598
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.