2017-02-13 9 views
0

私はSpringブートアプリケーションでタイプコンバータを使用しようとしていますが、それを動作させることはできません。 Githubにいくつかのコードを書いて、私がしようとしていることを正確に見ることができます。これはSpring 1.5.1とThymeleaf 3.0.3です。 https://github.com/matthewsommer/spring-thymeleaf-simple-converterSpring + Thymeleafタイプのコンバータ

基本的にこのコードは、コメントオブジェクトに人を追加しようとしています。人物オブジェクトは投稿されたときにnullであり、理由を理解できません。

奇妙なことは、人のIDが値属性に追加されていないが、th:field = "* {body}"が削除されていることです。私のHTMLがある

... https://github.com/thymeleaf/thymeleaf/issues/495しかし、私は現在BindingResultを追加しようとしているし、それが働いていない:

<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

最後に私はそれを動作させるためにいくつかの変更を加えなければならなかったが、 isはクラスごとの結果クラスです。

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をですが、すべてのものになり

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

はそれを動作させるために(基本的には、personObjectを取り外す必要はありません)。

+0

これで、コントローラでコントローラを実行せずに、コメントする人をSpringに設定させる方法はありますか?入力がフォームオブジェクト内にある場合、htmlの入力から直接comment.personを設定するのがいいと思う。 – yerself

+0

GETメソッドでuserIdを受け取った後、新しいコメントにUserを設定してより一般的にすることができますが、ユーザーを設定するクリーナーの方法はコントローラメソッドにあると思います – cralfaro

関連する問題