2016-12-08 36 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聯編程序位於抽象控制器中,因爲其他控制器可以重用該功能並且不必重寫特定於語言環境的處理程序。

希望這會有所幫助。

相關問題