private void copy(final Directory directory, final Map<String, Object> metadata) { final TagDescriptor<?> descriptor = MetadataHelper.getDescriptor(directory); final LazyDynaClass directoryClass = new LazyDynaClass(directory.getName(), null, directory.getTags() .parallelStream() .map(t -> new DynaProperty(t.getTagName().replaceAll(NON_WORD_CHARS, ""), String.class)) .toArray(DynaProperty[]::new)); directoryClass.setReturnNull(true); final DynaBean directoryMetadata = new LazyDynaBean(directoryClass); directory.getTags().stream().forEach(t -> directoryMetadata.set( t.getTagName().replaceAll(NON_WORD_CHARS, ""), descriptor.getDescription(t.getTagType()) )); metadata.put(directory.getName().replaceAll(NON_WORD_CHARS, ""), directoryMetadata); }
@Override protected void setUp() throws Exception { target = new LazyDynaBean(); final Fixture decorated = context.mock(Fixture.class); context.checking(new Expectations() {{ between(0, 2).of(decorated).canCheck("property"); will(returnValue(false)); between(0, 2).of(decorated).canSend("property"); will(returnValue(false)); between(0, 2).of(decorated).getTarget(); will(returnValue(target)); }}); fixture = new DynaBeanFixture(decorated); }
/** * Velocity 模板发送邮件 html 格式 * * @param to * @param subject * @throws javax.mail.MessagingException */ public void sendMailVelocity(String from ,String[] to, String subject) throws MessagingException { //如果不是 html 格式,修改为 SimpleMailMessage MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true, StandardCharsets.UTF_8.toString()); /** *邮件内容 */ helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); //模板内容 Map<String, Object> model = new HashMap<String, Object>(); model.put("firstName", "Yashwant"); model.put("lastName", "Chavan"); model.put("location", "china"); //创建动态 bean DynaBean dynaBean = new LazyDynaBean(); dynaBean.set("name", "It is name"); //simple dynaBean.set("gender", new Integer(1)); //simple //设置 bean 属性 // Velocity 工具类,实例可以直接放入 map ,在模板文件中直接使用 // 如日期格式化 $dateTool.format("yyyy-MM-dd",$info.issueTime) DateTool dateTool = new DateTool();//日期工具 NumberTool numberTool = new NumberTool();//数字工具 model.put("dateTool",dateTool); model.put("numberTool",numberTool); model.put("bean", dynaBean); String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "./templates/velocity_template_email-newsletter.vm", StandardCharsets.UTF_8.toString(), model); helper.setText(text, true); mailSender.send(message); }
@RequestMapping(value = "/website/test.html") public String testDynaBeanController(Model model) { //创建动态 bean DynaBean dynaBean = new LazyDynaBean(); //字符串属性,通过属性名 name 调用 dynaBean.set("name", "Peter Collinson"); //simple //数字属性 ,通过属性名 gender 调用 dynaBean.set("gender", new Integer(1)); //simple //bean 属性,通过属性名 bean 调用 UserBean beanExample = new UserBean(); beanExample.setName("name string."); //simple dynaBean.set("bean", beanExample); //map 属性,通过 customer 和 title 调用 dynaBean.set("customer", "title", "Mr"); // mapped dynaBean.set("customer", "surname", "Smith"); // mapped //可以指定属性顺序,通过 address 和 顺序号调用 /** * 此方式无法通过 jstl 调用,不使用 * */ dynaBean.set("address", 0, "addressLine1"); // indexed dynaBean.set("address", 1, "addressLine2"); // indexed dynaBean.set("address", 2, "addressLine3"); // indexed //其他用法 /** * 可以转换成 map , 用法同 LazyDynaBean */ LazyDynaMap dynaMapBean = new LazyDynaMap(); dynaMapBean.set("name", "Peter Collinson"); //simple; Map myMap = dynaMapBean.getMap(); // retrieve the Map //传递到前端 model.addAttribute("test", dynaBean); return "2760/jsp/test"; /** * 前端调用 */ //页面 jstl 读取,不需要,用引入任何类,仅引入 jstl 标签即可使用 //注意,使用 map 这个关键字 //http://commons.apache.org/proper/commons-beanutils/apidocs/org/apache/commons/beanutils/BasicDynaBean.html //官方 doc 中,getMap() 方法解释处有说明 /** 调用代码 : test 为 bean 名称,如果是字符串 $ {map.name} 即可 $ {test.map.name} <br /> $ {test.map.gender} <br /> $ {test.map.bean.name} <br /> $ {test.map.customer.title} <br /> $ {test.map.customer.surname} <br /> */ //http://www.programgo.com/article/85353617047/ }
protected void setUp() throws Exception { DynaProperty name = new DynaProperty("name", int.class); bean = new LazyDynaBean(new LazyDynaClass("target", new DynaProperty[] { name } )); }
@Test public void testDynaBeans() throws Exception { // for usage, see // http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.2/apidocs/org/apache/commons/beanutils/package-summary.html#package_description final DynaBean dynaBean = new LazyDynaBean(); // Create LazyDynaBean final MutableDynaClass dynaClass = (MutableDynaClass) dynaBean .getDynaClass(); // get DynaClass dynaClass.add("amount", java.lang.Integer.class); // add property dynaClass.add("myMap", java.util.TreeMap.class); // add mapped property final DynaBean employee = dynaClass.newInstance(); // TODO experiment with Jackson's AnnotationIntrospector to annotate // DynaBean#get(...) method with @JsonAnyGetter and #set(...) method // with @JsonAnySetter employee.set("address", new HashMap<String, String>()); employee.set("activeEmployee", Boolean.FALSE); employee.set("firstName", "Fred"); employee.set("lastName", "Flintstone"); LOG.trace("Employee: " + JsonUtil.toPrettyJSON(employee)); // set all <activeEmployee> attribute values to <true> final BeanPropertyValueChangeClosure closure = new BeanPropertyValueChangeClosure( "activeEmployee", Boolean.TRUE); final Collection<?> employees = Collections.singleton(employee); LOG.trace("Mutated employees: " + JsonUtil.toPrettyJSON(employees)); // update the Collection CollectionUtils.forAllDo(employees, closure); // filter for beans with <activeEmployee> set to <false> final BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate( "lastName", "Flintstone"); // filter the Collection CollectionUtils.filter(employees, predicate); LOG.trace("Filtered employees: " + JsonUtil.toPrettyJSON(employees)); }
/** * 使用 <code>valueMap</code> 来构造一个 DynaBean. * * <h3>说明:</h3> * <blockquote> * <ol> * <li>一般情况下,你可能不需要使用这个方法</li> * <li>很适合那种属性值数量不确定,并且又不想在页面使用map来渲染的地方,比如制作多维度的图表</li> * <li>程序内部,默认使用的是 {@link org.apache.commons.beanutils.LazyDynaClass}</li> * <li>不需要先创建一个期望的数据结构DynaClass,就能向{@link LazyDynaBean}中填充我们任意想填充的数据。</li> * <li>{@link LazyDynaBean}内部会根据我们填充进的数据(即使是一个map中的一个key-value pair),创建metadata的。</li> * </ol> * </blockquote> * * <h3>示例:</h3> * * <blockquote> * * <pre class="code"> * DynaBean newDynaBean = BeanUtil.newDynaBean( * toMap(// * Pair.of("address", (Object) newHashMap()), * Pair.of("firstName", (Object) "Fred"), * Pair.of("lastName", (Object) "Flintstone"))); * LOGGER.debug(JsonUtil.format(newDynaBean)); * </pre> * * <b>返回:</b> * * <pre class="code"> * { * "address": {}, * "firstName": "Fred", * "lastName": "Flintstone" * } * </pre> * * </blockquote> * * @param valueMap * the value map * @return the dyna bean * @throws NullPointerException * 如果 <code>valueMap</code> 是null,或者 <code>valueMap</code>中有key是null * @throws IllegalArgumentException * 如果<code>valueMap</code>中有key是empty * @see org.apache.commons.beanutils.LazyDynaBean * @since 1.8.1 */ public static DynaBean newDynaBean(Map<String, ?> valueMap){ Validate.notNull(valueMap, "valueMap can't be null!"); LazyDynaBean lazyDynaBean = new LazyDynaBean(); for (Map.Entry<String, ?> entry : valueMap.entrySet()){ Validate.notBlank(entry.getKey(), "entry.getKey() can't be blank!"); lazyDynaBean.set(entry.getKey(), entry.getValue()); } return lazyDynaBean; }
/** * Default Constructor which creates a <code>LazyDynaBean</code> to <i>back</i> * this form. */ public LazyValidatorForm() { super(new LazyDynaBean()); }
/** * <p>Return the <code>Map</code> containing the property values.</p> * * <p>Provided so that properties can be access using JSTL.</p> */ public Map getMap() { return ((LazyDynaBean)dynaBean).getMap(); }
/** * <p>Creates new <code>DynaBean</code> instances to populate * an 'indexed' property of beans - defaults to <code>LazyDynaBean</code> * type.</p> * * <p>Override this method if you require a different type of * <code>DynaBean</code>.</p> */ protected DynaBean newIndexedBean(String name) { return new LazyDynaBean(); }
/** * Default Constructor which creates a <code>LazyDynaBean</code> to * <i>back</i> this form. */ public LazyValidatorForm() { super(new LazyDynaBean()); }
/** * <p>Return the <code>Map</code> containing the property values.</p> * * <p>Provided so that properties can be access using JSTL.</p> */ public Map getMap() { return ((LazyDynaBean) dynaBean).getMap(); }
/** * <p>Creates new <code>DynaBean</code> instances to populate an 'indexed' * property of beans - defaults to <code>LazyDynaBean</code> type.</p> * * <p>Override this method if you require a different type of * <code>DynaBean</code>.</p> */ protected DynaBean newIndexedBean(String name) { return new LazyDynaBean(); }