views:

534

answers:

4

If I write

<form wicket:id="form" id="form>

or even

<form wicket:id="form>...

Then the rendered HTML shows the id 'form' appended with different numbers whenever the page is refreshed e.g.

   <form id="form7"....

Is there a way to disable this behavior of the Wicket framework?

A: 

It has been a while since I worked with Wicket, but I remember that when wicket uses ajax elements, its ids are auto-generated (the id of the tag, not the wicket:id). You can control the id of the tag when not using and ajax element. In your case, since there is no code, I would guess that you will have to change any AjaxButton or Ajax* from your form.

YuppieNetworking
The problems I am facing with these changing ids are on the client-side stuff, like my css cannot use these ids to map the styles, I cannot write javascript that plays with the element ids.
Monis Iqbal
@Monis Iqbal You're supposed to use Wicket's Ajax hooks etc. directly.
Esko
meaning that I cannot write custom javascript? Isn't this violation of SOC that Wicket supports? because javascript is supposed to be client-side.
Monis Iqbal
+2  A: 

This is the behavior you want in most cases when using wicket. The dynamic id is meant to prevent id collisions when Ajax behaviors are added to components or added to ajax responses for refreshing. For any of these situations, you really need both the client response and the server side state to be in cahoots. If there are external js resources you need the id of a component for dom lookup, then I would suggest adding a custom wicket component behavior that would then generate the js call to a function passing in the generated id.

I realize what I'm going to describe leads you more into the forest of Wicket. But I have been more than happy with the ajaxy stuff that Wicket opens up for you out of the box.

Matt
is it a Wicket norm to write even Javascript in the Java files?
Monis Iqbal
Like everything else unfortunately, it depends. There are examples for libraries of JS (wicket-ajax.js for example) and generated JS from within java. The Wicket source code should have examples of both. Ultimately just about any of the standard ajax behaviors at some point will include a standard header contributor to include the wicket-ajax.js file. Check out some of the ajax components for seeing generated JS many will have a get generated js method or something to attach to the component that it is added to.
Matt
+2  A: 

This is Wicket desing feature. You can use class for linking styles and components.

<form wicket:id="form" id="form>

Also you can to try (I never did it) setMarkupId . I'm not sure that it good way.

leonidv
setMarkupId didn't work.
Monis Iqbal
setMarkupId is the way how to do that see my answer below
Michal Bernhard
oh, sorry. I misread it as setOutputMarkupId(true). Apologies @leonidv. and thanks @Michal
Monis Iqbal
+2  A: 

We set markup ids by hand extensively on our project to ease automatic testing with Selenium testing framework. It definetely works.

Component.setOutputMarkupId(true); // write id attribute of element to html
Component.setMarkupId("someid"); // id attribute of element is "someid"
Michal Bernhard