PDA

View Full Version : ExtJS access to a properties file content loaded via index.jsp page



cavalleydude
8 Oct 2010, 7:20 AM
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):


...

<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?

Condor
8 Oct 2010, 7:44 AM
Create a .jsp that outputs all properties as a JSON object that is assigned to a variable, e.g.

Ext.ns('MyNamespace');
MyNamespace.properties = {
// dynamically filled from /privatefolder/my.properties
profileservice: 'abc',
otherproperty: 123
};
Then, simply include this .jsp with a <script> tag.

cavalleydude
8 Oct 2010, 8:07 AM
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


<%@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>

cavalleydude
8 Oct 2010, 8:18 AM
Hi Condor, I think I know how to do this... I was not thinking. I'll try it and post results shortly.

cavalleydude
8 Oct 2010, 8:45 AM
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.

cavalleydude
8 Oct 2010, 10:51 AM
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:



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



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



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.


<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.

sanjayarrk1
10 Sep 2011, 11:44 PM
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.