org.springframework.web.bind.support.WebRequestDataBinder.bind()方法的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(12.4k)|赞(0)|评价(0)|浏览(165)

本文整理了Java中org.springframework.web.bind.support.WebRequestDataBinder.bind()方法的一些代码示例,展示了WebRequestDataBinder.bind()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WebRequestDataBinder.bind()方法的具体详情如下:
包路径:org.springframework.web.bind.support.WebRequestDataBinder
类名称:WebRequestDataBinder
方法名:bind

WebRequestDataBinder.bind介绍

[英]Bind the parameters of the given request to this binder's target, also binding multipart files in case of a multipart request.

This call can create field errors, representing basic binding errors like a required field (code "required"), or type mismatch between value and bean property (code "typeMismatch").

Multipart files are bound via their parameter name, just like normal HTTP parameters: i.e. "uploadedFile" to an "uploadedFile" bean property, invoking a "setUploadedFile" setter method.

The type of the target property for a multipart file can be Part, MultipartFile, byte[], or String. The latter two receive the contents of the uploaded file; all metadata like original file name, content type, etc are lost in those cases.
[中]将给定请求的参数绑定到此绑定器的目标,并在多部分请求的情况下绑定多部分文件。
这个调用可以创建字段错误,表示基本的绑定错误,比如必填字段(代码“required”),或者值和bean属性之间的类型不匹配(代码“typeMismatch”)。
多部分文件通过其参数名绑定,就像普通的HTTP参数一样:即“uploadedFile”到“uploadedFile”bean属性,调用“setUploadedFile”setter方法。
多部分文件的目标属性的类型可以是Part、MultipartFile、byte[]或String。后两个接收上传文件的内容;在这些情况下,原始文件名、内容类型等所有元数据都会丢失。

代码示例

代码示例来源:origin: spring-projects/spring-framework

  1. /**
  2. * Extension point to bind the request to the target object.
  3. * @param binder the data binder instance to use for the binding
  4. * @param request the current request
  5. */
  6. protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
  7. ((WebRequestDataBinder) binder).bind(request);
  8. }

代码示例来源:origin: org.springframework/spring-web

  1. /**
  2. * Extension point to bind the request to the target object.
  3. * @param binder the data binder instance to use for the binding
  4. * @param request the current request
  5. */
  6. protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
  7. ((WebRequestDataBinder) binder).bind(request);
  8. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Override
  2. public void service(HttpServletRequest request, HttpServletResponse response) {
  3. WebRequestDataBinder binder = new WebRequestDataBinder(bean);
  4. ServletWebRequest webRequest = new ServletWebRequest(request, response);
  5. binder.bind(webRequest);
  6. response.setStatus(HttpServletResponse.SC_OK);
  7. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testFieldPrefixCausesFieldReset() throws Exception {
  3. TestBean target = new TestBean();
  4. WebRequestDataBinder binder = new WebRequestDataBinder(target);
  5. MockHttpServletRequest request = new MockHttpServletRequest();
  6. request.addParameter("_postProcessed", "visible");
  7. request.addParameter("postProcessed", "on");
  8. binder.bind(new ServletWebRequest(request));
  9. assertTrue(target.isPostProcessed());
  10. request.removeParameter("postProcessed");
  11. binder.bind(new ServletWebRequest(request));
  12. assertFalse(target.isPostProcessed());
  13. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testFieldDefault() throws Exception {
  3. TestBean target = new TestBean();
  4. WebRequestDataBinder binder = new WebRequestDataBinder(target);
  5. MockHttpServletRequest request = new MockHttpServletRequest();
  6. request.addParameter("!postProcessed", "off");
  7. request.addParameter("postProcessed", "on");
  8. binder.bind(new ServletWebRequest(request));
  9. assertTrue(target.isPostProcessed());
  10. request.removeParameter("postProcessed");
  11. binder.bind(new ServletWebRequest(request));
  12. assertFalse(target.isPostProcessed());
  13. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testFieldDefaultNonBoolean() throws Exception {
  3. TestBean target = new TestBean();
  4. WebRequestDataBinder binder = new WebRequestDataBinder(target);
  5. MockHttpServletRequest request = new MockHttpServletRequest();
  6. request.addParameter("!name", "anonymous");
  7. request.addParameter("name", "Scott");
  8. binder.bind(new ServletWebRequest(request));
  9. assertEquals("Scott", target.getName());
  10. request.removeParameter("name");
  11. binder.bind(new ServletWebRequest(request));
  12. assertEquals("anonymous", target.getName());
  13. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testFieldDefaultPreemptsFieldMarker() throws Exception {
  3. TestBean target = new TestBean();
  4. WebRequestDataBinder binder = new WebRequestDataBinder(target);
  5. MockHttpServletRequest request = new MockHttpServletRequest();
  6. request.addParameter("!postProcessed", "on");
  7. request.addParameter("_postProcessed", "visible");
  8. request.addParameter("postProcessed", "on");
  9. binder.bind(new ServletWebRequest(request));
  10. assertTrue(target.isPostProcessed());
  11. request.removeParameter("postProcessed");
  12. binder.bind(new ServletWebRequest(request));
  13. assertTrue(target.isPostProcessed());
  14. request.removeParameter("!postProcessed");
  15. binder.bind(new ServletWebRequest(request));
  16. assertFalse(target.isPostProcessed());
  17. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testEnumBinding() {
  3. EnumHolder target = new EnumHolder();
  4. WebRequestDataBinder binder = new WebRequestDataBinder(target);
  5. MockHttpServletRequest request = new MockHttpServletRequest();
  6. request.addParameter("myEnum", "FOO");
  7. binder.bind(new ServletWebRequest(request));
  8. assertEquals(MyEnum.FOO, target.getMyEnum());
  9. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testFieldPrefixCausesFieldResetWithIgnoreUnknownFields() throws Exception {
  3. TestBean target = new TestBean();
  4. WebRequestDataBinder binder = new WebRequestDataBinder(target);
  5. binder.setIgnoreUnknownFields(false);
  6. MockHttpServletRequest request = new MockHttpServletRequest();
  7. request.addParameter("_postProcessed", "visible");
  8. request.addParameter("postProcessed", "on");
  9. binder.bind(new ServletWebRequest(request));
  10. assertTrue(target.isPostProcessed());
  11. request.removeParameter("postProcessed");
  12. binder.bind(new ServletWebRequest(request));
  13. assertFalse(target.isPostProcessed());
  14. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testMultipartFileAsString() {
  3. TestBean target = new TestBean();
  4. WebRequestDataBinder binder = new WebRequestDataBinder(target);
  5. binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
  6. MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
  7. request.addFile(new MockMultipartFile("name", "Juergen".getBytes()));
  8. binder.bind(new ServletWebRequest(request));
  9. assertEquals("Juergen", target.getName());
  10. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testWithCommaSeparatedStringArray() throws Exception {
  3. TestBean target = new TestBean();
  4. WebRequestDataBinder binder = new WebRequestDataBinder(target);
  5. MockHttpServletRequest request = new MockHttpServletRequest();
  6. request.addParameter("stringArray", "bar");
  7. request.addParameter("stringArray", "abc");
  8. request.addParameter("stringArray", "123,def");
  9. binder.bind(new ServletWebRequest(request));
  10. assertEquals("Expected all three items to be bound", 3, target.getStringArray().length);
  11. request.removeParameter("stringArray");
  12. request.addParameter("stringArray", "123,def");
  13. binder.bind(new ServletWebRequest(request));
  14. assertEquals("Expected only 1 item to be bound", 1, target.getStringArray().length);
  15. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testMultipartFileAsStringArray() {
  3. TestBean target = new TestBean();
  4. WebRequestDataBinder binder = new WebRequestDataBinder(target);
  5. binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
  6. MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
  7. request.addFile(new MockMultipartFile("stringArray", "Juergen".getBytes()));
  8. binder.bind(new ServletWebRequest(request));
  9. assertEquals(1, target.getStringArray().length);
  10. assertEquals("Juergen", target.getStringArray()[0]);
  11. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testFieldDefaultWithNestedProperty() throws Exception {
  3. TestBean target = new TestBean();
  4. target.setSpouse(new TestBean());
  5. WebRequestDataBinder binder = new WebRequestDataBinder(target);
  6. MockHttpServletRequest request = new MockHttpServletRequest();
  7. request.addParameter("!spouse.postProcessed", "on");
  8. request.addParameter("_spouse.postProcessed", "visible");
  9. request.addParameter("spouse.postProcessed", "on");
  10. binder.bind(new ServletWebRequest(request));
  11. assertTrue(((TestBean) target.getSpouse()).isPostProcessed());
  12. request.removeParameter("spouse.postProcessed");
  13. binder.bind(new ServletWebRequest(request));
  14. assertTrue(((TestBean) target.getSpouse()).isPostProcessed());
  15. request.removeParameter("!spouse.postProcessed");
  16. binder.bind(new ServletWebRequest(request));
  17. assertFalse(((TestBean) target.getSpouse()).isPostProcessed());
  18. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testMultipartFilesAsStringArray() {
  3. TestBean target = new TestBean();
  4. WebRequestDataBinder binder = new WebRequestDataBinder(target);
  5. binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
  6. MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
  7. request.addFile(new MockMultipartFile("stringArray", "Juergen".getBytes()));
  8. request.addFile(new MockMultipartFile("stringArray", "Eva".getBytes()));
  9. binder.bind(new ServletWebRequest(request));
  10. assertEquals(2, target.getStringArray().length);
  11. assertEquals("Juergen", target.getStringArray()[0]);
  12. assertEquals("Eva", target.getStringArray()[1]);
  13. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testBindingWithNestedObjectCreationThroughAutoGrow() throws Exception {
  3. TestBean tb = new TestBeanWithConcreteSpouse();
  4. WebRequestDataBinder binder = new WebRequestDataBinder(tb, "person");
  5. binder.setIgnoreUnknownFields(false);
  6. MockHttpServletRequest request = new MockHttpServletRequest();
  7. request.addParameter("concreteSpouse.name", "test");
  8. binder.bind(new ServletWebRequest(request));
  9. assertNotNull(tb.getSpouse());
  10. assertEquals("test", tb.getSpouse().getName());
  11. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testCollectionFieldsDefault() throws Exception {
  3. TestBean target = new TestBean();
  4. target.setSomeSet(null);
  5. target.setSomeList(null);
  6. target.setSomeMap(null);
  7. WebRequestDataBinder binder = new WebRequestDataBinder(target);
  8. MockHttpServletRequest request = new MockHttpServletRequest();
  9. request.addParameter("_someSet", "visible");
  10. request.addParameter("_someList", "visible");
  11. request.addParameter("_someMap", "visible");
  12. binder.bind(new ServletWebRequest(request));
  13. assertThat(target.getSomeSet(), notNullValue());
  14. assertThat(target.getSomeSet(), isA(Set.class));
  15. assertThat(target.getSomeList(), notNullValue());
  16. assertThat(target.getSomeList(), isA(List.class));
  17. assertThat(target.getSomeMap(), notNullValue());
  18. assertThat(target.getSomeMap(), isA(Map.class));
  19. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void testBindingWithNestedObjectCreation() throws Exception {
  3. TestBean tb = new TestBean();
  4. WebRequestDataBinder binder = new WebRequestDataBinder(tb, "person");
  5. binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() {
  6. @Override
  7. public void setAsText(String text) throws IllegalArgumentException {
  8. setValue(new TestBean());
  9. }
  10. });
  11. MockHttpServletRequest request = new MockHttpServletRequest();
  12. request.addParameter("spouse", "someValue");
  13. request.addParameter("spouse.name", "test");
  14. binder.bind(new ServletWebRequest(request));
  15. assertNotNull(tb.getSpouse());
  16. assertEquals("test", tb.getSpouse().getName());
  17. }

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.spring-web

  1. /**
  2. * Extension point to bind the request to the target object.
  3. * @param binder the data binder instance to use for the binding
  4. * @param request the current request
  5. */
  6. protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
  7. ((WebRequestDataBinder) binder).bind(request);
  8. }

代码示例来源:origin: apache/servicemix-bundles

  1. /**
  2. * Extension point to bind the request to the target object.
  3. * @param binder the data binder instance to use for the binding
  4. * @param request the current request
  5. */
  6. protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
  7. ((WebRequestDataBinder) binder).bind(request);
  8. }

代码示例来源:origin: stackoverflow.com

  1. public String postAction(WebRequest webRequest) {
  2. WebRequestDataBinder binder = new WebRequestDataBinder(service, "service");
  3. binder.bind(webRequest);
  4. return "redirect:/my_test_action";
  5. }

相关文章