Announcement Announcement Module
Collapse
No announcement yet.
Need help with SpringMVC and PDF [Solved] Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help with SpringMVC and PDF [Solved]

    Hi,

    I read the related chapter in the reference about subclassing a AbstractPdfView. But I canīt get it working all.

    I want to combine Tapestry with Spring, but still use Spring MVC to create PDF files on the fly.

    My web.xml (DTDs ommited intentionally):
    Code:
    <web-app>
    
    	<display-name>FlexLayout</display-name>
    
        <filter>
            <filter-name>redirect</filter-name>
            <filter-class>org.apache.tapestry.RedirectFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>redirect</filter-name>
            <url-pattern>/</url-pattern>
        </filter-mapping>
    
    	<servlet>
    		<servlet-name>flexlayout</servlet-name>
    		<servlet-class>org.apache.tapestry.ApplicationServlet</servlet-class>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    	<servlet>
    		<servlet-name>flpdfpreview</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    
    	<servlet-mapping>
    		<servlet-name>flexlayout</servlet-name>
    		<url-pattern>/app</url-pattern>
    	</servlet-mapping>
    	<servlet-mapping>
    		<servlet-name>flpdfpreview</servlet-name>
    		<url-pattern>/pdf</url-pattern>
    	</servlet-mapping>
    
    	<welcome-file-list>
    		<welcome-file>index.html</welcome-file>
    	</welcome-file-list>
    
    </web-app>
    FlexLayoutEngine.java (for Tapestry):
    Code:
    package gtds.web;
    
    import java.util.Map;
    
    import org.apache.tapestry.engine.BaseEngine;
    import org.apache.tapestry.request.RequestContext;
    import org.springframework.context.ApplicationContext;
    import org.springframework.web.context.support.WebApplicationContextUtils;
    
    public class FlexLayoutEngine extends BaseEngine {
    
    	public static final String APPLICATION_CONTEXT_KEY = "appContext";
    
    	/**
    	* @see org.apache.tapestry.engine.AbstractEngine#setupForRequest(org.apache.tapestry.request.RequestContext)
    	*/
    	protected void setupForRequest(RequestContext context) {
    		super.setupForRequest(context);
    		// insert ApplicationContext in global, if not there
    		Map global = (Map) getGlobal();
    		ApplicationContext ac = (ApplicationContext) global.get(APPLICATION_CONTEXT_KEY);
    		if (ac == null) {
    			ac = WebApplicationContextUtils.getWebApplicationContext(
    					context.getServlet().getServletContext()
    			);
    			global.put(APPLICATION_CONTEXT_KEY, ac);
    		}
    	}
    
    }
    flpdfpreview-servlet.xml (Spring MVC):
    Code:
    <beans>
    
        <bean id="flSpringMVController" class="gtds.web.SpringMVController"/>
    
        <!-- <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="/pdf/*.pdf">flSpringMVController</prop>
                </props>
            </property>
        </bean> -->
    
    	<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
        	<property name="basename"><value>views</value></property>
    	</bean>
    
    </beans>
    views.properties (Spring MVC):
    Code:
    pdf.class=gtds.web.PreviewPdfView
    pdf.url=/pdf/preview.pdf
    SpringMVController.java (Spring MVC):
    Code:
    package gtds.web;
    
    import org.springframework.web.servlet.mvc.Controller;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import java.io.IOException;
    
    public class SpringMVController implements Controller {
    
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            return new ModelAndView("pdf");
    
        }
    }
    PreviewPdfView.java (Spring MVC):
    Code:
    package gtds.web;
    
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.view.document.AbstractPdfView;
    
    import com.lowagie.text.Document;
    import com.lowagie.text.PageSize;
    import com.lowagie.text.pdf.PdfWriter;
    
    public class PreviewPdfView extends AbstractPdfView {
    
    	/* (non-Javadoc)
    	 * @see org.springframework.web.servlet.view.document.AbstractPdfView#buildPdfDocument(java.util.Map, com.lowagie.text.Document, com.lowagie.text.pdf.PdfWriter, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
    	 */
    	protected void buildPdfDocument(
    			Map model,
    			Document doc,
    			PdfWriter writer,
    			HttpServletRequest request,
    			HttpServletResponse response)
    			throws Exception {
    
    		doc.setPageSize(PageSize.A4);
    		doc.addAuthor("FlexLayout Professionelle Layouts Service");
    		doc.addTitle("FlexLayout Professionelle Layouts Service");
    		doc.addSubject("FlexLayout Professionelle Layouts Service");
    		doc.open();
    		doc.close();
    		
    	}
    
    }
    Can someone help?

    Regards.

    P.S. This is a follow-up of Spring MVC with Tapestry.
    Last edited by Rod Johnson; Jan 18th, 2006, 11:04 AM.

  • #2
    You shouldn't need to open and close the document, but that shouldn't make a difference.

    Maybe you can indicate what the actual problem is. At first glance (without studying the code thoroughly) I can't really see what could be wrong.

    thanx,

    Alef

    Comment


    • #3
      This actual problem is that:

      I want to load a PDF page into a frame. The most of my site is done using Tapestry but the PDF pages are accessors of SpringMVCīs AbstractPdfView.

      I canīt identify the correct URL I need to load the PDF view into a frame.

      If I say in CustomizeLayout.html (a Tapestry page)
      Code:
      <html jwcid="@Shell" title="Customize Layout - FlexLayout" stylesheet="ognl&#58;assets.stylesheet">
      <frameset rows="64,*">
          <frame jwcid="CustomizeLayoutEditFrame" name="edit" scrolling="no" />
          <!-- <frame jwcid="CustomizeLayoutPreviewFrame" name="view" /> -->
          <frame src="pdf/preview.pdf"            name="view" />
      	<noframes>You need a browser with frames support.</noframes>
      </frameset>
      </html>
      Tomcat issues an error message:
      Code:
      HTTP Status 404 - /flpdfpreview/pdf/preview.pdf
               type  Status report
         message   /flexlayout/pdf/preview.pdf
      description  The requested resource &#40;/flpdfpreview/pdf/preview.pdf&#41; is not available.
      Regards.

      Comment


      • #4
        Code:
        HTTP Status 404 - /flpdfpreview/pdf/preview.pdf
        Where does the flpdfpreview come from. From the servlet-mapping.

        You're mapping the pdf servlet to /pdf and the other one to flpdfpreview.

        Try using /flexlayout/pdf/preview.pdf (I assume the flexlayout is the context in which the war runs?)

        Alef

        Comment


        • #5
          Alef,

          Originally posted by Alef Arendsen
          Try using /flexlayout/pdf/preview.pdf (I assume the flexlayout is the context in which the war runs?)
          Currently I have this web.xml (http://localhost:8080/ and http://localhost:8080/app shall be handled by Tapstry, http://localhost:8080/pdf shall be handled by SpringMVC and my AbstractPdfView impl.):
          Code:
          <web-app>
          
          	<display-name>FlexLayout</display-name>
          
              <filter>
                  <filter-name>redirect</filter-name>
                  <filter-class>org.apache.tapestry.RedirectFilter</filter-class>
              </filter>
              <filter-mapping>
                  <filter-name>redirect</filter-name>
                  <url-pattern>/</url-pattern>
              </filter-mapping>
          
          	<servlet>
          		<servlet-name>flexlayout</servlet-name>
          		<servlet-class>org.apache.tapestry.ApplicationServlet</servlet-class>
          		<load-on-startup>1</load-on-startup>
          	</servlet>
          	<servlet>
          		<servlet-name>flpdfpreview</servlet-name>
          		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          		<load-on-startup>1</load-on-startup>
          	</servlet>
          
          	<servlet-mapping>
          		<servlet-name>flexlayout</servlet-name>
          		<url-pattern>/app</url-pattern>
          	</servlet-mapping>
          	<servlet-mapping>
          		<servlet-name>flpdfpreview</servlet-name>
          		<url-pattern>/pdf</url-pattern>
          	</servlet-mapping>
          
          	<welcome-file-list>
          		<welcome-file>index.html</welcome-file>
          	</welcome-file-list>
          
          </web-app>
          The servlet flpdfpreview ist further configured in flpdfpreview-servlet.xml (As you see, I donīt know how to configure urlMapping):
          Code:
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
          
          <beans>
          
          	<bean id="flSpringMVController" class="gtds.web.SpringMVController"/>
          
          	<!-- <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
          		<property name="mappings">
          			<props>
          				<prop key="/pdf/*.pdf">flSpringMVController</prop>
          			</props>
          		</property>
          	</bean> -->
          
          	<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
              	<property name="basename"><value>views</value></property>
          	</bean>
          
          </beans>
          Of course I have a views.properties:
          Code:
          pdf.class=gtds.pdf.PreviewPdfView
          pdf.url=/pdf/preview.pdf
          CustomizeLayout.java looks like this (with that the browser tires to load http://localhost:8080/flexlayout/pdf/preview.pdf into the lower frame):
          Code:
          <html jwcid="@Shell" title="Customize Layout - FlexLayout" stylesheet="ognl&#58;assets.stylesheet">
          <frameset rows="64,*">
              <frame jwcid="CustomizeLayoutEditFrame" name="edit" scrolling="no" />
              <frame src="pdf/preview.pdf"            name="view" />
          	<noframes>You need a browser with frames support.</noframes>
          </frameset>
          </html>
          I get this response in the PDF frame:
          Code:
          HTTP Status 404 - /flexlayout/pdf/preview.pdf 
          * * * * *type* Status report 
          * *message */flexlayout/pdf/preview.pdf 
          description* The requested resource &#40;/flexlayout/pdf/preview.pdf&#41; is not available.
          Do do see my mistake?

          Regards.

          Comment


          • #6
            If you comment out the urlMapping, nothing will work. Remember: 1) request comes in, 2) gets mapped to a controller (flSpringMVCController) by a URLHandlerMapping which 3) returns a ModelAndView (in this case: pdf) which 4) gets rendered. You can't directly point your browser to views or something.

            Remove the comments from the urlMapping and see if /pdf/prefview.pdf works (try it in a separate browser window, without the frame). It should.

            Alef

            Comment


            • #7
              Alef,

              Originally posted by Alef Arendsen
              Remove the comments from the urlMapping and see if /pdf/prefview.pdf works (try it in a separate browser window, without the frame). It should.
              I did so but nothing changed.

              Regards.

              Comment


              • #8
                Need help with SpringMVC and PDF [Solved]

                SOLVED.

                After tweaking URl mapping, views.properties and web.xml I finally solved that problem.

                Thank you for your help.

                Regards.

                Comment

                Working...
                X