Struts - Reading Data From "Properties" File

Creating Properties file in Net Beans:

Expand project ----> 
right click on source package---->
new---->
other---->
other---->
properties file ---->
 next ----> filename ----> Finish. 
directory Structure
  1. Write Properties file
myfile.properties
______________________
# Resource Bundle file.
#
my.title =<center> <b>welcome</b></center> 
my.uname=username 
my.pwd=password 
my.btn.cap=checkdetails.

2.Configure properties file in struts configuration file
  <message-resources parameter="com/myapp/struts/myfile"/>
After</action-mapping>

3.Read presentation logic labels from properties file being from JSP program

Example:

Struts.config.xml
********************************************
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">


<struts-config>
    <form-beans>
         
        <form-bean name="LoginForm" type="com.myapp.struts.LoginForm"/>
    
    </form-beans>
    
    <global-exceptions>
    
    </global-exceptions>

    <global-forwards>
        <forward name="welcome"  path="/Welcome.do"/>
    </global-forwards>

    <action-mappings>
        <action input="/" name="LoginForm" path="/login" scope="session" type="com.myapp.struts.LoginAction"/>
        <action path="/Welcome" forward="/welcomeStruts.jsp"/>
    </action-mappings>


    <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

    <message-resources parameter="com/myapp/struts/ApplicationResource"/>    
        <message-resources parameter="com/myapp/struts/myfile"/>
    <!-- ========================= Tiles plugin ===============================-->
    <!--
    This plugin initialize Tiles definition factory. This later can takes some
    parameters explained here after. The plugin first read parameters from
    web.xml, thenoverload them with parameters defined here. All parameters
    are optional.
    The plugin should be declared in each struts-config file.
    - definitions-config: (optional)
    Specify configuration file names. There can be several comma
    separated file names (default: ?? )
    - moduleAware: (optional - struts1.1)
    Specify if the Tiles definition factory is module aware. If true
    (default), there will be one factory for each Struts module.
    If false, there will be one common factory for all module. In this
    later case, it is still needed to declare one plugin per module.
    The factory will be initialized with parameters found in the first
    initialized plugin (generally the one associated with the default
    module).
    true : One factory per module. (default)
    false : one single shared factory for all modules
    - definitions-parser-validate: (optional)
    Specify if xml parser should validate the Tiles configuration file.
    true : validate. DTD should be specified in file header (default)
    false : no validation

    Paths found in Tiles definitions are relative to the main context.
    -->
    <plug-in className="org.apache.struts.tiles.TilesPlugin" >
        <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />      
        <set-property property="moduleAware" value="true" />
    </plug-in>
    
    <!-- ========================= Validator plugin ================================= -->
    <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property
            property="pathnames"
            value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
    </plug-in>
  
</struts-config>


myfile.prop
*******************************************

# Resource Bundle file.
#
my.title =<center> <b>welcome</b></center> 
my.uname=username 
my.pwd=password 
my.btn.cap=checkdetails.


login.jsp
**********************************
<%@taglib  uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%-- 
    Document   : login
    Created on : Oct 3, 2013, 1:52:42 PM
    Author     : Satya<satyanarayana@greenbuds.co.in & satyajohnny@live.com>
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <bean:message key="my.title"/><br/>
        <html:form action="/login">
        <bean:message key="my.uname"/><html:text property="uname" />
        <bean:message key="my.pwd"/>      <html:text property="pwd" />
        </html:form>
    </body>
</html>

Reading Properties data From execute(-,-,-,-) method



 MessageResources msg = getResources(request);
       String c =  msg.getMessage("my.class");
       String d =  msg.getMessage("my.driver");
          Class.forName(c);
          System.out.println("Class :   "+c+"Driver :"+d);
 con = DriverManager.getConnection(d,"","");


Post a Comment

Thank You

Previous Post Next Post