private void bind(ServletRequest request, ServletRequestDataBinder dataBinder) { MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request); MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class); if (multipartRequest != null) { bindMultipart(multipartRequest.getMultiFileMap(), mpvs); } String attr = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE; Map<String, String> uriVars = (Map<String, String>) request.getAttribute(attr); if (uriVars != null) { for (Map.Entry<String, String> entry : uriVars.entrySet()) { if (mpvs.contains(entry.getKey())) { logger.warn("Skipping URI variable '" + entry.getKey() + "' since the request contains a bind value with the same name."); } else { mpvs.addPropertyValue(entry.getKey(), entry.getValue()); } } } this.extendDataBinder.doExtendBind(mpvs, dataBinder); dataBinder.bind(mpvs); }
@Test public void testNoPrefix() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("forname", "Tony"); request.addParameter("surname", "Blair"); request.addParameter("age", "" + 50); ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(request); doTestTony(pvs); }
@Test public void testMultipleValuesForParameter() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); String[] original = new String[] {"Tony", "Rod"}; request.addParameter("forname", original); ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(request); assertTrue("Found 1 parameter", pvs.getPropertyValues().length == 1); assertTrue("Found array value", pvs.getPropertyValue("forname").getValue() instanceof String[]); String[] values = (String[]) pvs.getPropertyValue("forname").getValue(); assertEquals("Correct values", Arrays.asList(values), Arrays.asList(original)); }
private Map<String, Object> createMappedValues(Class<?> genericClass, NativeWebRequest webRequest, MethodParameter parameter, String prefix) { ServletRequest servletRequest = (ServletRequest) webRequest.getNativeRequest(); Map<String, Object> resultMap = new LinkedHashMap<String, Object>(); WebDataBinder binderHelper = new WebDataBinder(null); // 将数组提取为一个一个的KEY,这里是集合必须要有prefix + '[' Set<String> keySet = getSortedKeySet(servletRequest, prefix + '['); for (String key : keySet) { Object genericObj = null; if (key.endsWith(separator)) { ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(servletRequest, key, StringUtils.EMPTY); String realKey = key.substring(0, key.length() - 1); genericObj = convertIfDomainClass(webRequest, pvs, genericClass, realKey); if (null == genericObj) { genericObj = BeanUtils.instantiateClass(genericClass); } WebDataBinder objectBinder = new WebDataBinder(genericObj, realKey); objectBinder.bind(pvs); } else { Map<String, Object> paramValues = WebUtils.getParametersStartingWith(servletRequest, key); if (!paramValues.isEmpty()) { if (Collection.class.isAssignableFrom(genericClass)) { genericObj = binderHelper.convertIfNecessary(paramValues.values(), genericClass); } else { genericObj = binderHelper.convertIfNecessary(paramValues.values().iterator().next(), genericClass); } } } if (genericObj != null) { resultMap.put(key, genericObj); } } return resultMap; }
public Entity parseModel(HttpServletRequest request) { Object o = null; try { o = getEntityClass().newInstance(); } catch (Exception e) { throw new RuntimeException("无法创建类的实例:" + getEntityClass()); } DataBinder dataBinder = new DataBinder(o); dataBinder.registerCustomEditor(Time.class, new TimeEditor()); MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request); dataBinder.bind(mpvs); return (Entity) o; }
/** * Save element to database * * @param type * the domain type * @param request * current HttpServletRequest * @param page * current page * @param size * current page size * @param model * current model * * @return edit/view template name */ @RequestMapping(value = "${crudadmin.url}/post/{type}/{size}/{page}", method = RequestMethod.POST) public String save(@PathVariable String type, @PathVariable int size, @PathVariable int page, HttpServletRequest request, Model model) { try { CrudAdminRepository repoAdmin = repositoryMap.get(type); ServletRequestParameterPropertyValues values = new ServletRequestParameterPropertyValues(request); final BindingResult result = repoAdmin.getWebDataBinder(values).getBindingResult(); Object object = result.getTarget(); String idString = values.getPropertyValue("id").getValue().toString(); idString = idString.isEmpty() ? null : idString; Object id = null; try { // existing object ? if (idString != null) { id = idSerializer.fromString(idString, repoAdmin.getIdType()); repoAdmin.getId().set(object, id); } // save repoAdmin.getRepositoryObjectSerializable().save(object); idString = getStringFromId(repoAdmin.getId().get(object), repoAdmin); return "redirect:/" + crudAdminProperties.getUrl() + "/view/" + repoAdmin.getDomainTypeNameLowerCase() + "/" + size + "/" + page + "/" + idString+'/'; } catch (Exception transactionException) { Throwable rootCause = transactionException; while (rootCause.getCause() != null) { rootCause = rootCause.getCause(); } if (rootCause instanceof ConstraintViolationException) { ConstraintViolationException constraintViolationException = (ConstraintViolationException) rootCause; String message = ""; for (ConstraintViolation<?> constraintValidation : constraintViolationException .getConstraintViolations()) { message += "<br/><b>" + constraintValidation.getPropertyPath().toString() + "</b> " + constraintValidation.getMessage(); } return doEdit(type, idString, size, page, message, model); } throw transactionException; } } catch (Exception ex) { throw new RuntimeException(ex); } }
@Test public void testNoParameters() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(request); assertTrue("Found no parameters", pvs.getPropertyValues().length == 0); }