简体   繁体   English

将富文本编辑器的内容返回到servlet

[英]Return contents of rich text editor to servlet

I want to return the contents of whatever is typed into the summernote rich text editor back to a servlet so I then can save it into a database but I cannot figure how to do it. 我想将在summernote富文本编辑器中键入的所有内容返回给servlet,这样我就可以将其保存到数据库中,但是我不知道该怎么做。

My jsp page script: 我的jsp页面脚本:

<script>
  $(document).ready(function() 
  {
      $('#summernote').summernote();
  });
</script>

<script type="text/javascript">
  var markupStr = $('#summernote').summernote('code'); //this gets the contents from the text editor.
  function myFunction()
  {
      return markupStr = $('#summernote').summernote('code');
  }
</script>

Button in jsp to submit to servlet: jsp中的按钮提交给servlet:

new content: here 新内容:此处

Servlet: Servlet的:

 String test = request.getParameter("summerNoteText"); 

The actual questions should be: 实际的问题应该是:

How to set text value to and form element? 如何设置文本值和形成元素?
How to submit it to the server? 如何将其提交到服务器?

The answers: 答案:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>add text to text area and submit</title>
<!-- include libraries(jQuery, bootstrap) -->
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet">
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script> 
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.js"></script> 

<!-- include summernote css/js-->
<link href="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.2/summernote.css" rel="stylesheet">
<script src="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.2/summernote.js"></script>
<body>
    <div id="summernote">Hello Summernote</div>
    <script>
        $(document).ready(function() {
            $('#summernote').summernote();

            // copy the html-text from summernote to the hidden textarea
            // and let the browser submit it
            $('#myForm').submit(function() {
                $('textarea[name=summerNoteText]').val($('#summernote').summernote('code'));
            });
        }); 
    </script>
    <!-- use method post, because method get has limits of the max length -->
    <form action="Test" method="post" id="myForm">
        <!-- add a hidden textarea, wher the summernote code will be writen on submit -->
        <textarea name="summerNoteText" style="display:none;"></textarea>
        <input type="submit"/>
    </form>
</body>
</html>

Explained: 解释:

  • add a hidden textarea 添加一个隐藏的文本区域
  • copy the content of the editor to the textarea before submitting it. 在提交之前,将编辑器的内容复制到文本区域。
  • but use form method post, because of it has higher max length limit You can read more on maximum length of HTTP GET request? 但请使用表单方法post,因为它具有更高的最大长度限制您可以阅读有关HTTP GET请求的最大长度的更多信息吗?

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

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