Announcement Announcement Module
No announcement yet.
Rookie Question about View Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Rookie Question about View


    I need to manage a lot of pdf documents in my system.

    Can i just do something like this?

    <bean id="pdfViewResolver" class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
    <property name="prefix" value="/WEB-INF/templates/"/>
    <property name="suffix" value=".pdf"/>

    so when my action controller received the request by document id like doc901, I just return "new ModelAndView("doc901")"
    a pdf document at /WEB-INF/templates/doc901.pdf should be displayed to the user.

    I have tried this, but it fails to display the pdf document.

    Anyone can help me.


  • #2
    Why don't you simply put the .pdf in some directory on your webapp's root and let it be served by your appserver?


    No need to configure anything.


    • #3
      Do you want to open a link or do you want to stream the pdf to the client? It might be nessary to change the header. Have a look at the abstract pdf view.


      • #4

        for secuity reason, i need to put all pdf documents under WEB-INF folder, (I will verify user's privilege first) how can i put a direct link under this situation?

        anyone can help me?



        • #5
          To return a PDF to a browser I simply use the AbstractPdfView Class

          There's a example in the countries example included in the distribution.

          You should try to have a look

          here's a very simple example :

          my controller:
          public class Send2Page implements Controller {

          public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
          return new ModelAndView(new HelloPdfView());


          Here's the abstractPdfView

          public class HelloPdfView extends AbstractPdfView {

          protected void buildPdfDocument(Map model, Document document, PdfWriter writer,
          HttpServletRequest request, HttpServletResponse response)
          throws DocumentException, NoSuchMessageException {

          document.add(new Paragraph("Hello World"));
          document.add(new Paragraph("Salut, hello in french"));
          Paragraph paragraph = new Paragraph();
          paragraph.add("je suis vraiment hot");
          paragraph.add("Sans Modestis ");
          paragraph.add("une cellule ca fait quoi?");




          And the config




          <!--you will need you create a beanNameUrlHanlderMapping, if you are not
          creating a SimpleUrlHandlerMapping-->

          <bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.Bea nNameUrlHandlerMapping"/>

          <bean name="/showme.pdf" class="web.Send2Page">

          good luck man

          Now I'm trying to open the pdf in a new window(new browser), so the user
          don't have to click back in order to get back to application.
          Do you where I can find the information?


          • #6

            i have static pdf documents created already (all are located under /WEB-INF/templates/pdf/). what i need to do is to collect pdf name from user, then return the specified pdf document to the user.



            • #7
              You understand your need now,

              I don't know how to do it, but I guess will be to
              render the pdf that user asked in a AbstractView and
              then return it in a controller.

              You should take a look at the class abstractPdfView and

              try to look a the source that comes with spring

              try to look at how the people of Spring did extend AbstractPdfView
              from AbstractView.

              Also I remember that when I did that kinda stuff in the old fashion
              way (servlet) I need to use
              that line of command

              read a pdf file,
              and write it to the output stream (response.getOutputStream()

              can't find example right now. sorry

              But my advice before returning to the servlet mode,
              try to a have a peek on the spring code. It comes with the
              distribution, lot's a interesting stuff in.