1. #1
    Sencha User
    Join Date
    Apr 2010
    Posts
    55
    Vote Rating
    0
    cavalleydude is on a distinguished road

      0  

    Default ExtJS access to a properties file content loaded via index.jsp page

    ExtJS access to a properties file content loaded via index.jsp page


    Hello, I need to externalize some of my webapp services (paths to services used in my application). I have a properties file (my.properties) located on the same server as tomcat that's running my ExtJS webapp ( /privatefolder/my.properties ) for instance. I want to be able from JavaScript (or ExtJS framework) to access those properties that are loaded into memory... how can I do this?

    I use my index.jsp page to load this file as such (here is a snippet from the index.jsp):

    Code:
    ...
    
    <script type="text/javascript" src="lib/extjs/adapter/ext/ext-base-debug.js"></script>
    <script type="text/javascript" src="lib/extjs/ext-all-debug.js"></script>
    
    <%@page import="java.io.FileInputStream" %>
    <%@page import="java.util.Properties" %>
    
    <%
    Properties properties = new Properties();
    properties.load(new FileInputStream("/privatefolder/my.properties"));
    out.println( properties.getProperty("profileservice") );
    %>
    
    ...
    // The properties file is loading fine, I see the output displayed for the property "profileservice", but I want to be able to read those properties elsewhere in my ExtJS code... is this possible?

    I've searched the net looking for clues, but so far nothing concrete.
    I've never done this before, but seems natural, any help would be greatly appreciated...

    OR perhaps I'm going about this all wrong , is there a better way to externalize a properties file outside my app (war file) for ExtJS applications?
    Last edited by cavalleydude; 8 Oct 2010 at 7:32 AM. Reason: add more details

  2. #2
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    97
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    Create a .jsp that outputs all properties as a JSON object that is assigned to a variable, e.g.
    Code:
    Ext.ns('MyNamespace');
    MyNamespace.properties = {
      // dynamically filled from /privatefolder/my.properties
      profileservice: 'abc',
      otherproperty: 123
    };
    Then, simply include this .jsp with a <script> tag.

  3. #3
    Sencha User
    Join Date
    Apr 2010
    Posts
    55
    Vote Rating
    0
    cavalleydude is on a distinguished road

      0  

    Default


    Okay, that sounds good. I'm trying to understand how to write this jsp file. How do I read the properties into the json object?

    My new jsp file will be myappservices.jsp
    Code:
    <%@page import="java.io.FileInputStream" %> 
    <%@page import="java.util.Properties" %>  
    <% Properties properties = new Properties(); %>
    <% properties.load(new FileInputStream("/privatefolder/my.properties"));%> 
    // The question is, how do I get from properties into the json object values below?
    <json:object>
      <json:property name="profileservice" value="${element.profileservice}" />
      <json:property name="demoservice" value="${element.demoservice}" />   
      <json:property name="authservice" value="${element.authservice}" />
    </json:object>

  4. #4
    Sencha User
    Join Date
    Apr 2010
    Posts
    55
    Vote Rating
    0
    cavalleydude is on a distinguished road

      0  

    Default


    Hi Condor, I think I know how to do this... I was not thinking. I'll try it and post results shortly.

  5. #5
    Sencha User
    Join Date
    Apr 2010
    Posts
    55
    Vote Rating
    0
    cavalleydude is on a distinguished road

      0  

    Default


    Actually, do you have an example of how to do this? How do I get the namespace assigned, etc. It sounds like I'm going to write the output as a <script> </script> with the JSON object assigned to a var object. It would really help to see an example... if you have one.

  6. #6
    Sencha User
    Join Date
    Apr 2010
    Posts
    55
    Vote Rating
    0
    cavalleydude is on a distinguished road

      0  

    Default


    Okay, SOLVED... Condor thank you for the help. I found a slightly different way to do it.

    A file resides on the server named "myfile.properties" in a private folder (not in the web server) and contains the following:

    Code:
     devBaseServiceUri=http://adiffdevserver/rest
     testBaseServiceUri=http://adifftestserver/rest
     prodBaseServiceUri=http://adiffprodserver/rest

    I created a new javascript file named "MYAPP.js"
    with contents

    Code:
    MYAPP = {
    
    }
    /**
     * Constants for MYAPP
     */
    MYAPP.Constants = {
        devBaseServiceUri :'http://mydevserver/rest',
        testBaseServiceUri : 'http://mytestserver/rest',
        prodBaseServiceUri : 'http://myproductionserver/rest'
    }
    Then inside the index.jsp it will load my java properties file, and set the values in the environment.
    A final script will read environment values into my JavaScript object variables.

    Code:
    <script type="text/javascript" src="js/MYAPP.js"></script>
    
    <%@page import="java.io.FileInputStream" %>
    <%@page import="java.util.Properties" %>
    
    <%
    Properties properties = new Properties();
    properties.load(new FileInputStream("/privatefolder/myfile.properties"));
    System.setProperty("mydevservername", properties.getProperty("devBaseServiceUri") );
    System.setProperty("mytestservername", properties.getProperty("testBaseServiceUri") );
    System.setProperty("myprodservername", properties.getProperty("prodBaseServiceUri") );
    %>
    
    <script type="text/javascript">
        MYAPP.Constants.devBaseServiceUri = "<%= System.getProperty("mydevservername")%>";
        MYAPP.Constants.testBaseServiceUri = "<%= System.getProperty("mytestservername")%>";
        MYAPP.Constants.prodBaseServiceUri = "<%= System.getProperty("myprodservername")%>";
    </script>
    Then inside the application anywhere, you can call MYAPP.Constants.devBaseServiceUri for the value read from the properties file.

    Anyhow,... thank you so much. You got me on the right track.

  7. #7
    Sencha User
    Join Date
    Jun 2011
    Posts
    25
    Vote Rating
    0
    sanjayarrk1 is on a distinguished road

      0  

    Default How to read constants file using html file or using .js file only?

    How to read constants file using html file or using .js file only?


    Hi, I am using simple HTML file for my extJS project. How can I load/read a properties file or constants file using that HTML file or .js file?


    any kind of help will be appreciated.
    Thanks in advanced.

Similar Threads

  1. Help:extJS with JSP page
    By kunalkrishna in forum Ext 2.x: Help & Discussion
    Replies: 6
    Last Post: 10 Aug 2011, 7:14 AM
  2. opening a jsp page in ExtJs window
    By himan in forum Ext: Q&A
    Replies: 2
    Last Post: 7 Jul 2010, 1:31 PM
  3. Executing extjs code in page loaded through extjs
    By smoothiegunner in forum Ext 2.x: Help & Discussion
    Replies: 2
    Last Post: 8 Feb 2010, 5:27 PM
  4. How to load extjs datagrid in beteen a JSP page.
    By bhanuprakash_kavi in forum Ext 3.x: Help & Discussion
    Replies: 0
    Last Post: 23 Aug 2009, 6:29 AM
  5. How to access Ext.Window context from an ajax loaded HTML file?
    By SkoalFyfan in forum Ext 2.x: Help & Discussion
    Replies: 5
    Last Post: 31 Oct 2007, 8:44 AM

Thread Participants: 2