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.