【发布时间】: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;
}
}
我的问题是 @InitBinder 和 WebDataBinder 在抽象 Controller 中的作用是什么?提前谢谢你
【问题讨论】:
-
更新了我的答案,因为我不确定你是否知道 DataBinder
标签: java spring-mvc