1

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
}
1
  • @ModelAttribute != @RequestParam. Commented Apr 30, 2017 at 16:48

2 Answers 2

1

Instead of using a PropertyEditor I suggest using a @ModelAttribute annotated method next to your @RequestMapping method.

@ModelAttribute
public Message modelAttribute(@RequestParam("id") int id) {
    return messageRepository.getMessageById(id);
}

Leave your @RequestMapping as is and you can remove your MessagePropertyEditor and @InitBinder annotated method. Which would result in something like this.

@Controller
@RequestMapping("/message")
public class MessageController {

  @Autowired
  private MessageRepository messageRepository;

  @ModelAttribute
  public Message modelAttribute(@RequestParam("id") int id) {
      return messageRepository.getMessageById(id);
  }

  @GetMapping
  public String handleMessage(Model model,@ModelAttribute("message") Message message) {
    System.out.println(message);

    return "message";
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Add @RequestParam("id") to the paramter message like this:

public String handleMessage(Model model,@RequestParam("id") @ModelAttribute("message") Message message) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.