2.Using a Resource bundle in a Struts Application

Using a Resource bundle in a Struts Application:

·      1.  To make an application as compatible with I18N, the Labels are form elements or need to collectfrom Resource Bundle, instead of directly providing the Labels in a form design.
·        2.   A resource bundle is a properties file it contains data in the form of the key-=value pairs and
values are collected based on the keys
·         3.A Resource bundle file in a Struts application is not only form collecting Labels but also for
defining error messages.
·         4.In a Struts application, error messages must be defined in a Resource bundle only,
·         5.In order to collect Labels from a Resource Bundle in a JSP page, we need to use <bean:message>.
·         To use Framework provide bean tags in a JSP, we need to add the following taglib to the JSP
page.
       
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

# -- sathya properties
label.uname=<font color="blue">UserName</font>

label.pwd=<font color="blue">Password</font

satya.propertis
_____________________
# Resource Bundle file.
#
label.user=<b>Username    :  </b> 
label.pwd=<b>Password    :  </b> 

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"/><forward name="ok"  path="/ok.jsp"/>
<forward name="fail"  path="/fail.jsp"/>
    </global-forwards>

    <action-mappings>
        <action input="/" name="LoginForm" path="/login" scope="request" 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/satya" />
    
    <!-- ========================= 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>



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 8, 2013, 11:30:41 AM
    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>
        
        <html:form action="/login">
            
            <bean:message key="label.user"/> <html:text property="uname"/>
           <bean:message key="label.pwd"/><html:password property="pwd"/>
            <html:submit value="ok"/>
            
            
            
        </html:form>
    </body>
</html>

Post a Comment

Thank You

Previous Post Next Post