3

I have a Spring MVC controller with an action that's called using AJAX.

@SessionAttributes({"userContext"})
public class Controller
{
    ...
    @RequestMapping(value = "/my-url", method= { RequestMethods.POST })
    public ModelAndView doSomething(@ModelAttribute("userContext") UserContext context,
            SessionStatus sessionStatus)
    {
        BusinessObject obj = doSomeBusinessLogic(context.getUserName());
        sessionStatus.setComplete();
        ModelAndView mav = new ModelAndView("jsonView");
        mav.addObject("someInt", obj.getId());
        return mav;
    }
}

When I run this action, I get the following exception:

net.sf.json.JSONException: There is a cycle in the hierarchy!
at t.sf.json.util.CycleDetectionStrategy$StrictCycleDetectionStrategy.handleRepeatedReferenceAsObject(CycleDetectionStrategy.java:97)
at net.sf.json.JSONObject._fromBean(JSONObject.java:833)
at net.sf.json.JSONObject.fromObject(JSONObject.java:168)
at org.springframework.web.servlet.view.json.writer.jsonlib.PropertyEditorRegistryValueProcessor.processObjectValue(PropertyEditorRegistryValueProcessor.java:127)
at net.sf.json.JSONObject._fromMap(JSONObject.java:1334)
Truncated. see log file for complete stacktrace

After doing some debugging I found out that Spring is placing the UserContext object onto the ModelAndView that I am returning. If I hard-code my user name and remove the context object from the method's parameters, the action runs successfully. Is there a way to configure Spring to omit the ModelAttribute-annotated parameters from the returned ModelAndView? As you can see, sessionStatus.setComplete() has no effect.

3 Answers 3

1

I've had similar problems in the past with @SessionAttributes. By declaring @SessionAttributes({"userContext"}) you're telling Spring that you want "userContext" to always be available in the model, and so Spring has no choice but to send your UserContext object out to the model, just in case you're going to be redirecting or doing something else which might end up at another Controller.

The "solution" (and I didn't like it much, but it worked) was to omit the @SessionAttributes annotation on the controller, add an HttpSession parameter to the necessary methods and "manually" manage what's in it.

I'm interested to see if there's a better way, because it seems @SessionAttributes has tremendous potential to tidy up controller-level code.

0

I registered a WebArgumentResolver to get to my session variable. This allowed me to keep this session variable out of the response while keeping my action unit testable.

0

Along with @ModelAttribute, pass @ModelMap as a method argument. Based on business logic, error conditions -- if you do not need the attribute for certain scenarios, then remove it from the map.

    public ModelAndView foo(@ModelAttribute("userContext") UserContext, @ModelMap map){
      if(success){
       return success.jsp
      }
      else{
       map.remove("userContext");
      return "error.jsp" 
      }
    }

Not totally satisfied with having to pass the ModelMap as well, but I did not find any other easier way of doing it.

Cheers!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.