different markup, but same Java code in wicket?
I have a bunch of panels with different markup. I want to reuse the same Java class and work it out with variations.
In some panels some components are missing, but I don't want to make changes to my Java class to set the visibility of the component for each case. That can get a little messy. I want to reuse the same Java class, while my app doesn't break if there are missing components in the markup开发者_Python百科. So I think it comes down to this: 1- can you add components to the panel class and then not use them in the mark up? E.g:Java
add(new Label("a","hello"));
add(new Label("b","hi"));
add(new Label("c","see ya"));
add(new Label("d","good bye"));
hello_1.hml
<span wicket:id="a">xxx</span>
<span wicket:id="b">xxx</span>
<span wicket:id="c">xxx</span>
<span wicket:id="d">xxx</span>
hello_2.hml<br>
<span wicket:id="a">xxx</span>
<div wicket:id="d">xxx</div>
hello_3.hml<br>
<div wicket:id="a">xxx</div>
<b wicket:id="c">xxx</b>
hello_4.hml<br>
<div wicket:id="a">xxx</div>
I hope my question is clear.
To answer your question directly: no, the Java and the HTML side should match exactly.
However there's always a way around, depending on the specific use case.
- Make HTML elements invisible from CSS or a style attribute.
- Use fragments to avoid rendering optional bits.
- Use inheritance, with more feature-rich panels extending simple panels.
- Make components invisible from Java code.
But I have to admit that the problem sounds somewhat suspicious, it is possible that with a more careful separation of components the whole issue can be avoided. But without knowing more about the system it's hard to tell.
What about adding the component in the Java hierarchy depending on the variation ?
if ("4".equals(getVariation()) { add(new ComponentA("a")); }
精彩评论