Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. public class Person {
  2. private String name;
  3.  
  4. public String getName() {
  5. return name;
  6. }
  7.  
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. }
  12.  
  13. public class PersonPropertyEditor extends PropertyEditorSupport {
  14. @Override
  15. public void setAsText(String text) throws IllegalArgumentException {
  16. Person person = new Person();
  17. person.setName(text);
  18.  
  19. setValue(person);
  20. }
  21. }
  22.  
  23. @InitBinder
  24. public void initBinder(WebDataBinder webDataBinder) {
  25. webDataBinder.registerCustomEditor(Person.class,
  26. new PersonPropertyEditor());
  27. }
  28.  
  29. @RequestMapping(value = "/addPerson", method = RequestMethod.POST)
  30. public String homePost(
  31. @ModelAttribute("personConverted") Person personConverted) {
  32. System.out.println(personConverted.getName());
  33. return "redirect:/home";
  34. }
  35.  
  36. <form action="addPerson" method="post">
  37. <input type="text" name="personConverted" />
  38. <input type="submit" value="Submit" />
  39. <form>
  40.  
  41. @JsonDeserialize(using=DateTimeDeserializer.class)
  42. public DateTime getPublishedUntil() {
  43. return publishedUntil;
  44. }
  45.  
  46. public class DateTimeDeserializer extends StdDeserializer<DateTime> {
  47.  
  48. private DateTimeFormatter formatter = DateTimeFormat.forPattern(Constants.DATE_TIME_FORMAT);
  49.  
  50. public DateTimeDeserializer(){
  51. super(DateTime.class);
  52. }
  53.  
  54. @Override
  55. public DateTime deserialize(JsonParser json, DeserializationContext context) throws IOException, JsonProcessingException {
  56. try {
  57. if(StringUtils.isBlank(json.getText())){
  58. return null;
  59. }
  60. return formatter.parseDateTime(json.getText());
  61. } catch (ParseException e) {
  62. return null;
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement