Search code examples
htmlstruts2textareaibatis

Loading a textarea without new lines


Here is my problem : in my database, I got a VACHAR2 of length 2000. Let's call it the "commentReception".

It is mapped to a String in my Struts bean.

The jspx (that handles both consult and update, within the form) looks that way :

<s:if test="%{consultAction}">
  <s:textarea name="dto.commentReception" readonly="true" cssClass="textAreaValue readonly" />
</s:if>
<s:else>
  <s:textarea name="dto.commentReception" readonly="false" cssClass="textAreaValue" />
</s:else>

When I submit the form, the bean is filled correctly, meaning without any \r\n at the end, and I get only one line, even in the database : no newlines.

But when I switch to consultAction, or let's say re-edit the form (not consultAction) another time, I get my string with an extra 2 newlines. That's really annoying and I don't know if it comes from Struts or from the tag. (with me probably missing a tag attribute?)

Of course, if I submit the form again, the comment string will be stored in the database with the newlines. So it will add two more new lines every saving of my comment...

Do you know where my problem comes from? Thanks already.

Edit : the generated form

<textarea class="textAreaValue" id="saveControleReception_dto_commentReceiption" rows="" cols="" name="dto.commentReceiption">commentaire contrôle avant reception.

</textarea>

You can see there are some newlines, but they are not taken into account in my bean. But they are somehow when I reload it.


Solution

  • So, it seems that the struts tag is adding by default two \r\n to the String upon loading the bean. That must be some kind of bug. So here is my solution :

    <s:if test="%{consultAction}">
      <textarea readonly="true" class="textAreaValue readonly"><s:property value="dto.commentReceiption"/></textarea>
    </s:if>
    <s:else>
      <textarea class="textAreaValue" name="dto.commentReceiption"><s:property value="dto.commentReceiption"/></textarea>
    </s:else>
    

    I used the html tag instead of Strut's, and the property tag... If someone has a better answer (there must be), I'm still listening ;)