我正在使用 spring 在 Java 中开发一个 Web 应用程序。
该应用程序包括 JavaScript 中的 Ajax 调用,该调用请求 html 代码,然后将其插入到 html 文档中。
为了将 thymeleaf 模板处理为字符串,我使用 TemplateEngine process(..) 方法。
当 thymeleaf 模板包含表单时,我遇到错误。
我的示例代码:
表单.html:
<form th:object="${customer}" xmlns:th="http://www.w3.org/1999/xhtml">
<label>Name</label>
<input type="text" th:field="*{name}" />
</form>
Run Code Online (Sandbox Code Playgroud)
AjaxController.java:
package project;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
@Controller
public class AjaxController {
@Autowired
private TemplateEngine templateEngine;
private ObjectMapper objectMapper = new ObjectMapper();
@ResponseBody
@GetMapping(value="/form1")
public String form1() throws JsonProcessingException { …Run Code Online (Sandbox Code Playgroud)