List of usage examples for org.apache.commons.beanutils LazyDynaBean set
public void set(String name, Object value)
From source file:com.feilong.core.bean.BeanUtil.java
/** * New dyna bean.//from w w w. j av a 2 s . c om * * <p> * {@link LazyDynaBean}????DynaClass?LazyDynaBean??<br> * {@link LazyDynaBean}???mapkey-value pairLazyDynaBeanmapmetadatametadata<br> * * ?, {@link org.apache.commons.beanutils.LazyDynaClass} * </p> * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * * DynaBean newDynaBean = BeanUtil.newDynaBean(toMap(// * Pair.of("address", (Object) new HashMap()), * 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 * @see org.apache.commons.beanutils.LazyDynaBean * @throws NullPointerException * <code>valueMap</code> null, mapkeynull * @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; }
From source file:org.apache.commons.validator.example.ValidateExample.java
/** * This is the main method that will be called to initialize the Validator, create some sample beans, and * run the Validator against them./*from www.ja va 2 s . com*/ */ public static void main(String[] args) throws ValidatorException, IOException, SAXException { InputStream in = null; ValidatorResources resources = null; try { // Create a new instance of a ValidatorResource, then get a stream // handle on the XML file with the actions in it, and initialize the // resources from it. This would normally be done by a servlet // run during JSP initialization or some other application-startup // routine. in = ValidateExample.class.getResourceAsStream("validator-example.xml"); resources = new ValidatorResources(in); } finally { // Make sure we close the input stream. if (in != null) { in.close(); } } // Create a test bean to validate against. //ValidateBean bean = new ValidateBean(); LazyDynaClass dynaClass = new LazyDynaClass(); dynaClass.add("lastName", String.class); dynaClass.add("firstName", String.class); dynaClass.add("street1", String.class); dynaClass.add("city", String.class); dynaClass.add("state", String.class); dynaClass.add("postalCode", String.class); dynaClass.add("age", String.class); LazyDynaBean dynaBean = new LazyDynaBean(dynaClass); // Create a validator with the ValidateBean actions for the bean // we're interested in. Validator validator = new Validator(resources, "ValidateBean"); // Tell the validator which bean to validate against. validator.setParameter(Validator.BEAN_PARAM, dynaBean); ValidatorResults results = null; // Run the validation actions against the bean. Since all of the properties // are null, we expect them all to error out except for street2, which has // no validations (it's an optional property) results = validator.validate(); printResults(dynaBean, results, resources); // Now set all the required properties, but make the age a non-integer. // You'll notice that age will pass the required test, but fail the int // test. dynaBean.set("lastName", "Tester"); dynaBean.set("firstName", "John"); dynaBean.set("street1", "1 Test Street"); dynaBean.set("city", "Testville"); dynaBean.set("state", "TE"); dynaBean.set("postalCode", "12345"); dynaBean.set("age", "Too Old"); results = validator.validate(); printResults(dynaBean, results, resources); // Now only report failed fields validator.setOnlyReturnErrors(true); results = validator.validate(); printResults(dynaBean, results, resources); // Now everything should pass. validator.setOnlyReturnErrors(false); dynaBean.set("age", "123"); results = validator.validate(); printResults(dynaBean, results, resources); }