- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我对扩展抽象 Controller 的现有 Controller 做了一些更改
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
@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 中的作用是什么?提前谢谢你
最佳答案
WebDataBinder 用于将表单字段填充到 bean 上。 init binder 方法初始化 WebDataBinder 并在其上注册特定的处理程序等。
当在服务器端读取表单字段时,最好是按照它们对应的类型来读取它们,而不是作为字符串来读取。例如,“2016 年 3 月 21 日”读作 Java 的 Date 类型比读作 String 更好。这可能涉及某些验证等。例如,像 03/21/2016 这样的日期在美国可能有效,但在其他一些国家则无效。因此,您可以使用 DataBinder 注册编辑器、 validator 等。请看:validators in InitBinder和 DataBinder doc
在这种情况下,@InitBinder 方法初始化 Binder 并注册上下文(区域设置)特定的处理程序、编辑器等。因此,当传入请求有日期时,它将在映射到 java bean 之前由 CustomDateEditor 处理。或者法国本地货币的处理方式与其他本地货币不同 - 小数点分隔符。
init binder 在抽象 Controller 中,因为这样其他 Controller 可以重用功能,而不必重写特定于语言环境的处理程序。
希望这对您有所帮助。
关于java - 在这种情况下,@InitBinder 和 WebDataBinder 的作用是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41035966/
我正在尝试在 spring 的 MVC WebDataBinder 中注册自定义日期编辑器,以使 spring 解析我的自定义日期格式(准确地说,它是 ISO 格式)。我通过实现 CustomWebB
Spring数据绑定之DataBinder篇—01 WebDataBinder 上一篇我们对DataBinder的源码进行了详细的分析,下面我们对DataBinder的实现子类来做一下具体分析: 通过
我扩展 public class RenamingProcessor extends ServletModelAttributeMethodProcessor { @Override
我对扩展抽象 Controller 的现有 Controller 做了一些更改 public abstract class AbstractWizardController { private
我正在尝试将 Spring 验证与 swagger-codegen 生成的 Controller 接口(interface)一起使用。 swagger 代码生成为 Controller 提供了一个抽象
我编写了 WebDataBinder 的自定义实现。将来我想增强它,以便它在类本身上查找注释,并确定它是否应该将数据绑定(bind)到它。 如何将此类注入(inject)到 Spring 上下文中以代
我是一名优秀的程序员,十分优秀!