8 Replies Latest reply on Dec 4, 2009 5:20 PM by mikkus70

    How to give relative path in seam?

    ssahni

      I am writing code for developing a site. I am reading the XML for showing some values in dropdown. Please let me know where to keep this XML file? I mean in WEB-INF or in META-INF etc?


      Also please let me know how to refer this file through relative path. Whatsoever I give it takes through the c drive? I want to give relative path so that it does not effect code while moving in production? Please suggest.


      DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                  DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                  Document doc = docBuilder.parse(new File("Try.xml"));
      



        • 1. Re: How to give relative path in seam?
          mwohlf

          afaik META-INF is not for storing parts of your application, the only thing I keep there is persistence.xml, and MANIFEST.MF is generated by the war packer I think...


          you can get the path for a file config.xml in WEB-INF like this:



          ServletContext ctx = ServletLifecycle.getServletContext();
          String path = ctx.getRealPath("WEB-INF/config.xml");



          let me know if / in the path works on windows though, maybe you have to use java.io.File.pathSeparator

          • 2. Re: How to give relative path in seam?
            ssahni

            Is there no other way of doing so?


            As when I did as suggested I got the following compile time error.


            package javax.servlet does not exist
            



            I searched this on net. Tried all the solutions but I am not able to get away with this error.


            I am using Netbeans 6.5, jboss 4.2.0 GA, seam etc.  I could not find servlet.jar in anywhere on my machine. So,I put the servlet-api.jar in classpath but could not proceed.
            I found servlet-api.jar  in jboss-4.2.0.GA\server\default\lib directory. I even put servlet-api.jar in WEB-INF\lib and included it in classpath but still could not get this error removed.


            Please let me know is there any other way or I have to try on this only?

            • 3. Re: How to give relative path in seam?

              if package javax.servlet does not exist your applications should not even run, javax.servlet is a core requirement for any web based java application (with or without  Seam)


              Are you sure you correctly created a web project in netbeans?

              • 4. Re: How to give relative path in seam?
                ssahni

                I imported an existing project and the project which I imported is 'Web Free-Form Application'


                And since the project already contains the build folder so I am not able to create a 'Web Application with existing resources'.


                When I access the application using 'http:////localhost:8080//' I am able to access its pages.


                So please let me know where is the mistake. Does for 'Web Free-Form Application' it is not supposed to work?


                Thanks

                • 5. Re: How to give relative path in seam?

                  What IDE was used to create that existing project? Or is it compiled using a custom ant file?

                  • 6. Re: How to give relative path in seam?
                    ssahni

                    Netbeans was used to create that existing project. It has an custom ant file which is used to build that project.


                    Does that matter?


                    Thanks

                    • 7. Re: How to give relative path in seam?
                      keeper

                      Simple test:


                      If you are using jbosstools, try creating a blank seam project and run it to test your environment. You should then import the classes and views from the other project.

                      • 8. Re: How to give relative path in seam?
                        mikkus70

                        When I have config files I need to retrieve from code, I usually put them under WEB-INF/classes and load them as classpath resources:


                        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                                DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                                Document doc = docBuilder.parse(new File(this.getClass().getClassLoader().
                                getResourceAsStream("/Try.xml"));
                        



                        This also works if you put the file in a subdir of WEB-INF/classes (just specify its path relative to WEB-INF/classes).