I like to show an object in its edit form which have been selected before from another page. The selection page transmits the id of the selected object via GET to my controller.
How can I enforce that the parameters get bind to a message object and this then automatically initialized by using my property editor?
At the moment I get always a new object with the id property set but not initialized through my property editor. What is missing in my GET request?
Example Selection JSP page, which will transmit the id via GET request to my controller:
<a href="message?id=${message.id}">${message.title}</a>
My Controller with PropertyEditor class and InitBind method
@Controller
public class MessageController {
@Autowired
private MessageRepository messageRepository;
@RequestMapping(value="/message", method = RequestMethod.GET)
public String handleMessage(Model model,@ModelAttribute("message") Message message) {
// ISSUE Here the message object has only the "id" property set but get not initialized through the binder
System.out.println(message);
return "message";
}
// inline property editor for message class
public class MessagePropertyEditor extends PropertyEditorSupport {
@Override
public String getAsText() {
return String.valueOf(((Message) getValue()).getId());
}
@Override
public void setAsText(String id) throws IllegalArgumentException {
Message message = messageRepository.getMessageById(Integer.valueOf(id));
setValue(message);
}
}
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Message.class, new MessagePropertyEditor());
}
}
Example Message bean class
public class Message {
private int id;
private String title;
private String text;
// getter & setter methods
}
@ModelAttribute!=@RequestParam.