Spring + Thymeleaf 形式的转换器

标签 spring spring-mvc thymeleaf

我正在尝试在 Spring 引导应用程序中使用类型转换器并使用 Thymeleaf,但我无法使其正常工作。我在 Github 上放了一些代码,这样你就可以准确地看到我正在尝试做什么。这是 Spring 1.5.1 和 Thymeleaf 3.0.3。 https://github.com/matthewsommer/spring-thymeleaf-simple-converter

基本上这段代码只是试图将一个人添加到评论对象中。 person 对象在发布时为 null,我不明白为什么。

奇怪的是,人的 ID 没有被添加到 value 属性中,但是如果 th:field="*{body}"被删除了。我认为这与此有关:https://github.com/thymeleaf/thymeleaf/issues/495但我目前正在尝试添加 BindingResult 但它不起作用...

我的 HTML 是:

<body>
<div th:if="${personObject != null}" th:text="${personObject.name}"></div>
<form th:action="@{/}" th:object="${comment}" method="post">
    <input type="hidden" th:if="${personObject != null}" th:value="${personObject.id}" th:field="*{person}" />
    <textarea id="comment" placeholder="Comment..." th:field="*{body}"></textarea>
    <button id="comment_submit" type="submit">Comment</button>
</form>
<div th:text="${comment.body}"></div>
</body>

我的 Controller :

@Controller
public class HomeWebController {

  @RequestMapping(value = "/", method = RequestMethod.GET)
  public String getHome(final HttpServletRequest request, final Map<String, Object> model, @ModelAttribute(value = "comment") Comment comment) {
model.put("personObject", new Person(1, "John Smith"));
return "Home";
  }

  @RequestMapping(value = "/", method = RequestMethod.POST)
  public String postHome(final HttpServletRequest request, final Map<String, Object> model, @ModelAttribute(value = "comment") Comment comment) {
model.put("commentBody", comment.getBody());
model.put("person", comment.getPerson());
return "Home";
  }

}

和转换器:

@Component
public class StringToPersonConverter implements Converter<String, Person> {

  @Autowired
  public StringToPersonConverter() { }

  @Override
  public Person convert(String id) {
if(id == "1") {
  Person person = new Person(1, "John Smith");
  return person;
}
return null;
  }
    }

最佳答案

您好,最后我必须做一些更改才能使其正常工作,但这是逐个类的结果。

转换器应用:

@SpringBootApplication
@Configuration
@EnableWebMvc
public class ConvertorApplication extends WebMvcConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication.run(ConvertorApplication.class, args);
    }

    //Add converter and configuration annotation
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new StringToPersonConverter());
    }
}

StringToPersonConverter:

@Override
public Person convert(String id) {
    //Never compare String with == use equals, the "==" compares memory space not the values 
    if(id.equals("1")) {
      Person person = new Person(1, "John Smith");
      return person;
    }
    return null;
}

主页网络 Controller

@Controller
public class HomeWebController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String getHome(final Map<String, Object> model, @ModelAttribute(value = "comment") Comment comment) {
        //Initialize the comment with the person inside, no need of personObject object
        model.put("comment", new Comment(new Person(1, "John Smith")));
        return "Home";
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public String postHome(final Map<String, Object> model,
                           @ModelAttribute(value = "comment") Comment comment,
                           @RequestParam(value = "person.id") Person person) {
        //from the view retrieve the value person.id which will be used by the converter to build the Person entity
        comment.setPerson(person);
        model.put("comment", comment);
        return "Home";
    }
}

注释(添加空构造函数)

public Comment(){}

人(添加空构造函数)

public Person(){}

Home.jsp(基本去掉personObject,不需要)

<!DOCTYPE html>
<html xmlns:th="//www.thymeleaf.org">
<body>
    <div th:text="${comment.person.name}"></div>
    <form th:action="@{/}" th:object="${comment}" method="post">
        <input type="hidden"  th:field="*{person.id}" />
        <textarea id="comment" placeholder="Comment..." th:field="*{body}"></textarea>
        <button id="comment_submit" type="submit">Comment</button>
    </form>
    <div th:text="${comment.body}"></div>
</body>
</html>

这就是让它发挥作用的一切。

关于Spring + Thymeleaf 形式的转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42215166/

相关文章:

spring - 无法解决Grails自定义验证程序问题

java - 什么是分组通量以及我们如何使用分组通量?

java - 使用 BindingResult 在 JSP 上显示错误消息

java - Bluemix cloud foundry java 应用程序异常 : OutputStream already closed

regex - 检查字符串是否包含数字或者是数字 - Thymeleaf

javascript - 将 JSON 对象放入带有 th 标签的 thymeleaf 中

java - 有没有办法动态地从不同的类获取所有静态字段及其值?

java - Maven 和 spring-boot : how to specify profile(s) on spring-boot:repackage

java - Spring Autowiring byType 与 util :list

java - BindingResult 和 bean 名称 'matrix[0][0]' 的普通目标对象都不能作为请求属性