spring mvc: How Can I give the <form:textarea /> tag a default value?

前端 未结 7 1408
悲&欢浪女
悲&欢浪女 2021-01-18 17:19

I have a problem with giving the tag a default value.
When I created a .jsp file as follow,



        
相关标签:
7条回答
  • 2021-01-18 17:39

    I faced a similar situation. The below code:

    <form:textarea path="Content" id="my-text-box">${content}</form:textarea>
    

    Worked out for me.

    0 讨论(0)
  • 2021-01-18 17:40

    This works in Spring 4 where the form:textarea tag must be empty:

    <form:textarea path="content" value="${Object.content}"/>

    0 讨论(0)
  • 2021-01-18 17:43

    You can use jquery to get the model attribute value and set the value in textarea

     $( function() {
             var example = parseInt('${example}');       
             $("#bot").val(example); 
        } );
    
    0 讨论(0)
  • 2021-01-18 17:45

    Maybe you can use placeholder.

                <form:textarea
                                id="id"
                                class="class"
                                path="path"
                                placeholder="test"/>
    
    0 讨论(0)
  • 2021-01-18 17:54

    You can also set the defaul text in the Model as well. Your jsp would have:

    <form:textarea path="my-text-box" rows="20" cols="100" cssClass="rounded"/>
    

    and then in the Model, you'd have:

    private static final String DEFAULT_textareacontent = "Hello World";
    private String my-text-box = DEFAULT_textareacontent;
    

    Not sure if this is a best practice or not, but it seems to work for me.

    0 讨论(0)
  • 2021-01-18 17:58

    You have to use JS along with Spring MVC to make this work. This is what I did:

    1. Change your textarea to basic html as below:

       <textarea id="myTextArea" onchange="here();">${content}</textarea>
        OR
       <textarea id="myTextArea" onchange="here();">third hello world!</textarea>
      
    2. Add a hidden input field, using spring mvc:

      <form:input type="hidden" id="content" path="Content" value="third hello world!"/>
      
    3. Add the following JavaScript:

      <script>
        var text1 = document.getElementById('myTextArea').value;
        function here() {
           text1 = document.getElementById('myTextArea').value;
           document.getElementById("content").value = text1;
        }
      </script>
      
    0 讨论(0)
提交回复
热议问题