简体   繁体   English

从textarea读取换行

[英]Read new line from textarea

I have a web application built. 我建立了一个Web应用程序。 I am trying to fetch the text entered in form:textarea . 我正在尝试获取在form:textarea输入的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? 我得到的结果是hi! How Are You? hi! How Are You? , the new lines are missed. ,新行将丢失。

Here is my code sample: 这是我的代码示例:

JSP: 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 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. 请建议我是否需要更改方法或尝试其他方法。

Have you tried br tag? 您尝试过br标签吗? try using br tag where you are printing the result. 尝试在要打印结果的位置使用br标签。

Basically a white space character(which includes new line chr too) is converted by the HTML to single space. 基本上,HTML将空格字符(也包括换行符chr)转换为单个空格。 You will need to write onChange method for the textarea to convert the new line char to either <br> or <p> . 您将需要为textarea编写onChange方法,以将新行char转换为<br><p> By this you will be able to hold the property of new line. 这样,您将能够保留换行符的属性。

Please review this answer: https://stackoverflow.com/a/29575181/3465242 请查看以下答案: https : //stackoverflow.com/a/29575181/3465242

看起来您将其显示为HTML,请尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM