【问题标题】:What is the role of @InitBinder and the WebDataBinder in this case在这种情况下@InitBinder 和 WebDataBinder 的作用是什么
【发布时间】:2016-12-08 09:30:07
【问题描述】:

我对扩展抽象控制器的现有控制器进行了一些更改

public abstract class AbstractWizardController {


    private WizardDescriptor descriptor;


    private transient String dispacherUri;


    @InitBinder
    public void initBinder(final WebDataBinder binder) {
        final Locale locale = LocaleContextHolder.getLocale();
        final DecimalFormatSymbols decimalSymbol = new DecimalFormatSymbols(locale);
        if (Locale.FRENCH.equals(locale)) {
            decimalSymbol.setDecimalSeparator(WebConstants.Caracteres.VIRGULE);
            decimalSymbol.setGroupingSeparator(WebConstants.Caracteres.ESPACE);
        } else {
            decimalSymbol.setDecimalSeparator(WebConstants.Caracteres.POINT);
            decimalSymbol.setGroupingSeparator(WebConstants.Caracteres.VIRGULE);
        }
        final DecimalFormat decimalFormat = new DecimalFormat(PseConstants.Pattern.MONTANT_PATTERN, decimalSymbol);
        decimalFormat.setMinimumFractionDigits(NumbersEnum.DEUX.getNumber());
        // Editeur personnalisé pour les montants
        binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, decimalFormat, true));

        // Editeur personnalisé pour les dates
        final SimpleDateFormat formatDate = Locale.FRENCH.equals(locale) ? new SimpleDateFormat(WebConstants.DatesFormats.DATE_LOCAL_FRANCAIS)
                : new SimpleDateFormat(WebConstants.DatesFormats.DATE_LOCAL_ANGLAIS);
        formatDate.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(formatDate, true));
    }
 }

这是我的控制器

@Controller
@RequestMapping(WebConstants.View.LETTRE_MANDAT)
public class EditerMandatController extends AbstractWizardController {

    /** Logger */
    private static final Logger LOGGER = LoggerFactory.getLogger(EditerMandatController.class);


    private transient IEditerLettreService editerLettreService;

    /**
     * Constructeur
     */
    public EditerMandatController() {
        setDispacherUri(WebConstants.View.LETTRE_MANDAT);
    }


    @RequestMapping(value = WebConstants.View.EDITER_LETTRE_MANDAT, method = RequestMethod.POST)
    public String editerLettreMandat(final HttpServletRequest req, final HttpServletResponse res,
            @ModelAttribute(WebConstants.Path.FORM) final LettreMandatBean lettreMandat, final org.springframework.ui.Model model) throws AppTechnicalException {

        final String idMandataire = WebUtilities.getIdMandataire(req);
        lettreMandat.setIdMandataire(idMandataire);

        final String lettreMandatCookie = JsonUtils.parseObjectJson(lettreMandat);

        if (StringUtils.isNotBlank(lettreMandatCookie)) {
            final Cookie cookie = new Cookie(WebConstants.Others.LETTRE_MANDAT_COOKIE + idMandataire, Base64.encodeBytes(lettreMandatCookie.getBytes()));
            cookie.setSecure(true);
            res.addCookie(cookie);
        }
        try {
            editerLettreService.editerLettre(req, res, lettreMandat);

        } catch (final AppTechnicalException e) {
            LOGGER.error(WebConstants.Messages.MESSAGE_INCIDENT_TECHNIQUE, e);
            addError(model, WebConstants.Messages.MESSAGE_INCIDENT_TECHNIQUE);
        }
        return WebConstants.View.PAGE_LETTRE_MANDAT;
    }

}

我的问题是 @InitBinderWebDataBinder 在抽象 Controller 中的作用是什么?提前谢谢你

【问题讨论】:

  • 更新了我的答案,因为我不确定你是否知道 DataBinder

标签: java spring-mvc


【解决方案1】:

WebDataBinder 用于将表单字段填充到 bean 上。一个 init binder 方法初始化 WebDataBinder 并在其上注册特定的处理程序等。

在服务器端读取表单字段时,最好按照对应的类型读取它们,而不是按照字符串读取。例如,“2016 年 3 月 21 日”作为 Java 的 Date 类型比 String 更好。这可能涉及某些验证等。例如,像 03/21/2016 这样的日期可能在美国有效,但在其他一些国家/地区无效。因此,您可以使用 DataBinder 注册编辑器、验证器等。请看:validators in InitBinderDataBinder doc

在这种情况下,@InitBinder 方法初始化活页夹并注册上下文(语言环境)特定的处理程序、编辑器等。 因此,当传入请求具有日期时,它将由 CustomDateEditor 处理,然后再映射到 java bean。或者法国当地货币的处理方式与其他当地人不同 - 小数分隔符。

初始化绑定器位于抽象控制器中,因为这样其他控制器可以重用该功能,而不必重写特定于语言环境的处理程序。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 2017-03-28
    • 2016-08-26
    相关资源
    最近更新 更多