List of usage examples for org.springframework.web.servlet.support BindStatus getValue
@Nullable
public Object getValue()
From source file:org.hdiv.web.servlet.tags.form.SelectedValueComparatorHDIV.java
/** * Returns <code>true</code> if the supplied candidate value is equal to the value bound to * the supplied {@link BindStatus}. Equality in this case differs from standard Java equality and * is described in more detail <a href="#equality-contract">here</a>. *//*from w ww .jav a 2s. co m*/ public static boolean isSelected(BindStatus bindStatus, Object candidateValue) { if (bindStatus == null) { return (candidateValue == null); } // Check obvious equality matches with the candidate first, // both with the rendered value and with the original value. Object boundValue = bindStatus.getValue(); if (ObjectUtils.nullSafeEquals(boundValue, candidateValue)) { return true; } Object actualValue = bindStatus.getActualValue(); if (actualValue != null && actualValue != boundValue && ObjectUtils.nullSafeEquals(actualValue, candidateValue)) { return true; } if (actualValue != null) { boundValue = actualValue; } else if (boundValue == null) { return false; } // Non-null value but no obvious equality with the candidate value: // go into more exhaustive comparisons. boolean selected = false; if (boundValue.getClass().isArray()) { selected = collectionCompare(CollectionUtils.arrayToList(boundValue), candidateValue, bindStatus); } else if (boundValue instanceof Collection) { selected = collectionCompare((Collection) boundValue, candidateValue, bindStatus); } else if (boundValue instanceof Map) { selected = mapCompare((Map) boundValue, candidateValue, bindStatus); } if (!selected) { selected = exhaustiveCompare(boundValue, candidateValue, bindStatus.getEditor(), null); } return selected; }
From source file:org.hdiv.web.servlet.view.freemarker.FreeMarkerMacroTests.java
public void testExposeSpringMacroHelpers() throws Exception { FreeMarkerView fv = new FreeMarkerView() { @Override/*w ww.j ava2 s . co m*/ protected void processTemplate(Template template, SimpleHash fmModel, HttpServletResponse response) throws TemplateException { Map model = fmModel.toMap(); assertTrue( model.get(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE) instanceof RequestContext); RequestContext rc = (RequestContext) model .get(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE); BindStatus status = rc.getBindStatus("tb.name"); assertEquals("name", status.getExpression()); assertEquals("juergen", status.getValue()); } }; fv.setUrl(TEMPLATE_FILE); fv.setApplicationContext(wac); fv.setExposeSpringMacroHelpers(true); Map model = new HashMap(); model.put("tb", new TestBean("juergen", 99)); fv.render(model, request, response); }
From source file:org.openmrs.web.taglib.FormatDateTag.java
public int doStartTag() { RequestContext requestContext = (RequestContext) this.pageContext .getAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE); if (date == null && getPath() != null) { try {/*from w w w .j a va2 s . c om*/ // get the "path" object from the pageContext String resolvedPath = getPath(); String nestedPath = (String) pageContext.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE); if (nestedPath != null) { resolvedPath = nestedPath + resolvedPath; } BindStatus status = new BindStatus(requestContext, resolvedPath, false); log.debug("status: " + status); if (status.getValue() != null) { log.debug("status.value: " + status.getValue()); if (status.getValue().getClass() == Date.class) { // if no editor was registered all will go well here date = (Date) status.getValue(); } else { // if a "Date" property editor was registerd for the form, the status.getValue() // object will be a java.lang.String. This is useless. Try getting the original // value from the troublesome editor log.debug("status.valueType: " + status.getValueType()); Timestamp timestamp = (Timestamp) status.getEditor().getValue(); date = new Date(timestamp.getTime()); } } } catch (Exception e) { log.warn("Unable to get a date object from path: " + getPath(), e); return SKIP_BODY; } } if (!dateWasSet && date == null) { log.warn("Both 'date' and 'path' cannot be null. Page: " + pageContext.getPage() + " localname:" + pageContext.getRequest().getLocalName() + " rd:" + pageContext.getRequest().getRequestDispatcher("")); return SKIP_BODY; } if (type == null) { type = ""; } DateFormat dateFormat = null; if (format != null && format.length() > 0) { dateFormat = new SimpleDateFormat(format, Context.getLocale()); } else if (type.equals("xml")) { dateFormat = new SimpleDateFormat("dd-MMM-yyyy", Context.getLocale()); } else { log.debug("context locale: " + Context.getLocale()); if (type.equals("long")) { dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Context.getLocale()); } else if (type.equals("medium")) { dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Context.getLocale()); } else { dateFormat = Context.getDateFormat(); } } if (dateFormat == null) { dateFormat = new SimpleDateFormat("MM-dd-yyyy"); } String datestr = ""; try { if (date != null) { if (type.equals("milliseconds")) { datestr = "" + date.getTime(); } else { if (showTodayOrYesterday && (DateUtils.isSameDay(Calendar.getInstance().getTime(), date) || OpenmrsUtil.isYesterday(date))) { //print only time of day but maintaining the format(24 Vs 12) if any was specified String timeFormatString = (format != null && !format.contains("a")) ? "HH:mm" : "h:mm a"; dateFormat = new SimpleDateFormat(timeFormatString); if (DateUtils.isSameDay(Calendar.getInstance().getTime(), date)) { datestr = Context.getMessageSourceService().getMessage("general.today") + " " + dateFormat.format(date); } else { datestr = Context.getMessageSourceService().getMessage("general.yesterday") + " " + dateFormat.format(date); } } else { datestr = dateFormat.format(date); } } } } catch (IllegalArgumentException e) { //format or date is invalid log.error("date: " + date); log.error("format: " + format); log.error(e); datestr = date.toString(); } try { pageContext.getOut().write(datestr); } catch (IOException e) { log.error(e); } // reset the objects to null because taglibs are reused release(); return SKIP_BODY; }
From source file:org.hdiv.web.servlet.tags.form.SelectTagTests.java
public void testWithNestedOptions() throws Exception { this.tag.setPath("country"); int result = this.tag.doStartTag(); assertEquals(Tag.EVAL_BODY_INCLUDE, result); BindStatus value = (BindStatus) getPageContext().getAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE); assertEquals("Selected country not exposed in page context", "UK", value.getValue()); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); this.tag.doFinally(); String output = getWriter().toString(); assertTrue(output.startsWith("<select ")); assertTrue(output.endsWith("</select>")); assertContainsAttribute(output, "name", "country"); }