/** * Adds a special {@link DefaultMapping} that results in setting the target field on the model to an empty value * (typically null). * * @param mapper the mapper to add the mapping to * @param field the field for which a mapping is to be added * @param model the model */ protectedvoidaddEmptyValueMapping(DefaultMapper mapper, String field, Object model){ ParserContext parserContext = new FluentParserContext().evaluate(model.getClass()); Expression target = expressionParser.parseExpression(field, parserContext); try { Class<?> propertyType = target.getValueType(model); Expression source = new StaticExpression(getEmptyValue(propertyType)); DefaultMapping mapping = new DefaultMapping(source, target); if (logger.isDebugEnabled()) { logger.debug("Adding empty value mapping for parameter '" + field + "'"); } mapper.addMapping(mapping); } catch (EvaluationException e) { } }
/** * <p> * Causes the model to be populated from information contained in request parameters. * </p> * <p> * If a view has binding configuration then only model fields specified in the binding configuration will be * considered. In the absence of binding configuration all request parameters will be used to update matching fields * on the model. * </p> * * @param model the model to be updated * @return an instance of MappingResults with information about the results of the binding. */ protected MappingResults bind(Object model){ if (logger.isDebugEnabled()) { logger.debug("Binding to model"); } DefaultMapper mapper = new DefaultMapper(); ParameterMap requestParameters = requestContext.getRequestParameters(); if (binderConfiguration != null) { addModelBindings(mapper, requestParameters.asMap().keySet(), model); } else { addDefaultMappings(mapper, requestParameters.asMap().keySet(), model); } return mapper.map(requestParameters, model); }
public Expression parseExpression(String expressionString, ParserContext context)throws ParserException { Assert.notNull(expressionString, "The expression string to parse is required"); if (context == null) { context = NullParserContext.INSTANCE; } if (context.isTemplate()) { return parseTemplate(expressionString, context); } else { if (expressionString.startsWith(getExpressionPrefix()) && expressionString.endsWith(getExpressionSuffix())) { if (!allowDelimitedEvalExpressions) { thrownew ParserException( expressionString, "The expression '" + expressionString + "' being parsed is expected be a standard OGNL expression. Do not attempt to enclose such expression strings in ${} delimiters--this is redundant. If you need to parse a template that mixes literal text with evaluatable blocks, set the 'template' parser context attribute to true.", null); } else { int lastIndex = expressionString.length() - getExpressionSuffix().length(); String ognlExpression = expressionString.substring(getExpressionPrefix().length(), lastIndex); return doParseExpression(ognlExpression, context); } } else { return doParseExpression(expressionString, context); } } }