org.springframework.web.bind.ServletRequestDataBinder Java Examples
The following examples show how to use
org.springframework.web.bind.ServletRequestDataBinder.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BindTagTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void transformTagNonExistingValue() throws JspException { // first set up the pagecontext and the bean PageContext pc = createPageContext(); TestBean tb = new TestBean(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "tb"); CustomDateEditor l = new CustomDateEditor(df, true); binder.registerCustomEditor(Date.class, l); pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", binder.getBindingResult()); // try with non-existing value BindTag bind = new BindTag(); bind.setPageContext(pc); bind.setPath("tb.name"); bind.doStartTag(); TransformTag transform = new TransformTag(); transform.setPageContext(pc); transform.setValue(null); transform.setParent(bind); transform.setVar("theString2"); transform.doStartTag(); assertNull(pc.getAttribute("theString2")); }
Example #2
Source File: Jackson2ServletRequestDataBinderFactory.java From gvnix with GNU General Public License v3.0 | 6 votes |
/** * Look current Thread for {@link ServletRequestDataBinder} created by * {@link DataBinderMappingJackson2HttpMessageConverter}, if found return * it, otherwise it delegates on parent method. * * @param target * @param objectName * @param request * @return ServletRequestDataBinder */ @Override protected ServletRequestDataBinder createBinderInstance(Object target, String objectName, NativeWebRequest request) { try { ServletRequestDataBinder binder = (ServletRequestDataBinder) ThreadLocalUtil .getThreadVariable(BindingResult.MODEL_KEY_PREFIX .concat("JSON_DataBinder")); if (binder != null) { return binder; } return super.createBinderInstance(target, objectName, request); } finally { ThreadLocalUtil.destroy(); } }
Example #3
Source File: SitePermissionHandler.java From webcurator with Apache License 2.0 | 6 votes |
@Override public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { super.initBinder(request, binder); NumberFormat nf = NumberFormat.getInstance(request.getLocale()); // Register the binders. binder.registerCustomEditor(Long.class, "selectedPermission", new CustomNumberEditor(Long.class, nf, true)); binder.registerCustomEditor(java.util.Date.class, "startDate", DateUtils.get().getFullDateEditor(true)); binder.registerCustomEditor(java.util.Date.class, "endDate", DateUtils.get().getFullDateEditor(true)); // If the session model is available, we want to register the Permission's // authorising agency editor. if(getEditorContext(request) != null) { //binder.registerCustomEditor(AuthorisingAgent.class, new PermissionAuthAgencyEditor(sessionModel.getAuthorisingAgents())); binder.registerCustomEditor(AuthorisingAgent.class, "authorisingAgent", new EditorContextObjectEditor(getEditorContext(request), AuthorisingAgent.class)); binder.registerCustomEditor(Set.class, "urls", new UrlPatternCollectionEditor(Set.class, true, getEditorContext(request))); } }
Example #4
Source File: BaseController.java From ankush with GNU Lesser General Public License v3.0 | 6 votes |
/** * Set up a custom property editor for converting form inputs to real * objects. * * @param request the current request * @param binder the data binder */ @InitBinder protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) { binder.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, null, true)); binder.registerCustomEditor(Long.class, null, new CustomNumberEditor( Long.class, null, true)); binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor()); SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd G 'at' HH:mm:ss z"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, null, new CustomDateEditor( dateFormat, true)); }
Example #5
Source File: BindTagTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void transformTagNonExistingValue() throws JspException { // first set up the pagecontext and the bean PageContext pc = createPageContext(); TestBean tb = new TestBean(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "tb"); CustomDateEditor l = new CustomDateEditor(df, true); binder.registerCustomEditor(Date.class, l); pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", binder.getBindingResult()); // try with non-existing value BindTag bind = new BindTag(); bind.setPageContext(pc); bind.setPath("tb.name"); bind.doStartTag(); TransformTag transform = new TransformTag(); transform.setPageContext(pc); transform.setValue(null); transform.setParent(bind); transform.setVar("theString2"); transform.doStartTag(); assertNull(pc.getAttribute("theString2")); }
Example #6
Source File: BindTagTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void transformTagNonExistingValue() throws JspException { // first set up the pagecontext and the bean PageContext pc = createPageContext(); TestBean tb = new TestBean(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "tb"); CustomDateEditor l = new CustomDateEditor(df, true); binder.registerCustomEditor(Date.class, l); pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", binder.getBindingResult()); // try with non-existing value BindTag bind = new BindTag(); bind.setPageContext(pc); bind.setPath("tb.name"); bind.doStartTag(); TransformTag transform = new TransformTag(); transform.setPageContext(pc); transform.setValue(null); transform.setParent(bind); transform.setVar("theString2"); transform.doStartTag(); assertNull(pc.getAttribute("theString2")); }
Example #7
Source File: BindTagTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void bindTagWithMappedProperties() throws JspException { PageContext pc = createPageContext(); IndexedTestBean tb = new IndexedTestBean(); Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult(); errors.rejectValue("map[key1]", "code1", "message1"); errors.rejectValue("map[key1]", "code2", "message2"); pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors); BindTag tag = new BindTag(); tag.setPageContext(pc); tag.setPath("tb.map[key1]"); assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE); BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE); assertTrue("Has status variable", status != null); assertTrue("Correct expression", "map[key1]".equals(status.getExpression())); assertTrue("Value is TestBean", status.getValue() instanceof TestBean); assertTrue("Correct value", "name4".equals(((TestBean) status.getValue()).getName())); assertTrue("Correct isError", status.isError()); assertTrue("Correct errorCodes", status.getErrorCodes().length == 2); assertTrue("Correct errorMessages", status.getErrorMessages().length == 2); assertTrue("Correct errorCode", "code1".equals(status.getErrorCodes()[0])); assertTrue("Correct errorCode", "code2".equals(status.getErrorCodes()[1])); assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessages()[0])); assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessages()[1])); }
Example #8
Source File: BindTagTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void bindTagWithoutErrors() throws JspException { PageContext pc = createPageContext(); Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult(); pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors); BindTag tag = new BindTag(); tag.setPageContext(pc); tag.setPath("tb"); assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE); BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE); assertTrue("Has status variable", status != null); assertTrue("Correct expression", status.getExpression() == null); assertTrue("Correct value", status.getValue() == null); assertTrue("Correct displayValue", "".equals(status.getDisplayValue())); assertTrue("Correct isError", !status.isError()); assertTrue("Correct errorCodes", status.getErrorCodes().length == 0); assertTrue("Correct errorMessages", status.getErrorMessages().length == 0); assertTrue("Correct errorCode", "".equals(status.getErrorCode())); assertTrue("Correct errorMessage", "".equals(status.getErrorMessage())); assertTrue("Correct errorMessagesAsString", "".equals(status.getErrorMessagesAsString(","))); }
Example #9
Source File: BindTagTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void bindTagWithMappedProperties() throws JspException { PageContext pc = createPageContext(); IndexedTestBean tb = new IndexedTestBean(); Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult(); errors.rejectValue("map[key1]", "code1", "message1"); errors.rejectValue("map[key1]", "code2", "message2"); pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors); BindTag tag = new BindTag(); tag.setPageContext(pc); tag.setPath("tb.map[key1]"); assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE); BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE); assertTrue("Has status variable", status != null); assertTrue("Correct expression", "map[key1]".equals(status.getExpression())); assertTrue("Value is TestBean", status.getValue() instanceof TestBean); assertTrue("Correct value", "name4".equals(((TestBean) status.getValue()).getName())); assertTrue("Correct isError", status.isError()); assertTrue("Correct errorCodes", status.getErrorCodes().length == 2); assertTrue("Correct errorMessages", status.getErrorMessages().length == 2); assertTrue("Correct errorCode", "code1".equals(status.getErrorCodes()[0])); assertTrue("Correct errorCode", "code2".equals(status.getErrorCodes()[1])); assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessages()[0])); assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessages()[1])); }
Example #10
Source File: BindTagTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void bindTagWithIndexedProperties() throws JspException { PageContext pc = createPageContext(); IndexedTestBean tb = new IndexedTestBean(); Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult(); errors.rejectValue("array[0]", "code1", "message1"); errors.rejectValue("array[0]", "code2", "message2"); pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors); BindTag tag = new BindTag(); tag.setPageContext(pc); tag.setPath("tb.array[0]"); assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE); BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE); assertTrue("Has status variable", status != null); assertTrue("Correct expression", "array[0]".equals(status.getExpression())); assertTrue("Value is TestBean", status.getValue() instanceof TestBean); assertTrue("Correct value", "name0".equals(((TestBean) status.getValue()).getName())); assertTrue("Correct isError", status.isError()); assertTrue("Correct errorCodes", status.getErrorCodes().length == 2); assertTrue("Correct errorMessages", status.getErrorMessages().length == 2); assertTrue("Correct errorCode", "code1".equals(status.getErrorCodes()[0])); assertTrue("Correct errorCode", "code2".equals(status.getErrorCodes()[1])); assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessages()[0])); assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessages()[1])); }
Example #11
Source File: BindTagTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void nestedPathWithBindTagWithIgnoreNestedPath() throws JspException { PageContext pc = createPageContext(); Errors errors = new ServletRequestDataBinder(new TestBean(), "tb2").getBindingResult(); pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb2", errors); NestedPathTag tag = new NestedPathTag(); tag.setPath("tb"); tag.setPageContext(pc); tag.doStartTag(); BindTag bindTag = new BindTag(); bindTag.setPageContext(pc); bindTag.setIgnoreNestedPath(true); bindTag.setPath("tb2.name"); assertTrue("Correct doStartTag return value", bindTag.doStartTag() == Tag.EVAL_BODY_INCLUDE); BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE); assertTrue("Has status variable", status != null); assertEquals("tb2.name", status.getPath()); }
Example #12
Source File: BindTagTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void bindTagWithoutErrors() throws JspException { PageContext pc = createPageContext(); Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult(); pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors); BindTag tag = new BindTag(); tag.setPageContext(pc); tag.setPath("tb"); assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE); BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE); assertTrue("Has status variable", status != null); assertTrue("Correct expression", status.getExpression() == null); assertTrue("Correct value", status.getValue() == null); assertTrue("Correct displayValue", "".equals(status.getDisplayValue())); assertTrue("Correct isError", !status.isError()); assertTrue("Correct errorCodes", status.getErrorCodes().length == 0); assertTrue("Correct errorMessages", status.getErrorMessages().length == 0); assertTrue("Correct errorCode", "".equals(status.getErrorCode())); assertTrue("Correct errorMessage", "".equals(status.getErrorMessage())); assertTrue("Correct errorMessagesAsString", "".equals(status.getErrorMessagesAsString(","))); }
Example #13
Source File: AgencyControllerTest.java From webcurator with Apache License 2.0 | 6 votes |
/** * Test method for {@link org.webcurator.ui.admin.controller.AgencyController#initBinder(javax.servlet.http.HttpServletRequest, org.springframework.web.bind.ServletRequestDataBinder)}. */ @Test public final void testInitBinder() { MockHttpServletRequest request = new MockHttpServletRequest(); ServletRequestDataBinder binder = new ServletRequestDataBinder(new AgencyCommand(), "command"); try { testInstance.initBinder(request, binder); } catch (Exception e) { String message = e.getClass().toString() + " - " + e.getMessage(); log.debug(message); fail(message); } }
Example #14
Source File: ExtendedServletRequestDataBinderTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void uriTemplateVarAndRequestParam() throws Exception { request.addParameter("age", "35"); Map<String, String> uriTemplateVars = new HashMap<>(); uriTemplateVars.put("name", "nameValue"); uriTemplateVars.put("age", "25"); request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars); TestBean target = new TestBean(); WebDataBinder binder = new ExtendedServletRequestDataBinder(target, ""); ((ServletRequestDataBinder) binder).bind(request); assertEquals("nameValue", target.getName()); assertEquals(35, target.getAge()); }
Example #15
Source File: BindTagTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void nestedPathWithBindTagWithIgnoreNestedPath() throws JspException { PageContext pc = createPageContext(); Errors errors = new ServletRequestDataBinder(new TestBean(), "tb2").getBindingResult(); pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb2", errors); NestedPathTag tag = new NestedPathTag(); tag.setPath("tb"); tag.setPageContext(pc); tag.doStartTag(); BindTag bindTag = new BindTag(); bindTag.setPageContext(pc); bindTag.setIgnoreNestedPath(true); bindTag.setPath("tb2.name"); assertTrue("Correct doStartTag return value", bindTag.doStartTag() == Tag.EVAL_BODY_INCLUDE); BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE); assertTrue("Has status variable", status != null); assertEquals("tb2.name", status.getPath()); }
Example #16
Source File: AbstractOverrideTabHandler.java From webcurator with Apache License 2.0 | 5 votes |
@Override public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { // Determine the necessary formats. NumberFormat nf = NumberFormat.getInstance(request.getLocale()); // Register the binders. binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, nf, true)); binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, nf, true)); }
Example #17
Source File: RoleControllerTest.java From webcurator with Apache License 2.0 | 5 votes |
@Test public final void testInitBinder() { MockHttpServletRequest request = new MockHttpServletRequest(); ServletRequestDataBinder binder = new ServletRequestDataBinder(new RoleCommand(), "command"); try { testInstance.initBinder(request, binder); } catch (Exception e) { String message = e.getClass().toString() + " - " + e.getMessage(); log.debug(message); fail(message); } }
Example #18
Source File: TargetGroupsHandler.java From webcurator with Apache License 2.0 | 5 votes |
/** * @see org.webcurator.ui.util.TabHandler#initBinder(javax.servlet.http.HttpServletRequest, org.springframework.web.bind.ServletRequestDataBinder) */ @Override public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { super.initBinder(request, binder); // Register a number binder. NumberFormat nf = NumberFormat.getInstance(request.getLocale()); binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, nf, true)); }
Example #19
Source File: BindTagTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void bindTagWithIndexedProperties() throws JspException { PageContext pc = createPageContext(); IndexedTestBean tb = new IndexedTestBean(); Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult(); errors.rejectValue("array[0]", "code1", "message1"); errors.rejectValue("array[0]", "code2", "message2"); pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors); BindTag tag = new BindTag(); tag.setPageContext(pc); tag.setPath("tb.array[0]"); assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE); BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE); assertTrue("Has status variable", status != null); assertTrue("Correct expression", "array[0]".equals(status.getExpression())); assertTrue("Value is TestBean", status.getValue() instanceof TestBean); assertTrue("Correct value", "name0".equals(((TestBean) status.getValue()).getName())); assertTrue("Correct isError", status.isError()); assertTrue("Correct errorCodes", status.getErrorCodes().length == 2); assertTrue("Correct errorMessages", status.getErrorMessages().length == 2); assertTrue("Correct errorCode", "code1".equals(status.getErrorCodes()[0])); assertTrue("Correct errorCode", "code2".equals(status.getErrorCodes()[1])); assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessages()[0])); assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessages()[1])); }
Example #20
Source File: BindTagTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void bindTagWithNestedFieldErrors() throws JspException { PageContext pc = createPageContext(); TestBean tb = new TestBean(); tb.setName("name1"); TestBean spouse = new TestBean(); spouse.setName("name2"); tb.setSpouse(spouse); Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult(); errors.rejectValue("spouse.name", "code1", "message1"); pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors); BindTag tag = new BindTag(); tag.setPageContext(pc); tag.setPath("tb.spouse.name"); assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE); BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE); assertTrue("Has status variable", status != null); assertTrue("Correct expression", "spouse.name".equals(status.getExpression())); assertTrue("Correct value", "name2".equals(status.getValue())); assertTrue("Correct displayValue", "name2".equals(status.getDisplayValue())); assertTrue("Correct isError", status.isError()); assertTrue("Correct errorCodes", status.getErrorCodes().length == 1); assertTrue("Correct errorMessages", status.getErrorMessages().length == 1); assertTrue("Correct errorCode", "code1".equals(status.getErrorCode())); assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessage())); assertTrue("Correct errorMessagesAsString", "message1".equals(status.getErrorMessagesAsString(" - "))); }
Example #21
Source File: BindTagTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void bindErrorsTagWithoutErrors() throws JspException { PageContext pc = createPageContext(); Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult(); pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors); BindErrorsTag tag = new BindErrorsTag(); tag.setPageContext(pc); tag.setName("tb"); assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.SKIP_BODY); assertTrue("Doesn't have errors variable", pc.getAttribute(BindErrorsTag.ERRORS_VARIABLE_NAME) == null); }
Example #22
Source File: ServletModelAttributeMethodProcessor.java From spring-analysis-note with MIT License | 5 votes |
/** * This implementation downcasts {@link WebDataBinder} to * {@link ServletRequestDataBinder} before binding. * @see ServletRequestDataBinderFactory */ @Override protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) { ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class); Assert.state(servletRequest != null, "No ServletRequest"); ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder; servletBinder.bind(servletRequest); }
Example #23
Source File: RegisteredServiceSimpleFormController.java From cas4.0.x-server-wechat with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} * Sets the require fields and the disallowed fields from the * HttpServletRequest. * * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder(javax.servlet.http.HttpServletRequest, * org.springframework.web.bind.ServletRequestDataBinder) */ @Override protected void initBinder(final HttpServletRequest request, final ServletRequestDataBinder binder) throws Exception { binder.setRequiredFields(new String[] {"description", "serviceId", "name", "allowedToProxy", "enabled", "ssoEnabled", "anonymousAccess", "evaluationOrder"}); binder.setDisallowedFields(new String[] {"id"}); binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); }
Example #24
Source File: TargetSeedsHandler.java From webcurator with Apache License 2.0 | 5 votes |
@Override public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { // Determine the necessary formats. NumberFormat nf = NumberFormat.getInstance(request.getLocale()); // Register the binders. binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, nf, true)); // to actually be able to convert Multipart instance to byte[] // we have to register a custom editor (in this case the // ByteArrayMultipartEditor binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor()); // now Spring knows how to handle multipart object and convert them }
Example #25
Source File: GroupSearchControllerTest.java From webcurator with Apache License 2.0 | 5 votes |
@Test public final void testInitBinder() { MockHttpServletRequest request = new MockHttpServletRequest(); ServletRequestDataBinder binder = new ServletRequestDataBinder(new SearchCommand(), "command"); try { testInstance.initBinder(request, binder); } catch (Exception e) { String message = e.getClass().toString() + " - " + e.getMessage(); log.debug(message); fail(message); } }
Example #26
Source File: BaseController.java From jeewx with Apache License 2.0 | 5 votes |
/** * 将前台传递过来的日期格式的字符串,自动转化为Date类型 * * @param binder */ @InitBinder public void initBinder(ServletRequestDataBinder binder) { // SimpleDateFormat dateFormat = new SimpleDateFormat( // "yyyy-MM-dd hh:mm:ss"); // binder.registerCustomEditor(Date.class, new CustomDateEditor( // dateFormat, true)); binder.registerCustomEditor(Date.class, new DateConvertEditor()); }
Example #27
Source File: TargetInstanceAnnotationHandler.java From webcurator with Apache License 2.0 | 5 votes |
public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { NumberFormat nf = NumberFormat.getInstance(request.getLocale()); binder.registerCustomEditor(java.lang.Long.class, new CustomNumberEditor(java.lang.Long.class, nf, true)); binder.registerCustomEditor(java.lang.Integer.class, new CustomNumberEditor(java.lang.Integer.class, nf, true)); binder.registerCustomEditor(java.util.Date.class, DateUtils.get().getFullDateTimeEditor(true)); }
Example #28
Source File: UifDefaultFormMethodArgumentResolver.java From rice with Educational Community License v2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { String name = UifConstants.DEFAULT_MODEL_NAME; Object attribute = null; if (mavContainer.containsAttribute(name)) { attribute = mavContainer.getModel().get(name); } WebDataBinder binder = binderFactory.createBinder(webRequest, attribute, name); if (binder.getTarget() != null) { ServletRequest servletRequest = webRequest.getNativeRequest(ServletRequest.class); ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder; servletBinder.bind(servletRequest); if (binder.getBindingResult().hasErrors()) { if (isBindExceptionRequired(parameter)) { throw new BindException(binder.getBindingResult()); } } } Map<String, Object> bindingResultModel = binder.getBindingResult().getModel(); mavContainer.removeAttributes(bindingResultModel); mavContainer.addAllAttributes(bindingResultModel); return binder.getTarget(); }
Example #29
Source File: RaptorHandlerMethodProcessor.java From raptor with Apache License 2.0 | 5 votes |
private Object bindData(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { String name = ModelFactory.getNameForParameter(parameter); Object arg = BeanUtils.instantiateClass(parameter.getParameterType()); WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name); binder.registerCustomEditor(byte[].class, this.byteArrayBase64PropertyEditor); ServletRequest servletRequest = webRequest.getNativeRequest(ServletRequest.class); ((ServletRequestDataBinder) binder).bind(servletRequest); Map<String, Object> bindingResultModel = binder.getBindingResult().getModel(); mavContainer.removeAttributes(bindingResultModel); mavContainer.addAllAttributes(bindingResultModel); return arg; }
Example #30
Source File: BaseController.java From jeecg with Apache License 2.0 | 5 votes |
/** * 将前台传递过来的日期格式的字符串,自动转化为Date类型 * * @param binder */ @InitBinder public void initBinder(ServletRequestDataBinder binder) { // SimpleDateFormat dateFormat = new SimpleDateFormat( // "yyyy-MM-dd hh:mm:ss"); // binder.registerCustomEditor(Date.class, new CustomDateEditor( // dateFormat, true)); binder.registerCustomEditor(Date.class, new DateConvertEditor()); }