Announcement Announcement Module
Collapse
No announcement yet.
IllegalStateException on deploying a webmvc app with a session scoped bean. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • IllegalStateException on deploying a webmvc app with a session scoped bean.

    Hi,

    As I am making my way through the tutorials and static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-factory-scopes documentation, I cannot get around an IllegalStateException I encounter whenever I deploy my simple spring-webmvc-2.5 application. My web application can be deployed with the 'model' bean defaulting to a singleton scope, however when I change the dispatcher-servlet.xml and set the scope for a model bean to session (scope="session"), I get an IllegalStateException when the application is deployed (and the appserver requires a restart).

    I followed the documentation to the letter, tried various configuration permutations (using filters and/or listeners), I verified if the ApplicationContext is a XmlWebApplicationContext (and it is, so the context should be web-aware). I'm using web mvc version 2.5 and I am using the org.springframework.web.servlet.DispatcherServlet (according to the documentation then no special setup is necessary: DispatcherServlet and DispatcherPortlet already expose all relevant state).

    I have googled myself senseless, visited every possible forum to find an answer, but I am at the end of my rope. So...... I have prepared this small sample project (attached zipped) with only 1 controller, 1 command class, 1 jsp and 1 model class. It's a small project but also big enough to present a reproducible case. The attached (netbeans) project can be build and deployed without a problem. However, when I change the line (in dispatcher-servlet.xml)
    Code:
    8<---- <bean name="homeModelBean" class="sample.sessionscope.model.HomeModel"> ----
    to
    Code:
    8<---- <bean name="homeModelBean" class="sample.sessionscope.model.HomeModel" scope="session"> ----
    and build and deploy, I get the IllegalStateException. The first exception message in full is: Caused by: java.lang.IllegalStateException: ContainerBase.addChild: start: LifecycleException: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request? If you are actually operating within a web request and still receive this message,your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.


    To answer the question Are you referring to request attributes outside of an actual web request?, I think the answer the answer would be no, I'm just deploying a web application and I don't think my code is referring to request attributes (it is true though that it is outside an actual web request)

    Long story short, I would really appreciate it, I mean really, if someone could help me further and explain why I'm still getting the IllegalStateException (while I think I'm doing everything right) on deploying the web app to an instance of a Sun Java System Application Server 9.1_02 (build b04-fcs).

  • #2
    First off all WHY do you want your model defined and wired in the application context. Why not simply create one in the formBackingObject ofyour controller (which is the way it is intended to work).

    Are you referring to request attributes outside of an actual web request?
    Well actually the answer is yes, you are just starting your application so there is currently no httprequest or httpsession active so no way to utilize the bean.

    Comment


    • #3
      First of all, thanks for your prompt reply, I appreciate it.

      I thought I'd prepare a simple small project to support anybody who is willing to have a look at my 'problem' instead of burying the helper with a lot of excess information. Maybe I stripped the project to the point that it may not make sense anymore. Sorry for that. So I'll rephrase my question:

      I want to define a bean that has a session scope that I use in a web application (think of a shopping cart). I take it you suggest that I use a formBackingObject for that, I believe others in the Spring community call it a commandObject.

      So when you suggest to Why not simply create one (a model) in the formBackingObject of your controller and it is true that a commandObject and formBackingObject are synonymous, my next question is, how do I wire a commandObject. In all the tutorials and documentation I have been eyeballing the last month, I haven't found a single example how to do that. I may have been looking at the wrong documentation (static.springframework.org/spring/docs and Spring in Action among others). I hope you can point me in the right direction for a sample or reference document that helps me further.

      Thanks,

      Dieuwe van Amerongen.

      Comment


      • #4
        You don't wire a formBackingObject. You don't want about 30 users share the same object. You do that in the formBackingObject method, that is where you setup the formBackingObject.

        Althoug it could be useable by specifing session/request scope or retrieving it from the ApplicationContext in the formBackingObject method and let it be a prototype in your configuration.

        I think the session/request scope approach might lead to issues with the management in the Controllers.

        Code:
        return new ModelAndView("home", "model", model);
        Another thing that is confusing is why are you putting the model object into the model?!

        Why? This will lead to serialization of the model object, which is a proxy. So I'm not sure what you want to achieve here.

        Edit: It just struck me, you forgot the aop:scoped-proxy element inside the session scoped bean.
        Last edited by Marten Deinum; Aug 13th, 2008, 04:52 AM.

        Comment


        • #5
          You don't wire a formBackingObject.
          that's what I thought
          You don't want about 30 users share the same object.
          I couldn't agree more
          You do that in the formBackingObject method, that is where you setup the formBackingObject.
          thanks

          Another thing that is confusing is why are you putting the model object into the model?!
          What's in a name, according to one MVC definition the model represents the information (the data) of the application and the business rules used to manipulate the data. It doesn't matter if I call it cart, shopping trolley or model. Fact of the matter is that the object has a property called name representing data (the name of the object) and in this case it had no behavior (business rules). I thought I'd just keep my sample generic, but I see it may have been confusing calling it model.

          Edit: It just struck me, you forgot the aop:scoped-proxy element inside the session scoped bean.
          That is it, I tried it and it solved my problem. Thanks a million, now I can go on to other things with happy thoughts.

          p.s. I'll refactor my original 'project' and attach the updated version later with less confusing names and one that works. Again, thanks a million.

          Comment


          • #6
            attached a new zip file with the implemented solution

            with files:

            build.xml
            src/java/sample/sessionscope/controller/HomeCommand.java
            src/java/sample/sessionscope/controller/HomeController.java
            src/java/sample/sessionscope/model/OneBusinessObjectPerSession.java
            web/redirect.jsp
            web/WEB-INF/applicationContext.xml
            web/WEB-INF/dispatcher-servlet.xml
            web/WEB-INF/sun-web.xml
            web/WEB-INF/web.xml
            web/WEB-INF/jsp/home.jsp


            not too much

            Comment

            Working...
            X