开发者

Modify Wicket FormComponent markup without panel?

开发者 https://www.devze.com 2023-03-23 11:04 出处:网络
In Wicket, I\'d like to subclass TextField form component to add additional markup around thetag. Why I do not want to create a Panel:

In Wicket, I'd like to subclass TextField form component to add additional markup around the tag.

Why I do not want to create a Panel:

1) I want the web page designer to use the input tag: <input wicket:i开发者_运维百科d="blah">

2) I don't want the subclass to lose the FormField semantics in Java, e.g.: field.setRequired(true);, etc.

I'm fine with hard-coding the wrapping markup in Java. But I'd like this to behave like a FormField in Java.

Any ideas? Looked around for examples, but stumped on this one

Edit: I'm aware of Borders, but my issue with them is you have to add them in both the markup and in Java. For example:

<div wicket:id="border">
<input type="text" wicket:id="field"/>
</div>

--

FormComponent<Integer> field = new TextField<Integer>("field", new Model(1));
field.setRequired(true);
Border border = new MyBorder("border");
border.add(field);
form.add(border);

This makes the web page designer have to be aware of special markup, and the Java can't be encapsulated (as a FormField subclass).


Ah, this is what I wanted via IBehavior:

My wrapper behavior (sorry for the Scala syntax):

class FieldWrapper extends AbstractTransformerBehavior {
  def transform(component: Component, output: CharSequence): CharSequence = """
<div class="blah">
  Blah blah blah  
  %s
</div>
""".format(output)
}

My subclass:

class MyField[T](id: String, model: IModel[T]) extends TextField[T](id, model) {
  add(new FieldWrapper)
}

Original Markup:

<input type="text" wicket:id="foobar"/>

Generated markup:

<div class="blah">
  Blah blah blah  
  <input type="text" value="" name="foobar" xmlns:wicket="http://wicket.apache.org">
</div>

Thanks S.O. for jumpstarting my mind :-)


You wouldn't even need to subclass TextField. although it might be easier to to so if you want to reuse it. If you just want to add markup outside of the original tag, it's the poster use case for a Border.


If digging into the rendering of a Component is needed

MarkupContainer#onRender()

is your friend.

An example might be:

AbstractTree#onRender()

mf

0

精彩评论

暂无评论...
验证码 换一张
取 消