Example usage for java.beans PropertyEditorSupport PropertyEditorSupport

List of usage examples for java.beans PropertyEditorSupport PropertyEditorSupport

Introduction

In this page you can find the example usage for java.beans PropertyEditorSupport PropertyEditorSupport.

Prototype

public PropertyEditorSupport() 

Source Link

Document

Constructs a PropertyEditorSupport object.

Usage

From source file:com.kingen.web.CommonController.java

/**
 * ??//from   w  ww  .  j a v  a  2s.c  o  m
 * 1. ?StringHTML?XSS
 * 2. Date?String
 */
@InitBinder
protected void initBinder(WebDataBinder binder) {
    // String??StringHTML?XSS
    binder.registerCustomEditor(String.class, new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) {
            setValue(text == null ? null : text.trim());
            //            setValue(text == null ? null : StringEscapeUtils.escapeHtml4(text.trim()));
            //            setValue(text == null ? null : StringEscapeUtils.escapeJavaScript(text.trim()));
        }

        @Override
        public String getAsText() {
            Object value = getValue();
            return value != null ? value.toString() : "";
        }
    });
    // Date ?
    binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) {

            setValue(DateUtils.parseDate(StringUtils.trim(text)));
            logger.debug("---" + getValue());
        }
    });
}

From source file:ru.org.linux.search.SearchController.java

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(SearchOrder.class, new PropertyEditorSupport() {
        @Override//from   www.  j  a v  a 2 s.c  om
        public void setAsText(String s) throws IllegalArgumentException {
            if ("1".equals(s)) { // for old links
                setValue(SearchOrder.RELEVANCE);
            } else if ("2".equals(s)) {
                setValue(SearchOrder.DATE);
            } else {
                setValue(SearchOrder.valueOf(s));
            }
        }
    });

    binder.registerCustomEditor(SearchInterval.class, new PropertyEditorSupport() {
        @Override
        public void setAsText(String s) throws IllegalArgumentException {
            setValue(SearchInterval.valueOf(s.toUpperCase()));
        }
    });

    binder.registerCustomEditor(SearchRange.class, new PropertyEditorSupport() {
        @Override
        public void setAsText(String s) throws IllegalArgumentException {
            setValue(SearchRange.valueOf(s.toUpperCase()));
        }
    });

    binder.registerCustomEditor(User.class, new UserPropertyEditor(userDao));

    binder.setBindingErrorProcessor(new ExceptionBindingErrorProcessor());
}

From source file:com.askme.controller.app.AppController.java

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(Category.class, "category", new PropertyEditorSupport() {
        @Override/*from  w w w.  jav  a  2  s  .c  om*/
        public void setAsText(String text) {
            Category category = categoryService.findById(Integer.parseInt(text));
            setValue(category);
        }
    });

    binder.registerCustomEditor(Set.class, "tags", new CustomCollectionEditor(Set.class) {
        @Override
        protected Object convertElement(Object element) {
            Integer id = Integer.parseInt((String) element);
            return tagService.findById(id);
        }
    });
}

From source file:com.oak_yoga_studio.controller.AdminController.java

/**
 * /*ww  w .j  a  v a2s  . c o  m*/
 * @param binder 
 */

@InitBinder
protected void initBinder(WebDataBinder binder) {

    //      binder.registerCustomEditor(Integer.class, "totalSeat",
    //            new PropertyEditorSupport() {
    //
    //               @Override
    //               public void setAsText(String text) {
    //                  Integer totalSeat= Integer.parseInt(text);
    //                  setValue(totalSeat);
    //               }
    //            });
    //      
    //      binder.registerCustomEditor(Long.class, "id",
    //            new PropertyEditorSupport() {
    //
    //               @Override
    //               public void setAsText(String text) {
    //                  Long id= Long.parseLong(text);
    //                  setValue(id);
    //               }
    //            });
    //
    //      

    binder.registerCustomEditor(List.class, "prerequisites", new CustomCollectionEditor(List.class) {
        @Override
        protected Object convertElement(Object element) {
            Integer id = null;
            if (element instanceof String && !((String) element).equals("")) {
                // From the JSP 'element' will be a String
                try {
                    id = Integer.parseInt((String) element);
                } catch (NumberFormatException e) {
                    System.out.println("Element was " + ((String) element));
                    e.printStackTrace();
                }
            } else if (element instanceof Integer) {
                // From the database 'element' will be a int
                id = (Integer) element;
            }
            return id != null ? courseService.getCourseById(id) : null;

        }
    });

    binder.registerCustomEditor(Faculty.class, "professor", new PropertyEditorSupport() {

        @Override
        public void setAsText(String facultyID) {
            Faculty faculty = facultyServcie.getFacultyById(Integer.parseInt(facultyID));

            setValue(faculty);
        }
    });

    binder.registerCustomEditor(Course.class, "course", new PropertyEditorSupport() {

        @Override
        public void setAsText(String courseID) {
            Course course = courseService.getCourseById(Integer.parseInt(courseID));
            setValue(course);
        }
    });
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testConvertClassToString() {
    MutablePropertyValues values = new MutablePropertyValues();
    values.add("name", Integer.class);
    TestBean tb = new TestBean();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(tb);
    bw.registerCustomEditor(String.class, new PropertyEditorSupport() {
        @Override/*  ww  w .  j  a  v a 2s.c o  m*/
        public void setValue(Object value) {
            super.setValue(value.toString());
        }
    });
    bw.setPropertyValues(values);
    assertEquals(Integer.class.toString(), tb.getName());
}

From source file:edu.byu.softwareDistribution.web.controller.shopper.ShoppingCartController.java

@InitBinder
public final void addDateEditor(ServletRequestDataBinder binder) {
    binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
        @Override/*from w  ww  .ja  v  a2s  .com*/
        public void setAsText(String s) throws IllegalArgumentException {
            if (s == null) {
                return;
            }
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            try {
                setValue(format.parse(s));
            } catch (ParseException e) {
                LOG.debug("Invalid Date.");
            }
        }
    });

}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testStringArrayPropertyWithCustomStringEditor() throws Exception {
    PropsTester pt = new PropsTester();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(pt);
    bw.registerCustomEditor(String.class, "stringArray", new PropertyEditorSupport() {
        @Override// w  w w .  ja v a2s  . c  o m
        public void setAsText(String text) {
            setValue(text.substring(1));
        }
    });

    bw.setPropertyValue("stringArray", new String[] { "4foo", "7fi", "6fi", "5fum" });
    assertTrue("stringArray length = 4", pt.stringArray.length == 4);
    assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi")
            && pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum"));

    List<String> list = new ArrayList<String>();
    list.add("4foo");
    list.add("7fi");
    list.add("6fi");
    list.add("5fum");
    bw.setPropertyValue("stringArray", list);
    assertTrue("stringArray length = 4", pt.stringArray.length == 4);
    assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi")
            && pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum"));

    Set<String> set = new HashSet<String>();
    set.add("4foo");
    set.add("7fi");
    set.add("6fum");
    bw.setPropertyValue("stringArray", set);
    assertTrue("stringArray length = 3", pt.stringArray.length == 3);
    List<String> result = Arrays.asList(pt.stringArray);
    assertTrue("correct values", result.contains("foo") && result.contains("fi") && result.contains("fum"));

    bw.setPropertyValue("stringArray", "8one");
    assertTrue("stringArray length = 1", pt.stringArray.length == 1);
    assertTrue("correct values", pt.stringArray[0].equals("one"));
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testStringPropertyWithCustomEditor() throws Exception {
    TestBean tb = new TestBean();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(tb);
    bw.registerCustomEditor(String.class, "name", new PropertyEditorSupport() {
        @Override/* ww w. j  a  v  a 2s.  co  m*/
        public void setValue(Object value) {
            if (value instanceof String[]) {
                setValue(StringUtils.arrayToDelimitedString(((String[]) value), "-"));
            } else {
                super.setValue(value != null ? value : "");
            }
        }
    });
    bw.setPropertyValue("name", new String[] {});
    assertEquals("", tb.getName());
    bw.setPropertyValue("name", new String[] { "a1", "b2" });
    assertEquals("a1-b2", tb.getName());
    bw.setPropertyValue("name", null);
    assertEquals("", tb.getName());
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testIntArrayPropertyWithCustomEditor() {
    PropsTester pt = new PropsTester();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(pt);
    bw.registerCustomEditor(int.class, new PropertyEditorSupport() {
        @Override//from w  w w.  j a v a 2s  .c o m
        public void setAsText(String text) {
            setValue(new Integer(Integer.parseInt(text) + 1));
        }
    });

    bw.setPropertyValue("intArray", new int[] { 4, 5, 2, 3 });
    assertTrue("intArray length = 4", pt.intArray.length == 4);
    assertTrue("correct values",
            pt.intArray[0] == 4 && pt.intArray[1] == 5 && pt.intArray[2] == 2 && pt.intArray[3] == 3);

    bw.setPropertyValue("intArray", new String[] { "3", "4", "1", "2" });
    assertTrue("intArray length = 4", pt.intArray.length == 4);
    assertTrue("correct values",
            pt.intArray[0] == 4 && pt.intArray[1] == 5 && pt.intArray[2] == 2 && pt.intArray[3] == 3);

    bw.setPropertyValue("intArray", new Integer(1));
    assertTrue("intArray length = 4", pt.intArray.length == 1);
    assertTrue("correct values", pt.intArray[0] == 1);

    bw.setPropertyValue("intArray", new String[] { "0" });
    assertTrue("intArray length = 4", pt.intArray.length == 1);
    assertTrue("correct values", pt.intArray[0] == 1);

    bw.setPropertyValue("intArray", "0");
    assertTrue("intArray length = 4", pt.intArray.length == 1);
    assertTrue("correct values", pt.intArray[0] == 1);
}