2017-02-13 99 views
0

我想在Spring启动应用程序中使用类型转换器并使用Thymeleaf,但我无法使它工作。我在Github上放了一些代码,这样你就可以看到我正在尝试做什么。这是Spring 1.5.1和Thymeleaf 3.0.3。 https://github.com/matthewsommer/spring-thymeleaf-simple-converterSpring + Thymeleaf类型转换器的形式

基本上这段代码只是试图添加一个人到评论对象。 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 
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; 
    } 
    } 
+0

您是否试过该代码? – cralfaro

回答

0

您好我不得不做一些改变,使其工作,但日是按班分类的结果。

ConvertorApplication:

@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; 
} 

HomeWebController

@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> 

这将是一切使它工作。

+0

现在有没有办法在控制器中设置人评论的方式?如果输入是在表单对象中,我觉得comment.person可以直接从html上的输入设置。 – yerself

+0

也许你可以在GET方法中接收userId,然后将User设置为新评论,并使其更通用,但我认为设置用户的更简洁的方式是使用控制器方法 – cralfaro