, , Thymeleaf . null, Thymeleaf, .
Thymeleaf, , Introspector, , , a BeanInfo. - bean, BeanInfo, . SimpleBeanInfo ( getPropertyDescriptors).
:
public class InventoryItemBeanInfo extends SimpleBeanInfo {
@Override
public PropertyDescriptor[] getPropertyDescriptors() {
try {
PropertyDescriptor id = new PropertyDescriptor("id", InventoryItem.class);
PropertyDescriptor[] descriptors = {id};
return descriptors;
} catch (IntrospectionException e) {
throw new Error(e.toString());
}
}
}
, Player, .
, , , InventoryItem bean, BeanInfo .
, . , , .
, , . HiddenProperty:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface HiddenProperty {}
BeanInfo , bean . , , :
public class HiddenPropertyAwareBeanInfo extends SimpleBeanInfo {
private Class<?> beanClass;
private PropertyDescriptor[] descriptors;
public HiddenPropertyAwareBeanInfo(Class<?> beanClass) {
this.beanClass = beanClass;
}
@Override
public PropertyDescriptor[] getPropertyDescriptors() {
if(descriptors != null) {
return descriptors;
}
Method[] methodList = beanClass.getMethods();
List<PropertyDescriptor> propDescriptors = new ArrayList<>();
for(Method m : methodList) {
if(Modifier.isStatic(m.getModifiers())) {
continue;
}
try {
if(m.getParameterCount() == 0 && !m.isAnnotationPresent(HiddenProperty.class)) {
if(m.getName().startsWith("get")) {
propDescriptors.add(new PropertyDescriptor(m.getName().substring(3), beanClass));
} else if(m.getName().startsWith("is")) {
propDescriptors.add(new PropertyDescriptor(m.getName().substring(2), beanClass));
}
}
} catch(IntrospectionException ex) {
continue;
}
}
descriptors = new PropertyDescriptor[propDescriptors.size()];
return propDescriptors.toArray(descriptors);
}
}
BeanInfo, :
public class InventoryItemBeanInfo extends HiddenPropertyAwareBeanInfo {
public InventoryItemBeanInfo() {
super(InventoryItem.class);
}
}
, , :
@Entity
@Table(name = "inventory_item")
public class InventoryItem {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@ManyToOne
@JoinColumn(name="id_player")
private Player player;
public InventoryItem() {
}
@HiddenProperty
public Player getPlayer() {
return this.player;
}
}
. , , Player Thymeleaf.
, , , 100% Java Beans, . Introspector , , .
, , , Thymeleaf. - , .