Passing value from controller to html in spring

前端 未结 3 829
挽巷
挽巷 2021-01-20 07:24

Hello I have a simple web page where I have a button and a text near to button. I need to change text when button clicked and get the new text from code.

This is con

相关标签:
3条回答
  • 2021-01-20 07:54

    Try these : <p align="center">${stream}</p> or <p th:text="${stream}"></p>

    This tag may be causing a problem :

    <button class="button"
            onclick="window.location.href = '/stream';">Stream</button>
    

    Please check it by removing this.

    0 讨论(0)
  • 2021-01-20 08:01

    Thank you for your help. Below shown line helped:

    <p th:inline="text">[[${stream}]]</p>
    
    0 讨论(0)
  • 2021-01-20 08:10

    Change

    <p align="center">?????</p>
    

    To

    <p align="center">${stream}</p> OR <p th:text="${stream}"></p> 
    

    How it is working?

    You can access variables value by ${key}.

    Example

    model.addAttribute("key", value);   
    

    Get value by ${key} in HTML

    In Thymeleaf, these model attributes (or context variables in Thymeleaf jargon) can be accessed with the following syntax: ${attributeName}, where attributeName in our case is stream. This is a Spring EL expression. In short, Spring EL (Spring Expression Language) is a language that supports querying and manipulating an object graph at runtime.

    UPDATE

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

    Replace <p align="center">?????</p> with

    <input type="text" id="stream" name="stream" th:value="${stream}" />
    

    OR

    <input type="text" th:field="*{stream}" />`
    

    OR

    <input type="text" id="stream" name="stream" th:field="*{stream}" th:value="${stream}" />
    

    Also try <p th:inline="text">[[${stream}]]</p>; <p data-th-text="${stream}" />

    Thymeleaf Document Thymeleaf Document Inputs


    UPDATE 2

    Get value from Thymeleaf to Spring Boot

    <form th:action="@{/send}" method="get">
       <textarea  th:name="doc" rows="10" cols="100" name="doc"></textarea>
       <button type="submit">Send</button>
    </form>
    
        @GetMapping("/send")
        public String send(@RequestParam(name="doc", required = false) String doc) {
            //change required = false as per requirement
            System.out.println("Doc: "+doc);
            return "textarea-input";
        }
    

    Note: use "th:field" for Entity/Model

    0 讨论(0)
提交回复
热议问题