2016-12-08 35 views
3

我做继承抽象控制器什么是@InitBinder在这种情况下的作用和WebDataBinder

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?预先感谢您

+0

更新了我的答案,因为我不确定您是否知道DataBinder – Atul

回答

3

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

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

在这种情况下@InitBinder方法初始化粘合剂和寄存器上下文(区域)具体处理程序,编辑器等 因此,当进入的请求具有它将被进行处理的日期CustomDateEditor映射到java bean之前。或者法国本地货币将以与其他本地货币不同的方式进行处理 - 小数点分隔符。

init联编程序位于抽象控制器中,因为其他控制器可以重用该功能并且不必重写特定于语言环境的处理程序。

希望这会有所帮助。

相关问题