Hi David!
Maybe you need this (at least this is what worked for me):
/**
* @author Martin
*/
public abstract class AjaxFormSubmittingChangeListenerBehavior extends
AjaxFormSubmitBehavior {
private final static Method hiddenFieldGetter;
static {
try {
hiddenFieldGetter = Form.class.getDeclaredMethod("getHiddenFieldId");
hiddenFieldGetter.setAccessible(true);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* @see org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#onBind()
*/
@Override
protected void onBind() {
super.onBind();
if (!(getComponent() instanceof IOnChangeListener))
{
throw new WicketRuntimeException("Behavior " + getClass().getName() +
" can only be added to an instance of a IOnChangeListener");
}
}
/**
* @param event
*/
public AjaxFormSubmittingChangeListenerBehavior(String event) {
super(event);
}
/**
* @see
org.apache.wicket.ajax.form.AjaxFormSubmitBehavior#onError(org.apache.wicket.ajax.AjaxRequestTarget)
*/
@Override
protected void onError(AjaxRequestTarget target) {
onSubmit(target);
}
/**
* @see
org.apache.wicket.ajax.form.AjaxFormSubmitBehavior#onEvent(org.apache.wicket.ajax.AjaxRequestTarget)
*/
@Override
protected void onEvent(AjaxRequestTarget target) {
HttpServletRequest httpServletRequest = ((WebRequest) getComponent()
.getRequest()).getHttpServletRequest();
Map parameters;
if (httpServletRequest instanceof MockHttpServletRequest) {
parameters = ((MockHttpServletRequest)
httpServletRequest).getParameterMap();
} else {
parameters = ((org.mortbay.jetty.Request)
httpServletRequest).getParameters();
}
parameters.put(getHiddenFieldId(getForm()),
getComponent().urlFor(IOnChangeListener.INTERFACE));
final FormComponent<?> formComponent = (FormComponent<?>) getComponent();
try {
if (isUpdateModel()) {
formComponent.inputChanged();
formComponent.validate();
if (!formComponent.hasErrorMessage()) {
formComponent.valid();
formComponent.updateModel();
}
}
super.onEvent(target);
} catch (RuntimeException e) {
Utils.errorLog(AjaxFormSubmittingChangeListenerBehavior.class, e);
onError(target);
}
}
/**
* @return boolean
*/
protected boolean isUpdateModel() {
return true;
}
/**
* @param form
* @return String
*/
private String getHiddenFieldId(Form<?> form) {
try {
Form<?> root = form.getRootForm();
return (String) hiddenFieldGetter.invoke(root);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
public abstract class
AjaxFormSubmittingChangeListenerDropDownChoice<T> extends
DropDownChoice<T> {
/** Initialize */ {
add(new
AjaxFormSubmittingChangeListenerBehavior(JavaScriptConstants.ONCHANGE)
{
@Override
protected void onSubmit(AjaxRequestTarget target) {
AjaxFormSubmittingChangeListenerDropDownChoice.this.onSubmit(target);
}
});
}
/**
* @see org.apache.wicket.MarkupContainer#toString()
*/
@Override
public String toString() {
return super.toString() + " Value: " + getValue();
}
/**
* @param id
* @param choices
* @param renderer
*/
public AjaxFormSubmittingChangeListenerDropDownChoice(String id,
IModel<List<? extends T>> choices, IChoiceRenderer<T> renderer) {
super(id, choices, renderer);
}
/**
* @param target
*/
protected abstract void onSubmit(AjaxRequestTarget target);
/**
* @param target
*/
protected void onError(AjaxRequestTarget target) {
onSubmit(target);
}
/**
* @param id
* @param choices
*/
public AjaxFormSubmittingChangeListenerDropDownChoice(String id,
IModel<List<? extends T>> choices) {
super(id, choices);
}
/**
* @param id
* @param model
* @param choices
* @param renderer
*/
public AjaxFormSubmittingChangeListenerDropDownChoice(String id,
IModel<T> model,
IModel<List<? extends T>> choices, IChoiceRenderer<T> renderer) {
super(id, model, choices, renderer);
}
/**
* @param id
* @param model
* @param choices
*/
public AjaxFormSubmittingChangeListenerDropDownChoice(String id,
IModel<T> model,
IModel<List<? extends T>> choices) {
super(id, model, choices);
}
/**
* @param id
* @param model
* @param data
* @param renderer
*/
public AjaxFormSubmittingChangeListenerDropDownChoice(String id,
IModel<T> model,
List<? extends T> data, IChoiceRenderer<T> renderer) {
super(id, model, data, renderer);
}
/**
* @param id
* @param model
* @param choices
*/
public AjaxFormSubmittingChangeListenerDropDownChoice(String id,
IModel<T> model,
List<? extends T> choices) {
super(id, model, choices);
}
/**
* @param id
* @param data
* @param renderer
*/
public AjaxFormSubmittingChangeListenerDropDownChoice(String id,
List<? extends T> data,
IChoiceRenderer<T> renderer) {
super(id, data, renderer);
}
/**
* @param id
* @param choices
*/
public AjaxFormSubmittingChangeListenerDropDownChoice(String id,
List<? extends T> choices) {
super(id, choices);
}
/**
* @param id
*/
public AjaxFormSubmittingChangeListenerDropDownChoice(String id) {
super(id);
}
}
**
Martin
2010/3/21 Sven Meier <[email protected]>:
> David,
>
>>It seems that you do not understand the problem I have
>
> obviously not ;).
>
> A quickstart would help me to understand your problem better, but perhaps
> others have already a clue?
>
> Sven
>
> David Chang wrote:
>>
>> Sven,
>>
>> Thanks for your input. It seems that you do not understand the problem I
>> have (Wicket problem??? or my page problem???). I know how Ajax works by
>> adding components to Ajax request target.
>>
>> When it is non-Ajax, my page runs correctly with all three controls. When
>> Country DDC Ajax-controls State DDC, the two DDC works correctly. The
>> problem is that once this Ajax control is triggered by selecting a value in
>> Country DDC, the State DDC list is NOT updated when the Locale DDC value is
>> changed.
>>
>> I have a feel that Wicket has a problem here. I am using Wicket 1.4.7.
>>
>> Regards.
>> -David
>>
>>
>>
>> --- On Sun, 3/21/10, Sven Meier <[email protected]> wrote:
>>
>>
>>>
>>> From: Sven Meier <[email protected]>
>>> Subject: Re: Ajax has too much control?
>>> To: [email protected]
>>> Date: Sunday, March 21, 2010, 7:25 AM
>>> Hi David,
>>>
>>> DropDownChoice#wantOnSelectionChangedNotifications() will
>>> trigger rendering of the complete page.
>>> On Ajax request only those components are rendered you
>>> explicitely 'add' to the request, see AjaxRequestTarget#addComponent().
>>>
>>> If you want to have the same for Ajax requests, you can
>>> just 'add' the complete page.
>>>
>>> This is not very efficient though.
>>>
>>> Sven
>>>
>>> David Chang wrote:
>>>
>>>>
>>>> Forgive me about this meaningless subject, but I
>>>>
>>>
>>> cannot think of a better one.
>>>
>>>>
>>>> I have been learning Wicket through the WIA book. I
>>>>
>>>
>>> just found out something interesting to me. Not sure it is a
>>> bug, design, or something I did wrong.
>>>
>>>>
>>>> I have a page with three Wicket elements:
>>>>
>>>> 1. Locale selector through a DropDownChoice list. WIA
>>>>
>>>
>>> has complete code about how this works and I copied the
>>> solution into this page
>>>
>>>>
>>>> 2. Country DropDownChoice, whose values change between
>>>>
>>>
>>> English and Chinese depending on Locale DDC.
>>>>
>>>> 3. State DropDownChoice, whose values change between
>>>>
>>>
>>> English and Chinese depending on Locale DDC. The values in
>>> this DDC depends on the chosen value in Country DDC.
>>>
>>>>
>>>> Here are the experiments
>>>>
>>>> Experiment#1.
>>>>
>>>> Country DDC does not control values in State DDC via
>>>>
>>>
>>> Ajax and it has
>>>>
>>>> protected boolean
>>>>
>>>
>>> wantOnSelectionChangedNotifications() {
>>>
>>>>
>>>> return true;
>>>> }
>>>>
>>>> Everything works like a charm, which means
>>>> (1) when Country DDC value changes, State DDC changes
>>>>
>>>
>>> accodingly.
>>>
>>>>
>>>> (2) when Locale changes, both Country DDC and State
>>>>
>>>
>>> DDC lists change display values accordingly (which means
>>> both DDCs show a list of values in the same language).
>>>
>>>>
>>>> Experiment#2.
>>>>
>>>> Country DDC controls values in State DDC via Ajax. In
>>>>
>>>
>>> this case, when page is first loaded, I do not touch the
>>> Country DDC or State DDC. I simply change locale value any
>>> number of times, both Country DDC and State DDC lists change
>>> correctly depending on the session locale. Here is the
>>> strange thing. Then I change Country DDC value, State DDC
>>> changes correctly. Since then, HOWEVER, if I change locale
>>> values, ONLY Country DDC list changes correctly; State DDC
>>> list is not updated. It seems Wicket decides that State DDC
>>> is forever Aja-controlled by Country DDC only.
>>>
>>>>
>>>> Not qure sure if this a bug, design, or I did
>>>>
>>>
>>> something wrong.
>>>
>>>>
>>>> Please let me if you have difficulty understanding the
>>>>
>>>
>>> experiments.
>>>
>>>>
>>>> Thanks for any info or help.
>>>>
>>>> Cheers!
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>> ---------------------------------------------------------------------
>>>
>>>>
>>>> To unsubscribe, e-mail: [email protected]
>>>> For additional commands, e-mail: [email protected]
>>>>
>>>>
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [email protected]
>>> For additional commands, e-mail: [email protected]
>>>
>>>
>>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]