org.apache.commons.beanutils.LazyDynaBean Java Examples

The following examples show how to use org.apache.commons.beanutils.LazyDynaBean. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestMultiWrapDynaBean.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new test object with a list of wrapped beans.
 *
 * @param withDynaBean a flag whether also a DynaBean should be added to the
 *        wrapped beans
 * @return the test bean
 */
private MultiWrapDynaBean createBean(final boolean withDynaBean)
{
    params = new BasicBuilderParameters();
    wrapBean = new WrappedBeanTestImpl();
    final Collection<Object> beans = new ArrayList<>();
    beans.add(params);
    beans.add(wrapBean);
    if (withDynaBean)
    {
        wrapDynaBean = new LazyDynaBean();
        wrapDynaBean.set(MAPPED_PROPERTY, "someKey", "somValue");
        beans.add(wrapDynaBean);
    }
    return new MultiWrapDynaBean(beans);
}
 
Example #2
Source File: SpringMailService.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #3
Source File: TestBeanHelper.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether properties from one bean to another can be copied.
 */
@Test
public void testCopyProperties() throws Exception
{
    final PropertiesConfiguration src = new PropertiesConfiguration();
    src.setHeader("TestHeader");
    src.setFooter("TestFooter");
    final LazyDynaBean dest = new LazyDynaBean();
    BeanHelper.copyProperties(dest, src);
    assertEquals("Wrong footer property", "TestFooter", dest.get("footer"));
    assertEquals("Wrong header property", "TestHeader", dest.get("header"));
}
 
Example #4
Source File: DynaBeansExample.java    From spring-boot with Apache License 2.0 4 votes vote down vote up
@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/
}
 
Example #5
Source File: StrictLazyDynaBeanPointerFactory.java    From commons-jxpath with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public NodePointer createNodePointer(QName name, Object object, Locale locale) {
    return object instanceof LazyDynaBean ? new StrictLazyDynaBeanPointer(name,
            (LazyDynaBean) object, locale) : null;
}
 
Example #6
Source File: StrictLazyDynaBeanPointerFactory.java    From commons-jxpath with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public NodePointer createNodePointer(NodePointer parent, QName name, Object object) {
    return object instanceof LazyDynaBean ? new StrictLazyDynaBeanPointer(parent, name,
            (LazyDynaBean) object) : null;
}
 
Example #7
Source File: LazyDynaBeanTest.java    From commons-jxpath with Apache License 2.0 4 votes vote down vote up
public void testLazyProperty() throws JXPathNotFoundException {
    LazyDynaBean bean = new LazyDynaBean();
    JXPathContext context = JXPathContext.newContext(bean);
    context.getValue("nosuch");
}
 
Example #8
Source File: BeanUtil.java    From feilong-core with Apache License 2.0 3 votes vote down vote up
/**
 * 使用 <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
 * @since 1.13.2 param map change from {@code Map<String, ?> valueMap} to {@code Map<?, ?> valueMap}
 */
public static DynaBean newDynaBean(Map<?, ?> valueMap){
    Validate.notNull(valueMap, "valueMap can't be null!");

    //---------------------------------------------------------------
    LazyDynaBean lazyDynaBean = new LazyDynaBean();
    for (Map.Entry<?, ?> entry : valueMap.entrySet()){
        String key = ConvertUtil.toString(entry.getKey());

        Validate.notBlank(key, "entry.getKey() can't be blank!");
        lazyDynaBean.set(key, entry.getValue());
    }
    return lazyDynaBean;
}
 
Example #9
Source File: StrictLazyDynaBeanPointerFactory.java    From commons-jxpath with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new StrictLazyDynaBeanPointer instance.
 *
 * @param parent pointer
 * @param name is the name given to the first node
 * @param lazyDynaBean pointed
 */
public StrictLazyDynaBeanPointer(NodePointer parent, QName name, LazyDynaBean lazyDynaBean) {
    super(parent, name, lazyDynaBean);
    this.lazyDynaBean = lazyDynaBean;
}
 
Example #10
Source File: StrictLazyDynaBeanPointerFactory.java    From commons-jxpath with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new StrictLazyDynaBeanPointer instance.
 *
 * @param name is the name given to the first node
 * @param lazyDynaBean pointed
 * @param locale Locale
 */
public StrictLazyDynaBeanPointer(QName name, LazyDynaBean lazyDynaBean, Locale locale) {
    super(name, lazyDynaBean, locale);
    this.lazyDynaBean = lazyDynaBean;
}