Spring Boot with CKEditor and Ajax

0

My problem seems to be simple but I can not make it work as I would like. Since I want to register the value of a rich text. The form only has a rich text using CKEditor and a button to register.

<!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Demo</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://cdn.ckeditor.com/4.10.0/standard/ckeditor.js"></script>
    </head>
    <body>
    <div class="container">
        <form>
            <h2>Register demo</h2>
            <div class="form-group row">
                <textarea name="editor1"></textarea>
            </div>
            <div class="form-group row">
                <button class="btn btn-danger" onclick="registrar()">Agregar</button>
            </div>

            <script>
                function registrar(){
                    var data = CKEDITOR.instances.editor1.getData();
                    $.ajax({
                        type : "GET",
                        url : "/apolo/registrar",
                        contentType: 'application/json',
                        data: {
                            "data" : data
                        },
                        success: function(result) {
                            alert(result);
                        },
                        error : function(e) {
                            alert("error");
                        }
                    });
                }
            </script>
            <script>
                CKEDITOR.replace( 'editor1' );
            </script>
        </form>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </body>
    </html>

When registering I want to redirect me to another view where the list of registered post is.

<!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Demo</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://cdn.ckeditor.com/4.10.0/standard/ckeditor.js"></script>
    </head>
    <body>
    <div class="container">
        <h1>Demo</h1>
        <div th:each="test : ${tests}">
            <span th:utext="${test.texto}"></span>
        </div>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </body>
    </html>

In my controller I do return to the view, but it does not work, the result that goes to ajax shows me an html and the url has the registered data.

import com.example.richtext.demo.entity.Test;
import com.example.richtext.demo.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;

@Controller
public class TestController {

    @Autowired
    private TestService testService;

    @GetMapping(value = {"/index", "/"})
    public String index(Model model){
        Test test = new Test();
        model.addAttribute("test", test);
        return "index";
    }

    @RequestMapping(value = "/registrar", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public String registrar(@RequestParam("data") String data){
        System.out.println("entro registrar");
        System.out.println("data: "+data);
        Test test = new Test();
        test.setTexto(data);
        testService.registrar(test);
        return "list";
    }

    @GetMapping(value = "/list")
    public String list(Model model){
        List<Test> tests = (List<Test>) testService.listTest();
        model.addAttribute("tests", tests);
        return "list";
    }
}

What I see that works is that if you register and when I enter the list by url I also get the post.

    
asked by Santiago 05.09.2018 в 10:45
source

1 answer

0

Let's see, I think you get confused.

Spring receives a request, and returns a web page. It works correctly.

The "normal" operation of the browser is to make a request to the server, receive an HTML response and display it.

Ajax is thinking to make requests without redirection . The answer is not shown automatically; it is passed to javascript so that the javascript decides what to do according to its logic.

In this case, the server sends you the new web page; the browser passes it to handler success and it shows it in alert .

If you want to navigate to a new page when sending the form, do not use Ajax, make a% normal% of the current rate.

In a separate note, the GET requests:

  • They are supposed to be used for operations that do not change data (idempotent). If you're going to save the text in BD, GET is not the method you touch.

  • They have restrictions on the size of the parameters because the parameters pass as part of the URL, and there are limits - which depend on the server - on how large the URLs can be. A free text should not go by GET.

answered by 05.09.2018 в 11:55