在Tapestry 5 Web应用程序中编辑复杂的Java对象
作者:互联网
我正在使用Tapestry 5.3.6作为Web应用程序,我希望用户使用Web表单(立即建议使用beaneditform)编辑Java类的实例(“bean”或POJO) – 但是Java要编辑的类具有相当复杂的结构.我正在寻找Tapestry 5中最简单的方法.
首先,让我们定义一些实用程序类,例如
public class ModelObject {
private URI uri;
private boolean modified;
// the usual constructors, getters and setters ...
}
public class Literal<T> extends ModelObject {
private Class<?> valueClass;
private T value;
public Literal(Class<?> valueClass) {
this.valueClass = valueClass;
}
public Literal(Class<?> valueClass, T value) {
this.valueClass = valueClass;
this.value = value;
}
// the usual getters and setters ...
}
public class Link<T extends ModelObject> extends ModelObject {
private Class<?> targetClass;
private T target;
public Link(Class<?> targetClass) {
this.targetClass = targetClass;
}
public Link(Class<?> targetClass, T target) {
this.targetClass = targetClass;
this.target = target;
}
// the usual getters and setters ...
}
现在您可以创建一些相当复杂的数据结构,例如:
public class HumanBeing extends ModelObject {
private Literal<String> name;
// ... other stuff
public HumanBeing() {
name = new Literal<String>(String.class);
}
// the usual getters and setters ...
}
public class Project extends ModelObject {
private Literal<String> projectName;
private Literal<Date> startDate;
private Literal<Date> endDate;
private Literal<Integer> someCounter;
private Link<HumanBeing> projectLeader;
private Link<HumanBeing> projectManager;
// ... other stuff, including lists of things, that may be Literals or
// Links ... e.g. (ModelObjectList is an enhanced ArrayList that remembers
// the type(s) of the objects it contains - to get around type erasure ...
private ModelObjectList<Link<HumanBeing>> projectMembers;
private ModelObjectList<Link<Project>> relatedProjects;
private ModelObjectList<Literal<String>> projectAliases;
// the usual constructors, getters and setters for all of the above ...
public Project() {
projectName = new Literal<String>(String.class);
startDate = new Literal<Date>(Date.class);
endDate = new Literal<Date>(Date.class);
someCounter = new Literal<Integer>(Integer.class);
projectLeader = new Link<HumanBeing>(HumanBeing.class);
projectManager = new Link<HumanBeing>(HumanBeing.class);
projectMembers = new ModelObjectList<Link<HumanBeing>>(Link.class, HumanBeing.class);
// ... more ...
}
}
如果你将beaneditform指向Project.class的一个实例,那么在你必须提供很多自定义coercers,translators,valueencoders等之前你就不会走得太远 – 然后你仍然会遇到不能使用泛型的问题当“贡献”表示coercers,translators,valueencoders等.
然后我开始编写自己的组件来解决这些问题(例如ModelObjectDisplay和ModelObjectEdit),但是这需要我理解Tapestry的更多内容而不是我有时间学习…感觉就像我可能能够使用标准组件和自由使用“委托”等来做我想做的事.任何人都可以看到一条简单的路径供我使用吗?
感谢您阅读这篇文章.
PS:如果你想知道为什么我做了这样的事情,那是因为模型代表来自RDF图数据库(又称三重存储)的链接数据 – 我需要记住每一位数据的URI及其相关性(链接到其他位数据(欢迎您提出更好的方法)—)
编辑:
@uklance建议使用显示和编辑块 – 这是我已经尝试过的:
首先,我在AppPropertyDisplayBlocks.tml中有以下内容……
<t:block id="literal">
<t:delegate to="literalType" t:value="literalValue" />
</t:block>
<t:block id="link">
<t:delegate to="linkType" t:value="linkValue" />
</t:block>
并在AppPropertyDisplayBlocks.java中…
public Block getLiteralType() {
Literal<?> literal = (Literal<?>) context.getPropertyValue();
Class<?> valueClass = literal.getValueClass();
if (!AppModule.modelTypes.containsKey(valueClass))
return null;
String blockId = AppModule.modelTypes.get(valueClass);
return resources.getBlock(blockId);
}
public Object getLiteralValue() {
Literal<?> literal = (Literal<?>) context.getPropertyValue();
return literal.getValue();
}
public Block getLinkType() {
Link<?> link = (Link<?>) context.getPropertyValue();
Class<?> targetClass = link.getTargetClass();
if (!AppModule.modelTypes.containsKey(targetClass))
return null;
String blockId = AppModule.modelTypes.get(targetClass);
return resources.getBlock(blockId);
}
public Object getLinkValue() {
Link<?> link = (Link<?>) context.getPropertyValue();
return link.getTarget();
}
AppModule.modelTypes是从java类到Tapestry要使用的String的映射,例如: Link.class – > “link”和Literal.class – > “literal”…在AppModule中我有以下代码……
public static void contributeDefaultDataTypeAnalyzer(
MappedConfiguration<Class<?>, String> configuration) {
for (Class<?> type : modelTypes.keySet()) {
String name = modelTypes.get(type);
configuration.add(type, name);
}
}
public static void contributeBeanBlockSource(
Configuration<BeanBlockContribution> configuration) {
// using HashSet removes duplicates ...
for (String name : new HashSet<String>(modelTypes.values())) {
configuration.add(new DisplayBlockContribution(name,
"blocks/AppPropertyDisplayBlocks", name));
configuration.add(new EditBlockContribution(name,
"blocks/AppPropertyEditBlocks", name));
}
}
我有类似的编辑块代码…但是这些似乎都不起作用 – 我认为因为原始对象被传递给“委托”而不是被引用的对象,该对象是存储在文字中的值或者对象链接指向(hmm …应该是上面的[Ll] inkTarget,而不是[Ll] inkValue).我也一直遇到错误,Tapestry找不到合适的“翻译”,“价值编码器”或“coercer”…我在一段时间的压力下因此很难跟随这些曲折的段落以便离开迷宫 :-)
解决方法:
我建议在你想通过BeanEditForm编辑的对象周围构建一个瘦包装器并将它们传递给它.所以类似于:
public class TapestryProject {
private Project project;
public TapestryProject(Project proj){
this.project = proj;
}
public String getName(){
this.project.getProjectName().getValue();
}
public void setName(String name){
this.project.getProjectName().setValue(name);
}
etc...
}
这样,挂毯将处理它所知道的所有类型,让您不必创建自己的版本(顺便说一句,这本身就很简单).
标签:java,web-applications,tapestry,rdfstore 来源: https://codeday.me/bug/20190625/1285628.html