[英]Editing a complex Java object in a Tapestry 5 web application
我正在使用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
指向beaneditform
的一個實例,那么在你必須提供很多自定義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...
}
這樣,掛毯將處理它所知道的所有類型,讓您不必創建自己的版本(順便說一句,這本身就很簡單)。
您可以提供塊來顯示和編輯"link"
和"literal"
數據類型。
beaneditform
, beaneditor
和beandisplay
由BeanBlockSource
服務支持。 BeanBlockSource
負責為各種數據類型提供顯示和編輯塊。
如果您下載了tapestry源代碼並查看以下文件:
您將看到tapestry如何貢獻EditBlockContribution
和DisplayBlockContribution
來提供默認塊(例如,對於"date"
數據類型)。
如果您參與BeanBlockSource
,則可以為自定義數據類型提供顯示和編輯塊。 這將要求您在頁面中按ID引用塊。 通過@WhitelistAccessOnly
對用戶進行注釋,可以隱藏用戶的@WhitelistAccessOnly
。
這是使用接口和代理隱藏模型中的實現細節的示例。 請注意代理如何處理更新已修改的標志,並能夠將來自Literal數組的URI映射到HumanBeing接口中的屬性。
package com.github.uklance.triplestore;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
public class TripleStoreOrmTest {
public static class Literal<T> {
public String uri;
public boolean modified;
public Class<T> type;
public T value;
public Literal(String uri, Class<T> type, T value) {
super();
this.uri = uri;
this.type = type;
this.value = value;
}
@Override
public String toString() {
return "Literal [uri=" + uri + ", type=" + type + ", value=" + value + ", modified=" + modified + "]";
}
}
public interface HumanBeing {
public String getName();
public void setName(String name);
public int getAge();
public void setAge();
}
public interface TripleStoreProxy {
public Map<String, Literal<?>> getLiteralMap();
}
@Test
public void testMockTripleStore() {
Literal<?>[] literals = {
new Literal<String>("http://humanBeing/1/Name", String.class, "Henry"),
new Literal<Integer>("http://humanBeing/1/Age", Integer.class, 21)
};
System.out.println("Before " + Arrays.asList(literals));
HumanBeing humanBeingProxy = createProxy(literals, HumanBeing.class);
System.out.println("Before Name: " + humanBeingProxy.getName());
System.out.println("Before Age: " + humanBeingProxy.getAge());
humanBeingProxy.setName("Adam");
System.out.println("After Name: " + humanBeingProxy.getName());
System.out.println("After Age: " + humanBeingProxy.getAge());
Map<String, Literal<?>> literalMap = ((TripleStoreProxy) humanBeingProxy).getLiteralMap();
System.out.println("After " + literalMap);
}
protected <T> T createProxy(Literal<?>[] literals, Class<T> type) {
Class<?>[] proxyInterfaces = { type, TripleStoreProxy.class };
final Map<String, Literal> literalMap = new HashMap<String, Literal>();
for (Literal<?> literal : literals) {
String name = literal.uri.substring(literal.uri.lastIndexOf("/") + 1);
literalMap.put(name, literal);
}
InvocationHandler handler = new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getDeclaringClass().equals(TripleStoreProxy.class)) {
return literalMap;
}
if (method.getName().startsWith("get")) {
String name = method.getName().substring(3);
return literalMap.get(name).value;
} else if (method.getName().startsWith("set")) {
String name = method.getName().substring(3);
Literal<Object> literal = literalMap.get(name);
literal.value = args[0];
literal.modified = true;
}
return null;
}
};
return type.cast(Proxy.newProxyInstance(getClass().getClassLoader(), proxyInterfaces, handler));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.