1

I have a web application built. I am trying to fetch the text entered in form:textarea. I want to read the text entered by the user in the same format. Example if the user enters:

hi!
How
Are
you?

I am getting the result as hi! How Are You?, the new lines are missed.

Here is my code sample:

JSP:

 <form:form id="addRssFeed" modelAttribute="rssFeed" action="apiTextContent?mode=addNew&page=rssFeeds" enctype="multipart/form-data" method="post">
          <div class="form-group" style="width: 392px; ">
            <div class="form-row">
              <div >
                 <form:label path="inputTextFiled1" for="tile">Title: </form:label>
             <form:input  path="inputTextFiled1" autocomplete="off" class="form-control" id="title" type="text" placeholder="Enter the title to the Feed"/>
              </div>
              </div>
              <div class="form-row">
              <div >
               <form:label  path="inputTextFiled2" for="content">Content: </form:label>
               <form:textarea path="inputTextFiled2" class="form-control" style="width: 200%; height: 200px;" id="encJs" placeholder="Enter the body of Feed"></form:textarea>

              </div>
            </div>
             <div class="form-row">
              <div >
              <input name="file" id="fileToUpload" type="file" onchange="validateImage()"/>
              </div>
            </div>
              <div >
<%--       <p><form:input name="file" id="fileToUpload" type="file" onchange="validateImage()"/></p> --%>  <p id="message"></p></div> 
          </div>
          <a href="#" onclick="#">Show Preview </a>
          <form:button class="btn btn-primary btn-block"  id="Publish" style="width: 25%;margin: 0px auto;">Publish</form:button>

        </form:form>

ApiText.java

    private String inputTextFiled1;
    private String inputTextFiled2;

--- getters and setters---

Service

public String convertToImage(CommonsMultipartFile file, ApiTextModel apiTextModel) throws IOException {

        StringBuffer imageString = new StringBuffer("");
        String html= null;

        if(file.getSize() !=0){
             File convFile = new File( file.getOriginalFilename());
             file.transferTo(convFile);

            BufferedImage src;

            src = ImageIO.read(convFile);

            BufferedImage image = toBufferedImage(src);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(image, "png", baos);

            String data = DatatypeConverter.printBase64Binary(baos.toByteArray());
            imageString.append("<img  style=\"display:block; height: 300px; margin-left: auto; margin-right: auto;\"  src='data:image/png;base64," + data+"'>");

        }

            html =  "<html><body> "+
                    "<h2 style=\"text-align: center;font-size: 49px;\">"+apiTextModel.getInputTextFiled1() +" </h2> "+ imageString.toString() +
                    "<div style=\"font-size: 25px;\">"+apiTextModel.getInputTextFiled2() +"</div></body></html>";
            System.out.println(imageString);
        return html;

    }

Please suggest if i need to change my approach or try something else.

Rob
  • 26,989
  • 16
  • 82
  • 98

3 Answers3

0

Have you tried br tag? try using br tag where you are printing the result.

Aljo Anto
  • 1
  • 2
0

Basically a white space character(which includes new line chr too) is converted by the HTML to single space. You will need to write onChange method for the textarea to convert the new line char to either <br> or <p>. By this you will be able to hold the property of new line.

Please review this answer: https://stackoverflow.com/a/29575181/3465242

Ashu
  • 2,066
  • 3
  • 19
  • 33
0

Looks like you display it as HTML, please try:

String text = text.replace("\n", "<br>");

Piotr Niewinski
  • 1,298
  • 2
  • 15
  • 27