@Test public void test4() throws Exception{ String name="aaa"; String password="123"; String age="24"; String birthday="1980-09-09"; //ʹ��apache���Ѿ�ʵ��Convert�ӿڵ�ת������DateLocaleConverterʵ�ֽ�ǰ�˷��͵��������͵��ַ�������ת��Ϊbean�������Ե������������� ConvertUtils.register(new DateLocaleConverter(), Date.class); Person bean=new Person(); BeanUtils.setProperty(bean, "name", name);//��ʾ�����ĸ�bean���ĸ����ԣ���Ϊ������Ը�ֵΪvalue BeanUtils.setProperty(bean, "password", password); BeanUtils.setProperty(bean, "age", age);//ֻ֧���ַ�����8�ֻ������������Զ�ת�� BeanUtils.setProperty(bean, "birthday", birthday); System.out.println(bean.getName());//aaa System.out.println(bean.getPassword());//123 System.out.println(bean.getAge());//24�����Կ���ǰ̨���ݹ������ݶ������ַ������͵ģ�����bean����age������int�ͣ�Ҳ����beanUtils���֧�ֽ�ǰ̨���ַ��������Զ�ת��Ϊ���ֻ����������ݣ����Ǹ������ͣ��Ͳ������ˣ���Ҫ����Ϊ��������ע��һ��ת��������beanUtils�����ת����ȥ��ǰ̨string����ת��Ϊbean����ĸ����������ԣ����������� System.out.println(bean.getBirthday().toLocaleString());//1980-9-9 0:00:00 }
@Test public void test5() throws Exception{ Map map=new HashMap(); map.put("name", "aaa"); map.put("password", "123"); map.put("age", "23"); map.put("birthday", "1980-09-09"); //ʹ��apache���Ѿ�ʵ��Convert�ӿڵ�ת������DateLocaleConverterʵ�ֽ�ǰ�˷��͵��������͵��ַ�������ת��Ϊbean�������Ե������������� ConvertUtils.register(new DateLocaleConverter(), Date.class); Person bean =new Person(); BeanUtils.populate(bean, map);//ʹ��map���ϵ�key-valueֵ���bean��������ԣ��ڲ��ǽ�map�ؼ���keyΪname������valueֵ��䵽bean�����Ӧ��name����ֵ�ϣ���map�Ĺؼ������Ʊ�����bean���Ե�����������һ�� System.out.println(bean.getName());//aaa System.out.println(bean.getPassword());//123 System.out.println(bean.getAge());//24�����Կ���ǰ̨���ݹ������ݶ������ַ������͵ģ�����bean����age������int�ͣ�Ҳ����beanUtils���֧�ֽ�ǰ̨���ַ��������Զ�ת��Ϊ���ֻ����������ݣ����Ǹ������ͣ��Ͳ������ˣ���Ҫ����Ϊ��������ע��һ��ת��������beanUtils�����ת����ȥ��ǰ̨string����ת��Ϊbean����ĸ����������ԣ����������� System.out.println(bean.getBirthday().toLocaleString());//1980-9-9 0:00:00 }
/** * Test copy properties all. */ @Test public void testCopyPropertiesAll(){ BigDecimal money = new BigDecimal(500000); String[] nickNames = toArray("feilong", "飞天奔月", "venusdrogon"); Date date = new Date(); User user = new User(); user.setId(5L); user.setMoney(money); user.setDate(date); user.setNickNames(nickNames); DateLocaleConverter dateLocaleConverter = new DateLocaleConverter(Locale.US, TO_STRING_STYLE); ConvertUtils.register(dateLocaleConverter, Date.class); User user2 = new User(); BeanUtil.copyProperties(user2, user); //--------------------------------------------------------------- assertThat( user2, allOf(// hasProperty("money", is(money)), hasProperty("nickNames", is(nickNames)), //hasProperty("nickNames", is(toArray("feilong"))), // not(hasProperty("date", equalTo(date)))// hasProperty("date", equalTo(date))// )); }
private void addCustomer(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { CustomerFormBean cfb = WebUtil.fillBean(req, CustomerFormBean.class); if (!cfb.validation()) { req.setAttribute("formBean", cfb); req.getRequestDispatcher("/editCustomer.jsp").forward(req, res); return; } ConvertUtils.register(new DateLocaleConverter(Locale.CHINA), Date.class); Customer c = new Customer(); try { BeanUtils.copyProperties(c, cfb); } catch (Exception e) { throw new RuntimeException(e); } String[] hobbies = req.getParameterValues("hobbies"); if (hobbies != null && hobbies.length > 0) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < hobbies.length; i++) { if (i > 0) { sb.append(","); } sb.append(hobbies[i]); } c.setHobby(sb.toString()); } s.addCustomer(c); res.sendRedirect(req.getContextPath() + "/"); }
public MockHttpServletRequestBuilder addFormParameters(MockHttpServletRequestBuilder builder, Object object) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { if (object != null) { DateLocaleConverter dateLocaleConverter = new DateLocaleConverter(); ConvertUtils.register(dateLocaleConverter, Date.class); SimpleDateFormat dateFormat = new SimpleDateFormat(applicationSettings.getApplicationDateFormat()); Map<String, ?> propertyValues = TestUtil.getPropertyValues(object, dateFormat); for (Entry<String, ?> entry : propertyValues.entrySet()) { builder.param(entry.getKey(), Util.prepareDisplayValue(entry.getValue(), applicationSettings.getApplicationDateFormat())); } } return builder; }
public String update() { UserForm userForm=WebUtil.params2FormBean(getParameters(), UserForm.class); RepAdmin admin=new RepAdmin(); ConvertUtils.register(new DateLocaleConverter(), Date.class); try { BeanUtils.copyProperties(admin, userForm); } catch (Exception e) { e.printStackTrace(); } admin.setUsername(admin.getUsername().trim()); RepAdmin a=adminService.findAdminByName(admin.getUsername()); if(a!=null&&!a.getId().equals(admin.getId())){ getContextMap().put("message", "已经存在同名用户,请换个用户名!"); getContextMap().put("params", getParameters()); return "edit"; } if(admin.getPassword()!=null&&!admin.getPassword().isEmpty()){ admin.setPassword(MD5Util.MD5(admin.getPassword())); }else { admin.setPassword(null); } int roleid=Integer.parseInt(((String[])getParameters().get("roleid"))[0]); //判断更新的管理员是否是最后的一个未锁定的超级管理员,如果是的话则不能删除 RepRole role=roleService.get(roleid); RepRole originalRole=adminService.get(admin.getId()).getRepRole(); //判断是否符合最后一个超级管理员的条件 if(originalRole!=null&&originalRole.getName().equals("超级管理员") //如果是最后一个超级管理员,则判断是否进行了修改角色或锁定操作 &&(!role.getName().equals("超级管理员")||admin.getIslock()==1)){ int size=adminService.countUnLockSuperAdmins(); if(size<=1){ getContextMap().put("message", "不能对最后一个超级管理员进行锁定或者修改角色的操作!"); getContextMap().put("returnUrl", getRequest().getContextPath()+"/admin/user/admin_list.do"); return "faild"; } } admin.setRepRole(role); adminService.updateNotNullField(admin); getContextMap().put("message", "修改成功"); getContextMap().put("returnUrl", "admin_list.do"); return SUCCESS; }