【问题标题】:Return contents of rich text editor to servlet将富文本编辑器的内容返回给servlet
【发布时间】:2017-04-22 18:29:02
【问题描述】:

我想将输入到summernote富文本编辑器中的任何内容返回到servlet,这样我就可以将它保存到数据库中,但我不知道该怎么做。

我的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>

jsp中的按钮提交给servlet:

新内容: 这里

小服务程序:

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

【问题讨论】:

    标签: java jsp servlets summernote


    【解决方案1】:

    实际问题应该是:

    如何设置文本值到表单元素?
    如何提交到服务器?

    答案:

    <!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>
    

    解释:

    • 添加隐藏的文本区域
    • 在提交之前将编辑器的内容复制到文本区域。
    • 但是使用表单方法post,因为它有更高的最大长度限制 你可以阅读更多关于maximum length of HTTP GET request?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多