从技术上讲,问题不在于 JSF,而在于 EL。异常的包名已经暗示了这一点:javax.el.PropertyNotFoundException
. 正在使用的 EL 版本还不能识别 Java 记录。Jakarta EE 8 与 Java 8 相关联,但 Java Records 功能是在 Java 14 中引入的。理论上,它最早只会在与 Java 14 相关的 Jakarta EE 版本相关的 EL 版本中得到本地支持。但即使这样也不太可能因为 Java Records 仅作为“预览功能”提供(因此默认情况下根本不启用)。
回到您的具体问题,#{car.randomId()}
在 WildFly 21 上使用方法表达式语法对我来说确实有效。无论如何,EL 解析始终可以使用自定义ELResolver
. 这是一个启动示例,它根据作为属性检查Class#isRecord()
并收集可用字段:Class#getRecordComponents()
public class RecordELResolver extends ELResolver {
private static final Map<Class<?>, Map<String, PropertyDescriptor>> RECORD_PROPERTY_DESCRIPTOR_CACHE = new ConcurrentHashMap<>();
private static boolean isRecord(Object base) {
return base != null && base.getClass().isRecord();
}
private static Map<String, PropertyDescriptor> getRecordPropertyDescriptors(Object base) {
return RECORD_PROPERTY_DESCRIPTOR_CACHE
.computeIfAbsent(base.getClass(), clazz -> Arrays
.stream(clazz.getRecordComponents())
.collect(Collectors
.toMap(RecordComponent::getName, recordComponent -> {
try {
return new PropertyDescriptor(recordComponent.getName(), recordComponent.getAccessor(), null);
}
catch (IntrospectionException e) {
throw new IllegalStateException(e);
}
})));
}
private static PropertyDescriptor getRecordPropertyDescriptor(Object base, Object property) {
PropertyDescriptor descriptor = getRecordPropertyDescriptors(base).get(property);
if (descriptor == null) {
throw new PropertyNotFoundException("The record '" + base.getClass().getName() + "' does not have the field '" + property + "'.");
}
return descriptor;
}
@Override
public Object getValue(ELContext context, Object base, Object property) {
if (!isRecord(base) || property == null) {
return null;
}
PropertyDescriptor descriptor = getRecordPropertyDescriptor(base, property);
try {
Object value = descriptor.getReadMethod().invoke(base);
context.setPropertyResolved(base, property);
return value;
}
catch (Exception e) {
throw new ELException(e);
}
}
@Override
public Class<?> getType(ELContext context, Object base, Object property) {
if (!isRecord(base) || property == null) {
return null;
}
PropertyDescriptor descriptor = getRecordPropertyDescriptor(base, property);
context.setPropertyResolved(true);
return descriptor.getPropertyType();
}
@Override
public Class<?> getCommonPropertyType(ELContext context, Object base) {
if (!isRecord(base)) {
return null;
}
return String.class;
}
@Override
public boolean isReadOnly(ELContext context, Object base, Object property) {
if (!isRecord(base)) {
return false;
}
getRecordPropertyDescriptor(base, property); // Forces PropertyNotFoundException if necessary.
context.setPropertyResolved(true);
return true;
}
@Override
public void setValue(ELContext context, Object base, Object property, Object value) {
if (!isRecord(base)) {
return;
}
throw new PropertyNotWritableException("Java Records are immutable");
}
@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
if (!isRecord(base)) {
return null;
}
Map rawDescriptors = getRecordPropertyDescriptors(base);
return rawDescriptors.values().iterator();
}
}
为了让它工作,注册它faces-config.xml
如下:
<application>
<el-resolver>com.example.RecordELResolver</el-resolver>
</application>
真正的工作是在getValue()
方法中完成的。它基本上定位java.lang.reflect.Method
Java Record 的代表访问器并调用它。
也就是说,Java Record 不适合替代完全有价值的 JavaBean,主要是因为 Java Records 是不可变的。因此它们不能用作真实(JPA)实体,因为它们应该是可变的。Java 记录最多可用作 DTO。