SpringMVC中WebDataBinder的应用及原理

SpringMVC中WebDataBinder的应用及原理


     Controller方法的参数类型可以是基本类型,也可以是封装后的普通Java类型。若这个普通Java类型没有声明任何注解,则意味着它的每一个属性都需要到Request中去查找对应的请求参数。众所周知,无论客户端传入的是什么类型的请求参数,最终都要以字节的形式传给服务端。而服务端通过Request的getParameter方法取到的参数也都是字符串形式的结果。所以,需要有一个把字符串形式的参数转换成服务端真正需要的类型的转换工具,在spring中这个转换工具为WebDataBinder。

   WebDataBinder不需要我们自己去创建,我们只需要向它注册参数类型对应的属性编辑器PropertyEditor。PropertyEditor可以将字符串转换成其真正的数据类型,它的void setAsText(String text)方法实现数据转换的过程。

  具体的做法是,在Controller中声明一个InitBinder方法,方法中利用WebDataBinder将自己实现的或者spring自带的PropertyEditor进行注册。像下面这样:
  
    @InitBinder
    public void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, true));
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }
  
  处理没有任何注解的普通Java类型的参数解析器是ModelAttributeMethodProcessor,下面是参加解析方法的代码:
	public final Object resolveArgument(
			MethodParameter parameter, ModelAndViewContainer mavContainer,
			NativeWebRequest request, WebDataBinderFactory binderFactory)
			throws Exception {

		String name = ModelFactory.getNameForParameter(parameter);
		Object target = (mavContainer.containsAttribute(name)) ?
				mavContainer.getModel().get(name) : createAttribute(name, parameter, binderFactory, request);

		WebDataBinder binder = binderFactory.createBinder(request, target, name);
		if (binder.getTarget() != null) {
			bindRequestParameters(binder, request);
			validateIfApplicable(binder, parameter);
			if (binder.getBindingResult().hasErrors()) {
				if (isBindExceptionRequired(binder, parameter)) {
					throw new BindException(binder.getBindingResult());
				}
			}
		}

		mavContainer.addAllAttributes(binder.getBindingResult().getModel());
		return binder.getTarget();
	}

  每次请求到来后的参数解析都会利用WebDataBinderFactory创建一个binder对象,然后从这个binder中取得最终解析好的参数对象。WebDataBinderFactory是在InvocableHandlerMethod中定义的,即不同的Controller方法有着不同的WebDataBinderFactory。其实创建binder的同时还对binder进行了初始化,这个初始化过程就会执行Controller中的InitBinder方法。InitBinderDataBinderFactory实现了初始化binder的方法:
	public void initBinder(WebDataBinder binder, NativeWebRequest request) throws Exception {
		for (InvocableHandlerMethod binderMebinderMethod thod : this.binderMethods) {
			if (isBinderMethodApplicable(binderMethod, binder)) {
				Object returnValue = binderMethod.invokeForRequest(request, null, binder);
				if (returnValue != null) {
					throw new IllegalStateException("@InitBinder methods should return void: " + binderMethod);
				}
			}
		}
	}
   
      上面方法中的binderMethods就是在Controller中定义的InitBinder方法,并且binderMethod 同Controller中的其他方法一样也是InvocableHandlerMethod。从上面的代码可以看出,InitBinder方法可以声明多个,WebDataBinderFactory初始化binder的时候会分别调用每个InitBinder方法。而我们在初始化的过程中使用了binder.registerCustomEditor,间接地向BeanWrapperImpl中注册了传入的PropertyEditor,以便在参数类型转换的时候使用。

      还记得刚才的ModelAttributeMethodProcessor解析参数时,创建binder之后调用了bindRequestParameters实现了请求参数的绑定,它的子类ServletModelAttributeMethodProcessor重写了这个方法:
	protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
		ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);
		ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
		servletBinder.bind(servletRequest);
	}
    
     不论是父类还是子类,其实都是调用了binder的bind方法。下面是ServletRequestDataBinder的bind方法
	public void bind(ServletRequest request) {
		MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request);
		MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class);
		if (multipartRequest != null) {
			bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
		}
		addBindValues(mpvs, request);
		doBind(mpvs);
	}

     这个方法跟依赖注入的过程非常相似,依赖注入是根据属性在容器中找到满足条件的对象,然后设置到当前的bean中。而上面的方法不是在容器中查找,而是从Request中获取,即把Request中的请求参数注入到binder的target中去。此时进行类型转换的就是刚刚注册的PropertyEditor,因为InitBinder方法每次都会执行,所以使用者可以在每个Controller中对相同类型的参数定义不同的参数转换方式

    经过了bindRequestParameters方法的处理,现在binder中target(即HandlerMethod的参数)已经包含了Request中的请求参数。

      那么,现在还有一个问题, InvocableHandlerMethod中的WebDataBinderFactory是如何来的呢?它的创建过程在RequestMappingHandlerAdapter(本文所有逻辑过程均假定使用RequestMappingHandlerAdapter):

	private WebDataBinderFactory getDataBinderFactory(HandlerMethod handlerMethod) throws Exception {
		Class<?> handlerType = handlerMethod.getBeanType();
		Set<Method> methods = this.dataBinderFactoryCache.get(handlerType);
		if (methods == null) {
			methods = HandlerMethodSelector.selectMethods(handlerType, INIT_BINDER_METHODS);
			this.dataBinderFactoryCache.put(handlerType, methods);
		}
		List<InvocableHandlerMethod> binderMethods = new ArrayList<InvocableHandlerMethod>();
		for (Method method : methods) {
			InvocableHandlerMethod binderMethod = new InvocableHandlerMethod(handlerMethod.getBean(), method);
			binderMethod.setHandlerMethodArgumentResolvers(this.initBinderArgumentResolvers);
			binderMethod.setDataBinderFactory(new DefaultDataBinderFactory(this.webBindingInitializer));
			binderMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer);
			binderMethods.add(binderMethod);
		}
		return createDataBinderFactory(binderMethods);
	}
      


  • 7
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
@RequestMapping注解是Spring MVC最常用的注解之一,它用于将请求映射到控制器的处理程序方法上。通过使用@RequestMapping注解,我们可以轻松地将URL映射到处理程序方法,并且可以通过使用不同的HTTP方法(例如GET、POST等)指定不同的处理程序方法。以下是一个@RequestMapping在SpringMVC应用实例: ``` @Controller @RequestMapping("/user") public class UserController { @GetMapping("/list") public String listUsers(Model model) { // 处理获取用户列表的逻辑 List<User> userList = userService.getUserList(); model.addAttribute("userList", userList); return "user/list"; } @GetMapping("/{userId}") public String getUser(@PathVariable Long userId, Model model) { // 处理获取用户信息的逻辑 User user = userService.getUserById(userId); model.addAttribute("user", user); return "user/detail"; } @PostMapping("/add") public String addUser(@ModelAttribute User user) { // 处理添加用户的逻辑 userService.addUser(user); return "redirect:/user/list"; } @PostMapping("/update/{userId}") public String updateUser(@PathVariable Long userId, @ModelAttribute User user) { // 处理更新用户信息的逻辑 userService.updateUser(userId, user); return "redirect:/user/" + userId; } @PostMapping("/delete/{userId}") public String deleteUser(@PathVariable Long userId) { // 处理删除用户的逻辑 userService.deleteUser(userId); return "redirect:/user/list"; } } ``` 上面的示例,我们使用@Controller注解来定义一个控制器类,并使用@RequestMapping注解来指定请求的URL路径。例如,我们使用@GetMapping注解将请求映射到listUsers()方法,这个方法返回一个包含用户列表的模型,并将其渲染到名为“user/list”的视图。类似地,我们使用@PostMapping注解将请求映射到addUser()方法,这个方法处理添加用户的逻辑,并将其重定向到用户列表页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值