spring mvc: How Can I give the <form:textarea /

2020-04-07 05:48发布

I have a problem with giving the <form:textarea /> tag a default value.
When I created a .jsp file as follow,

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

jsp parser translate the above line to:

<textarea id="my-text-box" name="Content"></textarea>third hello world!

Also, giving the value attribute does not work.

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

jsp gives me as html output:

<textarea id="my-text-box" name="Content" value="third hello world!"></textarea>

you can see the <textarea> tag does not have the value attribute.

How can I pass a default value to <form:textarea> tag? Thank you in advance.

7条回答
▲ chillily
2楼-- · 2020-04-07 06:26

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>
    
查看更多
登录 后发表回答