Results 1 to 9 of 9

Thread: session bean lost values after refresh parent page

  1. #1
    Join Date
    Jul 2008
    Posts
    182

    Default session bean lost values after refresh parent page

    Hi,

    I tried to use spring mvc 3.1 session scope to pass values from child page back to parent page (jsp), but the value is lost after refresh the parent page.

    Here is my TestController code:

    Code:
    @Controller
    public class TestController {
     
         private static final Logger logger = Logger.getLogger(TestController.class);
       
        @Autowired
        private CategorySessionBean categorySessionBean;
       
       
        @RequestMapping(value = "/", method = RequestMethod.GET)
        public String home(Model model) {
            logger.debug(" =======HOME PAGE========");
            model.addAttribute("categorySessionBean", categorySessionBean);
            return "index";
        }
     
        @RequestMapping(value = "/popupPage", method = RequestMethod.GET)
        public String popupPage(Model model) {
            logger.debug(" =======POPUP PAGE========");
            logger.debug(" =======categorySessionBean: ========"+categorySessionBean.toString()); // all fields in this session bean thave lost when child javascript refresh parent page.
            model.addAttribute("categorySessionBean", categorySessionBean);
            return "popupPage";
        }
       
        @RequestMapping(method = RequestMethod.POST)
        public String processPopupPageSubmit(
            @ModelAttribute("categorySessionBean") CategorySessionBean categorySessionBean, BindingResult result, SessionStatus status) {
            logger.debug(" =======processSubmit========");
           logger.debug(" =======categorySessionBean: ========"+categorySessionBean.toString());  // after filled in the fields of this session bean in popupPage and click on submit, all values are store here.
            return "popupPage";
        }
    }
    parent jsp file (index.jsp):

    Code:
    <%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
    <%@taglib  uri="http://www.springframework.org/tags" prefix="spring" %>
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
     
     
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
     
     
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <form:form id="form" name="add_category_form" modelAttribute="categorySessionBean" action="save" method="post" >
                        <form:errors path="*" class="error" element="div" />
                        <form:input type="text" path="name"></form:input><br>
                        <form:input type="text" path="description"></form:input><br>
                        <form:input type="text" path="id"></form:input><br>
                        <a href="#" onclick="openUploadImagePage()">click to popup</a> <br>
                        <input type=BUTTON value="Submit" name="mySubmit" onClick="submitform()">
            </form:form>
        </body>
    </html>
     
     
    <script type="text/javascript">
        function submitform() {
                this.document.forms['add_category_form'].submit();
              }
             
        function openUploadImagePage() {
            window.open('popupPage','Popup Page','width=600,height=400');
        }
     
     
    </script>
    popup child jsp (popupPage.jsp) file:

    Code:
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <script language="JavaScript">
                 function refreshParent() {
                     var URL = unescape(window.opener.location.pathname); 
                     var PARMS = unescape(window.opener.location.search);
     
     
                     var ms = new Date().getTime();
                     window.opener.location.href="/TestSession-1.0/popupPage"+PARMS;
                     window.close();
                 }
            
            </script>
            <title>JSP Page</title>
        </head>
        <body>
            <form:form id="form" name="add_category_form" modelAttribute="categorySessionBean" action="save" method="post" >
                        <form:errors path="*" class="error" element="div" />
                        <c:choose>
                            <c:when test='${not empty categorySessionBean.name}'>
                                <script language="JavaScript">
                                    this.refreshParent();
                                </script>
                            </c:when>
                           
                        </c:choose>
                        <form:input type="text" path="name"></form:input><br>
                        <form:input type="text" path="description"></form:input><br>
                        <form:input type="text" path="id"></form:input><br>
                        <a href="#" onclick="openUploadImagePage()">click to popup</a> <br>
                        <input type=BUTTON value="Submit" name="mySubmit" onClick="submitform()">
            </form:form>
        </body>
    </html>
     
    <script type="text/javascript">
        function submitform() {
                this.document.forms['add_category_form'].submit();
              }
             
        function openUploadImagePage() {
            window.open('/popupPage','Popup Page','width=600,height=400');
        }
     
    </script>
    mvc-config.xml file:

    Code:
    <mvc:annotation-driven/>
     
     
              <mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/web-resources/" />
              <mvc:default-servlet-handler />
       
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/"/>
                <property name="suffix" value=".jsp"/>
            </bean>
              <!-- Map paths directly to view names without controller processing. Use the view-name attribute if necessary: by convention the view name equals the path without the leading slash -->
     
            <mvc:view-controller path="/" view-name="index" />
           
            <!-- an HTTP Session-scoped bean exposed as a proxy -->
            <bean id="categorySessionBean" class="testsession.domain.bean.CategorySessionBean" scope="session">
               <!-- this next element effects the proxying of the surrounding bean -->
               <aop:scoped-proxy/>
            </bean>
    Any suggestion is very appreciated.
    Thanks
    Sam

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    The @ModelAttribute is another instance it isn't the same as your session scoped bean and hence nothing is bound/set to the session scoped bean (it basically is useless).

    Is this really what you want? Or do you only want to store the modelattribute in the session between requests (if so use @SessionAttribute and not a session scoped bean) or does it really have to live as a session-scoped bean so that is it usable between different controllers?
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jul 2008
    Posts
    182

    Default

    HI Marten,

    Thank you for your suggestion.
    I wanted to keep some objects / values in a session-scoped bean, so that it can pass back and forth between submitted pages. But as you suggested to use @SessionAttribute, I am confused in what situation should I use session-scoped bean.

    Thanks a lot
    Sam

  4. #4
    Join Date
    Jul 2008
    Posts
    182

    Default

    Here I got a new error when use @SessionAttributes in my controller:

    let Spring MVC Dispatcher Servlet threw exception: org.springframework.web.HttpSessionRequiredExcepti on: Expected session attribute 'categoryBean'
    at org.springframework.web.method.annotation.ModelFac tory.initModel(ModelFactory.java:103) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotat ion.RequestMappingHandlerAdapter.invokeHandlerMeth od(RequestMappingHandlerAdapter.java:614) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotat ion.RequestMappingHandlerAdapter.handleInternal(Re questMappingHandlerAdapter.java:578) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.web.servlet.mvc.method.Abstrac tHandlerMethodAdapter.handle(AbstractHandlerMethod Adapter.java:80) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:923) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:852) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:882) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:778) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 343) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.access.intercept. FilterSecurityInterceptor.invoke(FilterSecurityInt erceptor.java:109) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.access.intercept. FilterSecurityInterceptor.doFilter(FilterSecurityI nterceptor.java:83) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 355) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.access.ExceptionT ranslationFilter.doFilter(ExceptionTranslationFilt er.java:97) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 355) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.session.SessionMa nagementFilter.doFilter(SessionManagementFilter.ja va:100) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 355) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.authentication.An onymousAuthenticationFilter.doFilter(AnonymousAuth enticationFilter.java:78) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 355) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.servletapi.Securi tyContextHolderAwareRequestFilter.doFilter(Securit yContextHolderAwareRequestFilter.java:54) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 355) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.savedrequest.Requ estCacheAwareFilter.doFilter(RequestCacheAwareFilt er.java:35) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 355) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.authentication.http://www.BasicAuthenticationFilter...lter.java:177) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 355) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.authentication.Ab stractAuthenticationProcessingFilter.doFilter(Abst ractAuthenticationProcessingFilter.java:188) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 355) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.authentication.lo gout.LogoutFilter.doFilter(LogoutFilter.java:105) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 355) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.context.SecurityC ontextPersistenceFilter.doFilter(SecurityContextPe rsistenceFilter.java:79) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.FilterChainProxy$ VirtualFilterChain.doFilter(FilterChainProxy.java: 355) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.security.web.FilterChainProxy. doFilter(FilterChainProxy.java:149) [spring-security-web-3.0.2.RELEASE.jar:]
    at org.springframework.web.filter.DelegatingFilterPro xy.invokeDelegate(DelegatingFilterProxy.java:346) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.web.filter.DelegatingFilterPro xy.doFilter(DelegatingFilterProxy.java:259) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.springframework.web.filter.HiddenHttpMethodFil ter.doFilterInternal(HiddenHttpMethodFilter.java:7 7) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.web.security.SecurityContextAssociati onValve.invoke(SecurityContextAssociationValve.jav a:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run( JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:619) [rt.jar:1.6.0_07]
    Controller code:

    Code:
    package testsession.domain;
    
    @Controller
    @SessionAttributes("categoryBean")
    public class TestController {
    
        private static final Logger logger = Logger.getLogger(TestController.class);
        
        
        @RequestMapping(value = "/", method = RequestMethod.GET)
        public String home(@ModelAttribute("categoryBean") CategoryBean categoryBean, Model model) {
            logger.debug(" =======HOME PAGE========");
            logger.debug(" =======categoryBean: ========"+categoryBean.toString());
            return "index";
        }
     
        @RequestMapping(value = "/popupPage", method = RequestMethod.GET)
        public String popupPage(@ModelAttribute("categoryBean") CategoryBean categoryBean, Model model) {
            logger.debug(" =======POPUP PAGE========");
            logger.debug(" =======categoryBean: ========"+categoryBean.toString());
            return "popupPage";
        }
        
        @RequestMapping(method = RequestMethod.POST)
        public String processPopupPageSubmit(
            @ModelAttribute("categoryBean") CategoryBean categoryBean, BindingResult result, SessionStatus status) {
            logger.debug(" =======processSubmit========");
            logger.debug(" =======categoryBean: ========"+categoryBean.toString());
            return "popupPage";
        }
    }

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    You really seem to be missing some basic understanding. I strongly suggest the reference guide especially the part that explains @ModelAttribute and @SessionAttribute...

    In short adding @SessionAttribute doesn't auto magically put a bean in your session you still need something that creates the model object (spring will store it in the session because that is what @SessionAttribute is telling spring to do).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  6. #6
    Join Date
    Jul 2008
    Posts
    182

    Default

    Hi Marten,

    I don't know where to find a full description/tutorial how to use @SessionAttribute.

    Should I need to use Session scopped bean like before, and <aop:scoped-proxy/> definition in xml file?

    Thanks
    Sam

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    As mentioned read the reference guide.

    Quote Originally Posted by mdeinum
    In short adding @SessionAttribute doesn't auto magically put a bean in your session you still need something that creates the model object (spring will store it in the session because that is what @SessionAttribute is telling spring to do).
    That something is a method annoted with @ModelAttribute or the initial GET method that is executed to render/select the page.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  8. #8
    Join Date
    Jul 2008
    Posts
    182

    Default

    I still can't see the reference guide telling me how to do this.
    Thanks
    Samuel

  9. #9
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    No it doesn't give you a step by step explanation, hell that would be impossible to explain all different combinations step-by-step...

    As mentioned READ... (and I already gave you the hint on how to implement it in my previous post which should alse be read!). But here goes.

    1. Read about how to expose model attributes (section 16.3.3.7)
    2. Read about the SessionAttributes (section 16.3.3.9)
    3. Combine 1 + 2

    Note: instead of 1 you can also use the inital GET method (as mentioned before) to add the object to the model.
    Last edited by Marten Deinum; Sep 18th, 2012 at 07:10 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •