I ran into the same problem. Please see the decision I made and see if it is useful to you.
My log page used the old model controller instead of spring 3.0 annotations.
I registered the global ajax error handler as follows
jQuery(document).ajaxError( function(event, request, ajaxOptions, thrownError) { try { var result = getJsonObject(request.responseText);
Then in my login controller, I check if the original request was an ajax request or the x-requested-with header was not used. If it was an ajax request, I return a json response saying {"timeout" : true} .
private boolean isAjaxRequest(HttpServletRequest request) { boolean isAjaxRequest = false; SavedRequest savedRequest = (SavedRequest) request.getSession() .getAttribute( DefaultSavedRequest.SPRING_SECURITY_SAVED_REQUEST_KEY); if (savedRequest != null) { List<String> ajaxHeaderValues = savedRequest .getHeaderValues("x-requested-with"); for (String value : ajaxHeaderValues) { if (StringUtils.equalsIgnoreCase("XMLHttpRequest", value)) { isAjaxRequest = true; } } } return isAjaxRequest; } ............. ............. ............. if (isAjaxRequest(request)) { Map<String, Object> model = new HashMap<String, Object>(); model.put("timeout", true); return new ModelAndView(new JSONView(model));
JSONView implementation example
import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSON; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import org.springframework.web.servlet.View; import com.greytip.common.json.CougarJsonConfig; public class JSONView implements View { private JSON jsonObject; private JsonConfig jsonConfig; private String value; public JSONView(Object value) { this(); jsonObject = JSONObject.fromObject(value, jsonConfig); } public JSONView(List<?> value) { this(); jsonObject = JSONArray.fromObject(value, jsonConfig); } public JSONView(String value) { this(); this.value = value; } public JSONView() { jsonConfig = new JsonConfig();
It uses the json-lib library to convert objects to json format.
Spring 3.0 has very good jackson library support , you can try using it.
source share