How to redirect from spring ajax controller? -
i have controller @responsebody annotation. want if user doesn't exists process user's id , return json object. if exists redirect user page userinfo. below code gives ajax error. there way redirect user page userinfo?
@requestmapping(value = "/user/userinfo", method = {requestmethod.get}) @responsebody public string getuserinfo(httpservletrequest request, httpservletresponse response, modelmap modelmap) { if(...){ .....//ajax return }else{ modelmap.addattribute("userinfo", userinfofromdb); return "user/user.jsp"; } }
well, method annotated @responsebody
. means string
return value body of response. here returning "user/user.jsp"
caller.
as have full access response, can explicitely redirect response.sendredirect(...);
. possible explicitely ask spring pass userinfofromdb
redirectattribute through flash. can see more details on in this other answer me (this latter interceptor, can used same controller). have return null
tell spring controller code have processed response. here be:
... }else{ map<string, object> flash = requestcontextutils.getoutputflashmap(request); flash.put("userinfo", userinfofromdb); response.redirect(request.getcontextpath() + "/user/user.jsp"); return null; } ...
the problem client side expects string response not arrive , must prepared that. if not, error client side. alternative not redirect pass special string containing next url:
... }else{ map<string, object> flash = requestcontextutils.getoutputflashmap(request); flash.put("userinfo", userinfofromdb); // prepare redirect attribute return "special_string_for_redirect:" + request.getcontextpath() + "/user/user.jsp"); }
and let javascript client code process response , ask next page.
Comments
Post a Comment