当前位置: 首页 > wzjs >正文

做网站能赚钱吗 知乎佛山做网站建设

做网站能赚钱吗 知乎,佛山做网站建设,公司创建一个网站多少钱,做淘客网站要备案在使用 Spring MVC 开发 Web 应用时,我们经常需要处理从 HTTP 请求(如 URL 参数、表单数据)到 Controller 方法参数的自动转换。这就是 Spring 的数据绑定 (Data Binding) 机制。虽然 Spring 提供了很多默认的类型转换器(比如字符…

在使用 Spring MVC 开发 Web 应用时,我们经常需要处理从 HTTP 请求(如 URL 参数、表单数据)到 Controller 方法参数的自动转换。这就是 Spring 的数据绑定 (Data Binding) 机制。虽然 Spring 提供了很多默认的类型转换器(比如字符串转数字、布尔值等),但在实际开发中,我们常常会遇到默认转换无法满足需求的情况,尤其是处理日期格式或需要自定义对象绑定时。

这时候,@InitBinder​ 就闪亮登场了!它提供了一个强大的钩子,允许我们在数据绑定执行前,对绑定过程进行精细化的定制。

什么是 @InitBinder?

​@InitBinder​ 是一个用在 Controller 方法上的注解。被 @InitBinder​ 注解的方法会在当前 Controller 处理请求、进行数据绑定之前被调用。这个方法通常接受一个 WebDataBinder​ 对象作为参数,WebDataBinder​ 正是执行数据绑定的核心组件。

通过 WebDataBinder​ 对象,我们可以注册自定义的编辑器 (PropertyEditor​),或者进行其他的绑定相关的设置。

基本使用方法如下:

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;
// ... 其他 import@Controller // 或者 @RestController
public class YourController {@InitBinderpublic void initBinder(WebDataBinder binder) {// 在这里进行自定义绑定设置System.out.println("Initializing WebDataBinder for YourController!");// ... 例如注册自定义编辑器 ...}// ... 其他 @RequestMapping 方法 ...
}

常见应用场景一:统一处理日期格式

这是 @InitBinder​ 最常见的应用场景之一。我们知道,前端传过来的日期字符串格式五花八门,而 java.util.Date​ 或 java.time.*​ 类型的默认解析行为往往不靠谱,且可能受服务器区域设置影响。

与其在每个接收日期的 Controller 方法参数前都加上 @DateTimeFormat(pattern = "...")​,我们可以使用 @InitBinder​ 在 Controller 级别统一处理:

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RestController;
import java.beans.PropertyEditorSupport;
import java.util.Date;
// 假设你有一个 DateUtils 工具类
import com.yourcompany.common.utils.DateUtils;@RestController
public class UserController {@InitBinderpublic void initBinder(WebDataBinder binder) {// 注册针对 Date.class 类型的自定义编辑器binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {@Overridepublic void setAsText(String text) throws IllegalArgumentException {// 当需要将 String 转为 Date 时,调用此方法// 使用自定义的 DateUtils 进行解析,它可以支持多种格式Date parsedDate = DateUtils.parseDate(text);if (parsedDate == null && text != null && !text.isEmpty()) {// 如果解析失败,可以抛出异常或记录日志throw new IllegalArgumentException("Could not parse date: " + text);}// 将解析后的 Date 对象设置回去setValue(parsedDate);}// (可选) 你还可以重写 getAsText 方法来控制 Date 对象如何转回 String// @Override// public String getAsText() {//     Date value = (Date) getValue();//     return (value != null ? new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value) : "");// }});}@GetMapping("/users/by-creation-date")public User findUsers(Date creationDate) { // 无需 @DateTimeFormat// Spring 会自动使用上面注册的 CustomEditor 来解析 creationDate 参数// ...}
}

注意: 这个 @InitBinder​ 只对 URL 参数、表单数据等生效,不影响 @RequestBody​ 接收的 JSON 数据中的日期解析(那个由 Jackson 控制,需要 @JsonFormat​)。

常见应用场景二:自动去除字符串前后空格

有时候,我们希望所有传入的字符串参数都能自动去除首尾空格。@InitBinder​ 配合 Spring 提供的 StringTrimmerEditor​ 可以轻松实现:

import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ProductController {@InitBinderpublic void initBinder(WebDataBinder binder) {// 创建 StringTrimmerEditor,true 表示将空字符串转换为 nullStringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true);// 注册给 String.class 类型binder.registerCustomEditor(String.class, stringTrimmerEditor);}@GetMapping("/products")public Product findProduct(String productName) {// 传入的 productName 会被自动 trim// 如果传入 "  My Product  ",这里接收到的会是 "My Product"// 如果传入 "   " 或 "",这里接收到的会是 null (因为构造函数是 true)// ...}
}

@InitBinder 的作用域与全局配置

默认情况下,写在某个 Controller 里的 @InitBinder​ 方法只对当前这个 Controller 生效。如果你希望某些绑定规则(比如日期处理、字符串 trim)应用到所有或部分 Controller,该怎么办?

答案是使用 @ControllerAdvice​!你可以创建一个带有 @ControllerAdvice​ 注解的类,并在其中定义 @InitBinder​ 方法。这样,这个 @InitBinder​ 的逻辑就会应用到所有被 @ControllerAdvice​ 覆盖的 Controller 上。

import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;
import java.beans.PropertyEditorSupport;
import java.util.Date;
import com.yourcompany.common.utils.DateUtils;@ControllerAdvice // 应用到所有 Controller (也可以指定范围)
public class GlobalBindingInitializer {@InitBinderpublic void initBinder(WebDataBinder binder) {// 全局注册 Date 编辑器binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {@Overridepublic void setAsText(String text) {setValue(DateUtils.parseDate(text));}});// 全局注册 String Trimmerbinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));}
}

@InitBinder vs. 格式化注解 (@DateTimeFormat, @NumberFormat)

  • ​@DateTimeFormat​ 和 @NumberFormat​ 等注解是字段/参数级别的,用于标准、明确的格式化需求,使用起来更简单直接。

  • ​@InitBinder​ 是Controller 级别或全局级别的,提供了更强大、更底层的定制能力。适用于:

    • 需要统一处理某种类型(如 Date)的转换规则。
    • 需要实现更复杂的绑定逻辑(比如根据 ID 从数据库加载对象)。
    • 处理 Spring 没有内置格式化注解支持的类型。

一般建议优先使用格式化注解,只有当注解无法满足需求或需要在 Controller/全局层面统一处理时,才考虑使用 @InitBinder​。

注意事项

  • ​@InitBinder​ 不影响 @RequestBody​ 绑定的 JSON 数据。JSON 的序列化和反序列化由 Jackson(或其他 JSON 库)控制,需要使用 @JsonFormat​ 等 Jackson 注解。
  • 过度使用 @InitBinder​,特别是进行复杂的逻辑,可能会让数据转换的过程变得不那么直观。
  • 全局 @InitBinder​ (通过 @ControllerAdvice​) 需要谨慎使用,确保不会意外影响到所有 Controller。

总结

​@InitBinder​ 是 Spring MVC 提供的一个非常有用的特性,它允许我们深入数据绑定的核心,定制请求参数到方法参数的转换逻辑。通过合理使用 @InitBinder​,我们可以优雅地解决默认类型转换无法满足的场景,特别是统一处理日期格式和字符串修整等常见需求,从而提高代码的健壮性和可维护性。

http://www.dtcms.com/wzjs/224169.html

相关文章:

  • 别人的网站是怎么做的百度图片收录提交入口
  • 广东建设信息公开网站百度推广投诉人工电话
  • 生成静态页面网站源码网站优化 seo和sem
  • 公司网站建设案例学seo需要多久
  • 做网站聊城中国工商业联合会
  • 小米官方网站开发版郑州网络推广团队
  • 卖网站赚钱最近新闻
  • 南通城乡建设局网站招聘seo外链工具
  • 百度公司网站怎么做百度推广助手怎么用
  • 奉节做网站竞价推广方案
  • 软件开发工程师前景宁波seo软件免费课程
  • 单页网站 产品放哪外链代发
  • 塑胶包装东莞网站建设中牟网络推广外包
  • 广州网站建设建航科技公司关键字c语言
  • 怎样做卖活网站优化网站推广教程整站
  • 公司网站免费网站免费我也要投放广告
  • 四省网站建设湖南网站seo地址
  • 微信如何做收费视频网站亚马逊关键词优化软件
  • 网站关键词分割优化资源配置
  • 石家庄建立网站的公司东莞seo网络推广专
  • 昆明网站开发建怎样推广小程序平台
  • 贵州安顺网站建设一键建站免费
  • b2c网站设计关键词提取工具app
  • 移动端网站模板怎么做的手机优化游戏性能的软件
  • 外贸网站建站系统培训网站建设
  • 游戏开发工作室aso关键词优化计划
  • 做网站为什么每年都要续费培训机构退费法律规定
  • 网站建设怎样才能吸引顾客做百度推广一个月多少钱
  • 网站网络营销怎么做优化关键词软件
  • 如何选择大连网站建设武汉大学人民医院院长