即使成功,从 ckeditor 保存的数据在我的数据库中也显示为空

问题描述 投票:0回答:1

我正在开发一个 Web 应用程序,其中使用 CKEditor 允许用户输入问题和多项选择选项。我正在尝试通过 Java Servlet 将此数据保存到 MSSQL 数据库。

这是发生的事情:

数据是使用 AJAX 请求从前端发送的。

servlet 处理请求并返回“数据保存成功!”留言。

但是,当我检查数据库时,数据实际上并没有保存。

这是我在 servlet 中使用的代码:

@WebServlet("/SubmitExamServlet")
public class SubmitExamServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String examTitle = request.getParameter("title");
        String subject = request.getParameter("subject");
        String question = request.getParameter("question");
        String option1 = request.getParameter("option1");
        String option2 = request.getParameter("option2");
        String option3 = request.getParameter("option3");
        String option4 = request.getParameter("option4");
        int correctOption = Integer.parseInt(request.getParameter("correctOption"));

        String DB_URL = "jdbc:sqlserver://THE_NOOB_BANANA;Database=ExamPortal;encrypt=false;integratedSecurity=true";

        
        try {
            Connection conn = DriverManager.getConnection(DB_URL);
            String sql = "INSERT INTO ExamQuestions (ExamTitle, Subject, QuestionText, Option1, Option2, Option3, Option4, CorrectOption) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, examTitle);
            stmt.setString(2, subject);
            stmt.setString(3, question);
            stmt.setString(4, option1);
            stmt.setString(5, option2);
            stmt.setString(6, option3);
            stmt.setString(7, option4);
            stmt.setInt(8, correctOption);

            stmt.executeUpdate();
            conn.close();

            response.sendRedirect("success.jsp");
        } catch (SQLException e) {
            e.printStackTrace();
            response.sendRedirect("error.jsp");
        }
    }
}

还有我的jsp脚本:

<script>
    function initializeCKEditors() {
        var editorConfig = {
            extraPlugins: 'mathjax',
            mathJaxLib: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML'
        };

        function suppressNotification(editor) {
            editor.on('instanceReady', function () {
                editor.showNotification = function (message, type) {
                    if (message.id !== 'editor-is-not-secure') {
                        return this.showNotification.original.call(this, message, type);
                    }
                };
                editor.showNotification.original = editor.showNotification;
            });
        }

        var questionEditor = CKEDITOR.replace('question', editorConfig);
        suppressNotification(questionEditor);

        var option1Editor = CKEDITOR.replace('option1', editorConfig);
        suppressNotification(option1Editor);

        var option2Editor = CKEDITOR.replace('option2', editorConfig);
        suppressNotification(option2Editor);

        var option3Editor = CKEDITOR.replace('option3', editorConfig);
        suppressNotification(option3Editor);

        var option4Editor = CKEDITOR.replace('option4', editorConfig);
        suppressNotification(option4Editor);
    }

    function sendDataToServlet() {
        // Collecting the correct option value
        var correctOption = $("input[name='correctOption']:checked").val();

        $.ajax({
            type: 'POST',
            url: 'SubmitExamServlet',
            data: {
                question: CKEDITOR.instances.question.getData(),
                option1: CKEDITOR.instances.option1.getData(),
                option2: CKEDITOR.instances.option2.getData(),
                option3: CKEDITOR.instances.option3.getData(),
                option4: CKEDITOR.instances.option4.getData(),
                correctOption: correctOption
            },
            success: function(response) {
                alert('Data saved successfully!');
            },
            error: function() {
                alert('An error occurred while saving the data.');
            }
        });
    }

    document.addEventListener('DOMContentLoaded', function() {
        initializeCKEditors();

        // Bind the AJAX submission to the publish button
        document.querySelector('.publish-btn').addEventListener('click', function(event) {
            event.preventDefault();
            sendDataToServlet();
        });
    });
</script>

我没有从 CKEditor 正确获取数据吗?

这是我尝试/检查过的:

数据库连接字符串和凭据正确。

servlet 中没有捕获任何异常或错误。

java ajax jsp servlets ckeditor
1个回答
0
投票

我修复了它,我如何使用 getData() 方法是正确的,我必须通过 AJAX 正确发送所有数据,仅此而已,并将它们获取到 servlet 中。

© www.soinside.com 2019 - 2024. All rights reserved.