You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Wicket <wi...@gmail.com> on 2011/01/19 18:39:47 UTC
Dealing with json in wicket
Hi,
I read about how to make a JSON response thru wicket but how do you
handle the request?
/ Mathias
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Dealing with json in wicket
Posted by "Marc Nuri (GMail)" <ma...@gmail.com>.
Hi
I use a combination of an IRequestTarget implementation and an extended
UrlCodingStrategy.
Regards.
--
Marc Nuri
This is what I use, maybe it helps
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public abstract class JSONRequestTarget implements IRequestTarget {
private final RequestParameters requestParameters;
public JSONRequestTarget(RequestParameters requestParameters) {
this.requestParameters = requestParameters;
}
protected abstract JSONArray getJSONData();
@Override
public void respond(RequestCycle requestCycle) {
//Change enconding to support tildes and special characters
requestCycle.getResponse().setContentType("text/html;
charset=utf-8");
requestCycle.getResponse().setCharacterEncoding("utf-8");
requestCycle.getResponse().write(getJSONData().toString());
}
@Override
public void detach(RequestCycle requestCycle) {
//Clean up stuff
requestParameters.setParameters(null);
}
public Map<String, ?> getParameters() {
return requestParameters.getParameters();
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public abstract class JSONUrlCodingStrategy extends
AbstractRequestTargetUrlCodingStrategy {
public JSONUrlCodingStrategy(final String mountPath) {
super(mountPath);
}
protected abstract JSONRequestTarget
generateRequestTarget(RequestParameters requestParameters);
@Override
public CharSequence encode(IRequestTarget requestTarget) {
if (!(requestTarget instanceof JSONRequestTarget)) {
throw new IllegalArgumentException("This encoder can only be
used with "
+ "instances of " + JSONRequestTarget.class.getName());
}
final AppendingStringBuffer url = new AppendingStringBuffer(40);
url.append(getMountPath());
final JSONRequestTarget target = (JSONRequestTarget) requestTarget;
appendParameters(url, new PageParameters(target.getParameters()));
return url;
}
@Override
public IRequestTarget decode(RequestParameters requestParameters) {
return generateRequestTarget(requestParameters);
}
@Override
public boolean matches(IRequestTarget requestTarget) {
boolean ret = false;
if (requestTarget instanceof JSONRequestTarget) {
ret = true;
}
return ret;
}
}
>
>
Re: Dealing with json in wicket
Posted by Marc Nuri San FĂ©lix <ma...@marcnuri.com>.
Hi
I use a combination of an IRequestTarget implementation and an extended
UrlCodingStrategy.
Regards.
--
Marc Nuri
This is what I use, maybe it helps
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public abstract class JSONRequestTarget implements IRequestTarget {
private final RequestParameters requestParameters;
public JSONRequestTarget(RequestParameters requestParameters) {
this.requestParameters = requestParameters;
}
protected abstract JSONArray getJSONData();
@Override
public void respond(RequestCycle requestCycle) {
//Change enconding to support tildes and special characters
requestCycle.getResponse().setContentType("text/html;
charset=utf-8");
requestCycle.getResponse().setCharacterEncoding("utf-8");
requestCycle.getResponse().write(getJSONData().toString());
}
@Override
public void detach(RequestCycle requestCycle) {
//Clean up stuff
requestParameters.setParameters(null);
}
public Map<String, ?> getParameters() {
return requestParameters.getParameters();
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public abstract class JSONUrlCodingStrategy extends
AbstractRequestTargetUrlCodingStrategy {
public JSONUrlCodingStrategy(final String mountPath) {
super(mountPath);
}
protected abstract JSONRequestTarget
generateRequestTarget(RequestParameters requestParameters);
@Override
public CharSequence encode(IRequestTarget requestTarget) {
if (!(requestTarget instanceof JSONRequestTarget)) {
throw new IllegalArgumentException("This encoder can only be
used with "
+ "instances of " + JSONRequestTarget.class.getName());
}
final AppendingStringBuffer url = new AppendingStringBuffer(40);
url.append(getMountPath());
final JSONRequestTarget target = (JSONRequestTarget) requestTarget;
appendParameters(url, new PageParameters(target.getParameters()));
return url;
}
@Override
public IRequestTarget decode(RequestParameters requestParameters) {
return generateRequestTarget(requestParameters);
}
@Override
public boolean matches(IRequestTarget requestTarget) {
boolean ret = false;
if (requestTarget instanceof JSONRequestTarget) {
ret = true;
}
return ret;
}
}
>
>