在我的应用程序中,我有一个这样的课程:
public class DateField {
private String pattern;
private Date date;
public DateField(Date date, String pattern) {
this.date = date;
this.pattern = pattern;
}
public Date getDate() {
return this.date;
}
public String getPattern() {
return this.pattern;
}
}
pattern
由用户定义,他们可以使用管理界面随时更改它,而无需重新启动应用程序。我已经阅读了spring documentation on formatters,我似乎无法根据给定的date
找到pattern
弹簧格式的方法,因为模式已硬编码到注释。我考虑过实现自己的Formatter
,但我只能实施print
方法,因为我没有pattern
方法中的parse
:
public class DateFieldFormatter implements Formatter<DateField> {
@Override
public String print(DateField dateField,
Locale locale) {
if (dateField == null) {
return "";
}
return new SimpleDateFormat(dateField.getPattern()).format(dateField.getDate());
}
@Override
public DateField parse(String dateString,
Locale locale) throws ParseException {
// I don't know what the pattern is here.
return null;
}
}
似乎让用户能够更改日期格式将是一个非常常见的用例,所以我必须在这里遗漏一些东西。我如何使这个工作?
谢谢!
答案 0 :(得分:0)
如果模式取决于用户设置,则应将其存储在除日期字段之外的其他对象中。恕我直言,最好有一个UserSettings
类,包含这个日期模式,UserSettingService
能够返回给定从请求中提取Principal
的设置或提取的用户ID来自Session
。
如果您注册Request
,则可以通过RequestContextHolder.currentContextAttributes()
从应用程序中的任意位置访问当前RequestContextListener
。您可以通过以下方式访问请求:
ServletRequestAttributes requestAttributes =
(ServletRequestAttributes ) RequestContextHolder.currentRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
如果您只需要会话属性:
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
String userId = requestAttributes.getAttribute("userid", RequestAttributes.SCOPE_SESSION);
通过您的示例,您可以这样使用它:
注入了实用程序bean的格式化程序
public class DateFieldFormatter implements Formatter<DateField> {
@Autowired
PatternService patternService;
@Autowired
UserExtractor userExtractor;
@Override
public String print(DateField dateField,
Locale locale) {
if (dateField == null) {
return "";
}
return new SimpleDateFormat(dateField.getPattern()).format(dateField.getDate());
// or return new SimpleDateFormat(getPattern()).format(dateField.getDate());
}
@Override
public DateField parse(String dateString,
Locale locale) throws ParseException {
String pattern = getPattern();
DateField dateField = new DateField();
dateField.setPattern(pattern);
Date date = new SimpleDateFormat(pattern).parse(dateString);
dateField.setDate(date);
return dateField;
}
private String getPattern() {
return patternService.getPattern(userExtractor.getCurrentUserId());
}
}
一些接口
interface PatternService {
String getPattern(String userID);
}
interface UserExtractor {
String getCurrentUserId();
}
从Session属性获取userId或从Request
中获取Principal的示例实现static class SessionUserExtractor implements UserExtractor {
@Override
public String getCurrentUserId() {
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
return (String) requestAttributes.getAttribute("userid", RequestAttributes.SCOPE_SESSION);
}
}
static class PrincipalUserExtractor implements UserExtractor {
@Override
public String getCurrentUserId() {
ServletRequestAttributes requestAttributes =
(ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
return request.getUserPrincipal().getName();
}
}