Announcement Announcement Module
Collapse
No announcement yet.
[solved] Newbie question: Generating an Excel View Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [solved] Newbie question: Generating an Excel View

    Hi.. I'm a newbie in java and spring framework.. so I'm sorry if my errors are a bit.. dumb or something..

    I've tried the Countries sample provided with the spring framework distribution.. and it worked perfectly..

    but when I try to use the same code to generate an excel view in my application.. I always encountered this exception:

    Code:
    java.lang.NullPointerException
    	com...OvertimeExcelView.buildExcelDocument(OvertimeExcelView.java:44)
    	org.springframework.web.servlet.view.document.AbstractExcelView.renderMergedOutputModel(AbstractExcelView.java:140)
    	org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:219)
    	org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:630)
    	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:523)
    	org.springframework.web.servlet.FrameworkServlet.serviceWrapper(FrameworkServlet.java:342)
    	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:328)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
    in my servlet xml:

    Code:
    	<bean id="overTimeFormController" 
    			class="com...OverTimeFormController">
    		<property name="commandName"><value>user</value></property>
    		<property name="commandClass">
    			<value>com...User</value>
    		</property>
    		<property name="formView">
    			<value>overtimeView</value>
    		</property>
    		<property name="successView">
    			<value>overTime.spring</value>
    		</property>	
    		<property name="validator"><ref bean="beanValidator" /></property>
    	</bean>
    I used a class named User to contain all the data to be shown in the view..
    and my controller class code is like this:

    Code:
    public class OverTimeFormController extends SimpleFormController &#123;
        
        /* &#40;non-Javadoc&#41;
         * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit&#40;javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException&#41;
         */
        protected ModelAndView onSubmit&#40;HttpServletRequest request,
                HttpServletResponse response, Object command, BindException binder&#41;
                throws Exception &#123;
            
            if&#40;request.getParameter&#40;"excel"&#41;!=null&#41;&#123;
    
                RefreshablePagedListHolder listHolder = 
        		    &#40;RefreshablePagedListHolder&#41; 
        		    	request.getSession&#40;true&#41;.getAttribute&#40;"user"&#41;;
        		return new ModelAndView&#40;"overtime_excelView", "user", listHolder&#41;;
                
            &#125;else&#123;
    	        User user = &#40;User&#41; command;
    	        BindException errors = new BindException&#40;user, "user"&#41;;
    	        return new ModelAndView&#40;"overtimeView", errors.getModel&#40;&#41;&#41;;            
            &#125;
        &#125;
        
    &#125;
    everything is working perfectly, the validation using commons validator, viewresolving using resource bundles.. but when I try to export to excel I always get a null pointer exception

    like you can see in the controller, the flow is like this..
    1. the user submitted the form with some parameters (like employee number and date), and the view is returned back to the user listing his overtime data.. (the application is perfectly working until this step)
    2. if the user pressed the export to excel button.. the controller will then generate the same form in excel format.. (this is where i get the exception)

    I also tried supplying the User object in formBackingObject.. but that didnt seem to help..

    what am I doing wrong? And am I supplying enough code? please tell me if i dont supply enough code...
    any help would be appreciated.. thank you

  • #2
    From the stackTrace, the exception is thrown in
    Code:
      com...OvertimeExcelView.buildExcelDocument
    could you provide the source, or an excerpt for this class?

    Comment


    • #3
      Oh.. of course..

      this is the source of OverTimeExcelView.java:

      Code:
      /*
       * Created on Aug 20, 2004
       *
       * TODO To change the template for this generated file go to
       * Window - Preferences - Java - Code Generation - Code and Comments
       */
      package com...view;
      
      import java.util.List;
      import java.util.Locale;
      import java.util.Map;
      
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      
      import org.apache.poi.hssf.usermodel.HSSFCell;
      import org.apache.poi.hssf.usermodel.HSSFRow;
      import org.apache.poi.hssf.usermodel.HSSFSheet;
      import org.apache.poi.hssf.usermodel.HSSFWorkbook;
      import org.springframework.beans.support.RefreshablePagedListHolder;
      import org.springframework.web.servlet.view.document.AbstractExcelView;
      
      /**
       * @author Administrator
       *
       * TODO To change the template for this generated type comment go to
       * Window - Preferences - Java - Code Generation - Code and Comments
       */
      public class OvertimeExcelView extends AbstractExcelView &#123;
      
          /* &#40;non-Javadoc&#41;
           * @see org.springframework.web.servlet.view.document.AbstractExcelView#buildExcelDocument&#40;java.util.Map, org.apache.poi.hssf.usermodel.HSSFWorkbook, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse&#41;
           */
          protected void buildExcelDocument&#40;Map model, HSSFWorkbook wb,
                  HttpServletRequest request, HttpServletResponse response&#41;
          throws Exception &#123;
              
              HSSFSheet sheet;
              HSSFRow sheetRow;
              HSSFCell cell;
      
      		// We search the data to insert
      		RefreshablePagedListHolder pgHolder = &#40;RefreshablePagedListHolder&#41; model.get&#40;"user"&#41;;
      		Locale loc = pgHolder.getLocale&#40;&#41;;
      
      		sheet = wb.createSheet&#40;"Spring"&#41;;
      
      		if &#40;null == pgHolder&#41; &#123;
      			getCell&#40;sheet, 0, 0&#41;.setCellValue&#40;getMessageSourceAccessor&#40;&#41;.getMessage&#40;"nolist", loc&#41;&#41;;
      			return;
      		&#125;        
              
              // Go to the first sheet
              // getSheetAt&#58; only if wb is created from an existing document
              //sheet = wb.getSheetAt&#40; 0 &#41;;
              sheet.setDefaultColumnWidth&#40;&#40;short&#41;12&#41;;
      
              // write a text at A1
              cell = getCell&#40; sheet, 0, 0 &#41;;
              setText&#40;cell,"Spring-Excel test"&#41;;
      
              List words = &#40;List &#41; model.get&#40;"list"&#41;;
              for &#40;int i=0; i < words.size&#40;&#41;; i++&#41; &#123;
                  cell = getCell&#40; sheet, 2+i, 0 &#41;;
                  setText&#40;cell, &#40;String&#41; words.get&#40;i&#41;&#41;;
              &#125;
      
          &#125;
      
      &#125;
      but i guess the exception is thrown before this class is called/instantiated..
      i just copied the example in the spring api for this class..

      Comment


      • #4
        java.lang.NullPointerException is thrown in line 44 in OvertimeExcelView.java
        Code:
          Locale loc = pgHolder.getLocale&#40;&#41;;
        this means that model.get("user") returns null.

        Attribute "user" is extracted from HttpSession in OverTimeFormController
        Code:
          RefreshablePagedListHolder listHolder = 
                      &#40;RefreshablePagedListHolder&#41;
                         request.getSession&#40;true&#41;.getAttribute&#40;"user"&#41;;
        can you check if the returned value is not null?

        Comment


        • #5
          yes.. i guess that's the problem.. the object "user" is not in the session..
          i already checked that

          Code:
          RefreshablePagedListHolder listHolder = 
                        &#40;RefreshablePagedListHolder&#41; 
                           request.getSession&#40;true&#41;.getAttribute&#40;"user"&#41;;
          returned null.. but like i said before.. I already tried to supply that object in the method formBackingObject in the controller.. but that didn't help...

          i really dont know where the problem is.. or why that returned null... because when I tried to show the view in plain html.. using jstl.. there's no problem at all..

          Comment


          • #6
            Are the user use supply to jstl and the one you want to use in Excel the same?

            Comment


            • #7
              yes of course.. all the data I want to process is in the user object..

              Comment


              • #8
                so why not just do
                Code:
                        User user = &#40;User&#41; command; 
                        if&#40;request.getParameter&#40;"excel"&#41;!=null&#41;&#123; 
                          return new ModelAndView&#40;"overtime_excelView", "user", user&#41;; 
                            
                        &#125;else&#123; 
                           BindException errors = new BindException&#40;user, "user"&#41;; 
                           return new ModelAndView&#40;"overtimeView", errors.getModel&#40;&#41;&#41;;            
                        &#125;

                Comment


                • #9
                  oh... yes2 that worked.. thanks a lot..

                  silly me.. I thought you must always use a refreshablepagedlistholder to contain the object to be shown in the excel view..

                  i guess i still have a lot to learn about java...

                  so.. here's what i did.. just in case some newbie like me want to know how to do it :oops: :

                  in ModelAndView:

                  Code:
                          User user = &#40;User&#41; command; 
                          if&#40;request.getParameter&#40;"excel"&#41;!=null&#41;&#123; 
                            return new ModelAndView&#40;"overtime_excelView", "user", user&#41;; 
                              
                          &#125;else&#123; 
                             BindException errors = new BindException&#40;user, "user"&#41;; 
                             return new ModelAndView&#40;"overtimeView", errors.getModel&#40;&#41;&#41;;            
                          &#125;
                  in OvertimeExcelView.java:

                  Code:
                  public class OvertimeExcelView extends AbstractExcelView &#123;
                  
                      /* &#40;non-Javadoc&#41;
                       * @see org.springframework.web.servlet.view.document.AbstractExcelView#buildExcelDocument&#40;java.util.Map, org.apache.poi.hssf.usermodel.HSSFWorkbook, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse&#41;
                       */
                      protected void buildExcelDocument&#40;Map model, HSSFWorkbook wb,
                              HttpServletRequest request, HttpServletResponse response&#41;
                      throws Exception &#123;
                          
                          HSSFSheet sheet;
                          HSSFRow sheetRow;
                          HSSFCell cell;
                  
                         sheet = wb.createSheet&#40;"Spring"&#41;;
                          sheet.setDefaultColumnWidth&#40;&#40;short&#41;12&#41;;
                  
                          // write a text at A1
                          cell = getCell&#40; sheet, 0, 0 &#41;;
                          setText&#40;cell,"Spring-Excel test"&#41;;
                  
                          User user = &#40;User&#41; model.get&#40;"user"&#41;;
                          List words = user.getList&#40;&#41;;
                  
                          ...
                  
                      &#125;
                  
                  &#125;

                  Comment


                  • #10
                    how did you define overtime_excelView in configuration file? do you define it as a page or something else?

                    thanks

                    Comment


                    • #11
                      i define in WEB-INF/xxx-servlet.xml :
                      Code:
                      	<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
                      		<property name="basename"><value>xxx-views</value></property>
                      		<property name="defaultParentView"><value>jspView</value></property>
                      	</bean>
                      and then i have in WEB-INF/classes/xxx-views.properties :
                      Code:
                      jspView.class=org.springframework.web.servlet.view.JstlView
                      jspView.requestContextAttribute=rc
                      
                      overtime_excelView.class=org.package.OvertimeExcelView
                      which is a class that extends
                      org.springframework.web.servlet.view.document.Abst ractExcelView

                      Comment


                      • #12
                        I followed your steps, but my case still has problem. I notice that in your xxx-views.properties you only have xxx.class=someValue without using xxx.url=someValue.

                        I have no idea about my problem, if you or some body can see my thread:
                        http://forum.springframework.org/showthread.php?t=41676

                        Comment

                        Working...
                        X