Example usage for org.springframework.validation BindingResultUtils getBindingResult

List of usage examples for org.springframework.validation BindingResultUtils getBindingResult

Introduction

In this page you can find the example usage for org.springframework.validation BindingResultUtils getBindingResult.

Prototype

@Nullable
public static BindingResult getBindingResult(Map<?, ?> model, String name) 

Source Link

Document

Find the BindingResult for the given name in the given model.

Usage

From source file:org.sakaiproject.metaobj.shared.control.XsltArtifactView.java

protected Source createXsltSource(Map map, String string, HttpServletRequest httpServletRequest,
        HttpServletResponse httpServletResponse) throws Exception {

    httpServletResponse.setContentType(getContentType());
    WebApplicationContext context = getWebApplicationContext();
    setUriResolver((URIResolver) context.getBean(uriResolverBeanName));

    ToolSession toolSession = SessionManager.getCurrentToolSession();

    String homeType = null;//from ww  w.j av  a 2s  .co  m

    ElementBean bean = (ElementBean) map.get("bean");

    Element root = null;
    Map paramsMap = new Hashtable();

    for (Enumeration e = httpServletRequest.getParameterNames(); e.hasMoreElements();) {
        String k = e.nextElement().toString();
        // Do not allow reserved parameter names to be overwritten
        if (!reservedParams.contains(k)) {
            paramsMap.put(k, httpServletRequest.getParameter(k));
        }
    }

    httpServletRequest.setAttribute(STYLESHEET_PARAMS, paramsMap);
    if (toolSession.getAttribute(FormHelper.PREVIEW_HOME_TAG) != null) {
        paramsMap.put("preview", "true");
    }

    if (toolSession.getAttribute(ResourceToolAction.ACTION_PIPE) != null) {
        paramsMap.put("fromResources", "true");
    }

    if (httpServletRequest.getAttribute(FormHelper.URL_DECORATION) != null) {
        paramsMap.put("urlDecoration", httpServletRequest.getAttribute(FormHelper.URL_DECORATION));
    }

    HashMap<String, String> xslParams = new HashMap<String, String>();
    xslParams.put(FormHelper.XSL_PRESENTATION_ID, FormHelper.PRESENTATION_ID);
    xslParams.put(FormHelper.XSL_PRESENTATION_TYPE, FormHelper.PRESENTATION_TEMPLATE_ID);
    xslParams.put(FormHelper.XSL_PRESENTATION_ITEM_ID, FormHelper.PRESENTATION_ITEM_DEF_ID);
    xslParams.put(FormHelper.XSL_PRESENTATION_ITEM_NAME, FormHelper.PRESENTATION_ITEM_DEF_NAME);
    xslParams.put(FormHelper.XSL_FORM_TYPE, ResourceEditingHelper.CREATE_SUB_TYPE);
    xslParams.put(FormHelper.XSL_ARTIFACT_REFERENCE, ResourceEditingHelper.ATTACHMENT_ID);
    xslParams.put(FormHelper.XSL_OBJECT_ID, FormHelper.XSL_OBJECT_ID);
    xslParams.put(FormHelper.XSL_OBJECT_TITLE, FormHelper.XSL_OBJECT_TITLE);
    xslParams.put(FormHelper.XSL_WIZARD_PAGE_ID, FormHelper.XSL_WIZARD_PAGE_ID);

    // Load up our XSL parameters according to the mapping into the tool session above.
    // Note that this is not always one-to-one due to some string/key inconsistencies around the tools.
    for (Entry<String, String> item : xslParams.entrySet()) {
        Object val = toolSession.getAttribute(item.getValue());
        if (val != null) {
            paramsMap.put(item.getKey(), val);
        }
    }

    Id id = null;

    if (bean instanceof Artifact) {
        root = getStructuredArtifactDefinitionManager().createFormViewXml((Artifact) bean, null);
        homeType = getHomeType((Artifact) bean);
        id = ((Artifact) bean).getId();
    } else {
        EditedArtifactStorage sessionBean = (EditedArtifactStorage) httpServletRequest.getSession()
                .getAttribute(EditedArtifactStorage.EDITED_ARTIFACT_STORAGE_SESSION_KEY);

        if (sessionBean != null) {
            root = getStructuredArtifactDefinitionManager()
                    .createFormViewXml((Artifact) sessionBean.getRootArtifact(), null);

            replaceNodes(root, bean, sessionBean);
            paramsMap.put("subForm", "true");
            homeType = getHomeType(sessionBean.getRootArtifact());
            id = sessionBean.getRootArtifact().getId();
        } else {
            return new javax.xml.transform.dom.DOMSource();
        }
    }

    if (id != null) {
        paramsMap.put("edit", "true");
        paramsMap.put(FormHelper.XSL_ARTIFACT_ID, id.getValue());
    }

    httpServletRequest.setAttribute(STYLESHEET_LOCATION,
            getStructuredArtifactDefinitionManager().getTransformer(homeType, readOnly));

    Errors errors = BindingResultUtils.getBindingResult(map, "bean");
    if (errors != null && errors.hasErrors()) {
        Element errorsElement = new Element("errors");

        List errorsList = errors.getAllErrors();

        for (Iterator i = errorsList.iterator(); i.hasNext();) {
            Element errorElement = new Element("error");
            ObjectError error = (ObjectError) i.next();
            if (error instanceof FieldError) {
                FieldError fieldError = (FieldError) error;
                errorElement.setAttribute("field", fieldError.getField());
                Element rejectedValue = new Element("rejectedValue");
                if (fieldError.getRejectedValue() != null) {
                    rejectedValue.addContent(fieldError.getRejectedValue().toString());
                }
                errorElement.addContent(rejectedValue);
            }
            Element message = new Element("message");
            message.addContent(context.getMessage(error, getResourceLoader().getLocale()));
            errorElement.addContent(message);
            errorsElement.addContent(errorElement);
        }

        root.addContent(errorsElement);
    }

    if (httpServletRequest.getParameter("success") != null) {
        Element success = new Element("success");
        success.setAttribute("messageKey", httpServletRequest.getParameter("success"));
        root.addContent(success);
    }

    if (toolSession.getAttribute(ResourceEditingHelper.CUSTOM_CSS) != null) {
        Element uri = new Element("uri");
        uri.setText((String) toolSession.getAttribute(ResourceEditingHelper.CUSTOM_CSS));
        root.getChild("css").addContent(uri);
        uri.setAttribute("order", "100");
    }

    if (toolSession.getAttribute(FormHelper.FORM_STYLES) != null) {
        List styles = (List) toolSession.getAttribute(FormHelper.FORM_STYLES);
        int index = 101;
        for (Iterator<String> i = styles.iterator(); i.hasNext();) {
            Element uri = new Element("uri");
            uri.setText(i.next());
            root.getChild("css").addContent(uri);
            uri.setAttribute("order", "" + index);
            index++;
        }
    }

    Document doc = new Document(root);
    return new JDOMSource(doc);
}

From source file:cherry.foundation.type.format.CustomNumberFormatTest.java

private String parseAndPrint(String name, String value) throws BindException {
    Map<String, String> paramMap = new HashMap<>();
    paramMap.put(name, value);/*from w w w  .  jav  a2 s. c  o m*/

    Form form = new Form();
    DataBinder binder = new DataBinder(form, "target");
    binder.setConversionService(conversionService);
    binder.bind(new MutablePropertyValues(paramMap));

    BindingResult binding = BindingResultUtils.getBindingResult(binder.close(), "target");
    return (String) binding.getFieldValue(name);
}

From source file:org.shept.org.springframework.web.servlet.mvc.delegation.ComponentUtils.java

/**
 * //from  w  w  w .  j  a v a  2 s .c  om
 * @param
 * @return
 *
 * @param modelAndView
 * @return
 */
public static SubCommandProvider getCommand(ModelAndView modelAndView) {
    String prefix = BindingResult.MODEL_KEY_PREFIX;
    if (modelAndView == null) {
        return null;
    }
    for (String name : modelAndView.getModel().keySet()) {
        if (name.startsWith(prefix)) {
            BindingResult res = BindingResultUtils.getBindingResult(modelAndView.getModel(),
                    name.substring(prefix.length()));
            if (res != null && res.getTarget() instanceof SubCommandProvider) {
                return (SubCommandProvider) res.getTarget();
            }
        }
    }
    return null;
}