我正在使用Spring MVC项目spring无法转换Post对象所需的用户对象。
我的NewPost对象:
public class NewPost {
private int postId;
@NotEmpty(message ="Title must not be empty!")
@Length(max = 50, message="Title must be less then 50 character")
private String title;
@NotEmpty(message ="Comment must not be empty!")
private String content;
@Length(max = 100, message="must be less then 100 character")
private String imagePath;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime publishDate;
private User user;
我的控制员:
//Get lates post from DB.
@RequestMapping(value = "/displayBlogPage", method = RequestMethod.GET)
public String displayLatesPost(Model model) {
List<NewPost> displayAllPost = new ArrayList<>();
displayAllPost = NPDao.getAllPost();
model.addAttribute("displayAllPost", displayAllPost);
//get newPost to the model.
model.addAttribute("newPost", new NewPost());
//get user object to the model
model.addAttribute("user", userDao.getUserbyId(1));
LocalDateTime timeStamp = LocalDateTime.now();
model.addAttribute("timeStamp", timeStamp);
return "NewPostPage";
}
//for add Post form
@RequestMapping(value = "/newPost", method = RequestMethod.POST)
public String createPost( @Valid @ModelAttribute("newPost") NewPost newPost, BindingResult result) {
if(result.hasErrors()){
return "NewPostPage";
}
NPDao.addNewPost(newPost);
return "redirect:NewPostPage";
}
我的JSP
<sf:form class="form-horizontal"
role="form" method="POST"
action="newPost" modelAttribute="newPost" >
<div class="col-md-12">
<div class="form-group">
<div class="col-md-6">
<sf:input type="text" class="form-control" path="title" placeholder="title" />
<sf:errors path="title" cssclass="error" ></sf:errors>
</div>
</div>
<div class="form-group">
<div class="col-md-6">
<sf:input type="text" class="form-control" path="imagePath" placeholder="image" />
<sf:errors path="imagePath" cssclass="error" ></sf:errors>
<sf:input type="text" class="form-control" path="publishDate" placeholder="date" value="${timeStamp}" />
<sf:errors type="date" path="publishDate" cssclass="error" ></sf:errors>
<sf:input type="text" class="form-control" path="user" placeholder="user" value="${user}"/>
<sf:errors path="user" cssclass="error" ></sf:errors>
<sf:input type="text" class="form-control" path="postId" placeholder="postid"/>
<sf:errors path="postId" cssclass="error" ></sf:errors>
<div class="form-group">
<div class="col-md-12">
<textarea type="text" class="form-control comment" name="comment" placeholder="Comment" required ></textarea>
<input type="submit" id="add"class="btn btn-default" value="Submit Post"/>
</div>
</div>
</div>
</sf:form>
这是例外:
无法将类型为java.lang.String的属性值转换为属性用户所需的com.sg.sophacms.Model.User类型;嵌套异常是java.lang.IllegalStateException:无法将类型为java.lang.String的值转换为属性user的所需类型com.sg.sophacms.Model.User:找不到匹配的编辑器或转换策略
答案 0 :(得分:0)
在您的控制器覆盖initBinder
(或使用@InitBinder
注释任何方法)并为类PropertyEditor
和字段名称“User
”提供user
(虽然它可以用于此类的任何字段)getAsText/setAsText
:
@Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { // or @InitBinde
binder.registerCustomEditor(User.class, "user", new PropertyEditorSupport() {
@Override
public void setAsText(String text) {
User user = new User(text); // Some kind of initialization of your object from string
setValue(user);
}
@Override
public String getAsText() {
User user = getValue();
return user.getId(); // Some kind of string from your User object
}
});
}
这个想法是你需要指定逻辑来翻译Object&lt; - &gt; String。在上面的示例中,我假设您可以从字符串构造User(str)
,并且User.getId()
将返回一个字符串。这是将对象绑定到文本字段所需的Object&lt; - &gt;字符串转换策略的示例。