Announcement Announcement Module
Collapse
No announcement yet.
How to initialize spring session bean. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to initialize spring session bean.

    Hi,
    With Spring mvc 3.1, I created a session-scoped bean.
    I use this session-scoped bean to hold/pass an image byte[] from a child page to a parent page, whereas the parent page will display the image keep in the session bean after the child-page uploaded the image.

    But every first time when I uploaded the image and submitted the child-page, child-page refresh the parent page, the javascript refresh is not able to populate the image into the session bean. After I manually hit the refresh button of the parent page on the browser or press Enter key on the browser or if I upload the image and submit the child-page again, the image is session bean will get populated with the image therefore the parent page is able to show up the image.

    Here is the controller:

    Code:
    Controller
    public class CategoryController {
    
        private static final Logger logger = Logger.getLogger(CategoryController.class);
    
        private CategoryService categoryService;
        private CategoryImageService categoryImageService;
        
        @Autowired
        CategoryPageBean sessionCategoryPageBean;
        
        @Autowired
        public CategoryController(CategoryService categoryService) {
    	this.categoryService = categoryService;
        }
    ....

    Parent page:

    Code:
    <form:form id="form" name="add_category_form" modelAttribute="categoryPageBean" action="save" method="post">
                        <form:errors path="*" class="error" element="div" />
    ...
    <c:choose>
                            <c:when test='${empty categoryPageBean.imageTmp}'>
                                <h3 class="bot-3">Upload Image</h3>
                                  <div class="btns"><a onClick="javascript: openUploadImagePage();" data-type="submit" class="button-1" >Upload Image</a></div>
                            </c:when>
                            <c:otherwise>
                                <span class="label">Uploaded Image:</span>
                                <spring:url value="/categories/tmp_thumbnail/${categoryId}" var="url" htmlEscape="true" />
                                <img alt="" src="${url}" />
                                <div class="btns"><a onClick="javascript: openUploadImagePage();" data-type="submit" class="button-1" >Upload Image</a></div>
                            </c:otherwise>
                          </c:choose>
    ...
    <div class="btns"><a data-type="reset" class="button-1" >Clear</a><a onClick="javascript: submitform();" data-type="submit" class="button-1" >Save</a></div>
                        </fieldset>
                    </form:form>
    <script type="text/javascript">
        function submitform() {
                this.document.forms['supplier_form'].submit();
              }
              
        function openUploadImagePage() {
            window.open('../categories/uploadCategoryImage/popupUploadImage?categoryId=${categoryId}&parentCategory=${categoryPageBean.parentCategory}&siblingCategory=${categoryPageBean.siblingCategory}&childCategory=${categoryPageBean.childCategory}','Upload Category Image','width=600,height=400');
        }
       </script>

    Child page:
    Code:
    <script language="JavaScript">
             function refreshParent() {
                 var URL = unescape(window.opener.location.pathname);  
                 var PARMS = unescape(window.opener.location.search);
    
    //            window.opener.location.href=window.opener.location.href;
    
                var ms = new Date().getTime();
                window.opener.location.href="/HousewareAdmin-web/categories/create"+ms+PARMS;
                window.close();
             }
             
             function submitUploadForm(){   
                 this.document.forms['uploadImagePopupForm'].submit();
                 refreshParent(); 
             }
              
    </script>
    <body>
    <form:form modelAttribute="categoryPageBean" name="uploadImagePopupForm" method="post"
    	enctype="multipart/form-data" onSubmit="return Validate();">
    	<fieldset><legend>Upload Category Image</legend>
    ...
    <tr>  
    			<td><form:label for="fileData" path="fileData">Image File</form:label><br />
    			</td>
    			<td><form:input path="fileData" id="image" type="file" /></td>
                    </tr>
                    <tr>
    			<td><br />
    			</td>
                            <!--
    			<td><input type="submit" value="Upload" /></td>
                            -->
                            <td>
                            <a onClick="submitUploadForm();" data-type="submit" class="button-1" >Upload File</a>
                            </td>
    		</tr>
    	</table>
    	</fieldset>
    </form:form>
    </body>
    webmvc-config.xml file:

    Code:
    <bean id="categoryPageBean" class="ix.houseware.category.bean.CategoryPageBean" scope="session" >
               <!-- this next element effects the proxying of the surrounding bean -->
               <aop:scoped-proxy/>
            </bean>
    Session bean:
    Code:
    @Component
    @Scope("session")
    public class CategoryPageBean{

    Very appreciate for any suggestion to resolve this issue.
    Thanks
    Sam
Working...
X