Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Person {
- private String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- public class PersonPropertyEditor extends PropertyEditorSupport {
- @Override
- public void setAsText(String text) throws IllegalArgumentException {
- Person person = new Person();
- person.setName(text);
- setValue(person);
- }
- }
- @InitBinder
- public void initBinder(WebDataBinder webDataBinder) {
- webDataBinder.registerCustomEditor(Person.class,
- new PersonPropertyEditor());
- }
- @RequestMapping(value = "/addPerson", method = RequestMethod.POST)
- public String homePost(
- @ModelAttribute("personConverted") Person personConverted) {
- System.out.println(personConverted.getName());
- return "redirect:/home";
- }
- <form action="addPerson" method="post">
- <input type="text" name="personConverted" />
- <input type="submit" value="Submit" />
- <form>
- @JsonDeserialize(using=DateTimeDeserializer.class)
- public DateTime getPublishedUntil() {
- return publishedUntil;
- }
- public class DateTimeDeserializer extends StdDeserializer<DateTime> {
- private DateTimeFormatter formatter = DateTimeFormat.forPattern(Constants.DATE_TIME_FORMAT);
- public DateTimeDeserializer(){
- super(DateTime.class);
- }
- @Override
- public DateTime deserialize(JsonParser json, DeserializationContext context) throws IOException, JsonProcessingException {
- try {
- if(StringUtils.isBlank(json.getText())){
- return null;
- }
- return formatter.parseDateTime(json.getText());
- } catch (ParseException e) {
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement