Time

Sunday, 16 August 2009

Implementing Internationalization in Struts

Advanced J2EE Concepts - Internationalization in Struts

How is ‘Internationalization’ implemented in struts?

Default Internationalization (Detected based on Browser's settings):

Internationalization is an important concept of making the application to be compatible with different geographical location and user-friendly with respect to their location by means of providing the information in their preferred language.

In struts, it can be done quite easily. The application resource file name can be extended with underscore followed by locale.

For example ,

When your application contains the file “ApplicationResource_en.properties”, it would mean that it supports english language. When another file “ApplicationResource_fr.properties” exists, it would mean the localization for ‘French’.

The selection of file and the content is done and decided automatically by struts. When the browser’s setting is found with French settings, then struts automatically retrieves the value for the specific key from the appropriate resource file. Similarly you can add multiple property files for various locale.

But please remember that you can have one property file name with different extensions like _en, _fr, _de, etc., If they differ, the internationalization will not work. Also that only one file (one time), is specified in struts-config.xml. Each property file must have the same key with different values as follows:

In ApplicationResources.properties_en, you specifymainpage.welcome=Hi, You are welcome..

In ApplicationResources.properties_fr, you specifymainpage.welcome=<>

Programmatic implementation of Internationalization (By manual selection)

The localization can even be done programmatically by placing ‘Locale’ object in session.
session.setAttribute("org.apache.struts.action.LOCALE", new Locale("fr"));

For example, You can create links one for each country's localization in the main page. Each link leads to an action class with different query string such as

English

German

French

When clicked, the action is called. Inside the action class you retrieve the lang parameter as follows

String lang = request.getParamter("lang");

if (lang.equals("fr"))

session.setAttribute("org.apache.struts.action.LOCALE", new Locale("fr"));

if (lang.equals("de"))
session.setAttribute("org.apache.struts.action.LOCALE", new Locale("de"));

Multiple value selection using checkbox in struts

How to design a page in struts using CheckBox for choosing 2 or more values?

Perform the following steps:

1. Action Form: Design a form with Array of Objects of String type.
String[] selectedValues = { };

2. JSP: Populate the values that it can hold using logic:iterate tag.
" ..... >

3. After Form submission: the selected values will be placed in selectedValues property

4. Now, in Action, iterate the collection, to identify the selected DTO object.

This needs to be designed as per the requirements.
For example, based on 'selectedValues', pick up the selected objects from the one collection into another,
so that the business routine can use the resultant collection.







Struts / Java / J2EE Frequently-Asked Interview Questions

Are Struts's action classes Thread-safe?
Yes. Action classes are based on "one-instance and many-threads". This is to conserve resources and to provide the best possible throughput.

What are the various Struts Tag libraries?
There are various struts tags. But the most-repeated tags are:
struts-bean.tld
struts-html.tld
struts-logic.tld
struts-nested.tld

What is ActionMapping in struts?
Action mapping defines the flow of one request. The possible sequence is
User -> request -> Form -> Validation -> Business Code -> Forward -> JSP -> response -> User.
The components involved are Action classes, Forms and JSP.

What are the advantages of having multiple struts-config in the same application?
The implementation with many struts-config is to organize the development work, so that many people may be involved and it is some organized way of doing things. But this would result in some compromise in performance(speed). Technically there is no any difference between single and multiple struts-config files.

What are the ways in which resource file can be used in struts?
Defining message resources in struts-config file.
Programmatically using resource files in Java classes or in JSP files.

Explain the term 'architecture of the application'?
Architecture is the set of rules (or framework) to bring in some common way of assembling or using J2EE components in the application. This helps in bringing consistency between codes developed by various developers in the team.

Which is the architecture followed by struts?
Struts follows MVC architecture.

What are components corresponding o M, V and C in struts?

Model : The model represents the data of an application. Anything that an application will persist becomes a part of model. The model also defines the way of accessing this data ( the business logic of application) for manipulation. It knows nothing about the way the data will be displayed by the application. It just provides service to access the data and modify it. Here 'Form Bean' represents Model layer.

View : The view represents the presentation of the application. The view queries the model for its content and renders it. The way the model will be rendered is defined by the view. The view is not dependent on data or application logic changes and remains same even if the business logic undergoes modification. JSP represents View Layer.

Controller : All the user requests to the application go through the controller. The controller intercepts the requests from view and passes it to the model for appropriate action. Based on the result of the action on data, the controller directs the user to the subsequent view. Action classes (action servlets) represent Controller layer.

Differentiate between the terms 'Design Patterns', 'Framework' and 'Architecture'.
Design Pattern: The various solutions arrived at for the known problem. This helps to avoid re-inventing the wheel. The risk-free solution can be easily used by others. For example, singleton is the design pattern that you can use instantly to enfore one object per class. You do not need to think of this on your own.
Framework: A framework is a structure or set of rules, used to solve or address complex issues. It is basically a reusable designf for the J2EE applications. For example, Struts, JSF,etc., are the frameworks. You can use these frameworks based on the requirements of your application and each has its own set of advantages.

Architecture: It is a design that describes how the various components in the application fit together. For example, MVC is an architecture which is helpful to define how the components interact within the application.


What is the difference between ActionForm and DynaActionForm?
In action form, you need to define the form class that extends ActionForm explicitly, whereas you can define the form dynamically inside the struts-config file when using DynaActionForm.

How can you use Validator framework in struts?
Validator Frameworks are helpful when the application needs server-side validation such that the particular set of validations occur very frequently within the same application. This avoids writing complex code in validation() method in every form bean. Using validator framework, there are different pre-written validations in place. You can customize these validations in XML file.

What are client-side and server-side vaidations?
Client-side validations: These are the validations that id done using javascripts. There is always a danger involved that the user can get through (crack-through) these validations. But for some simple validations, like converting lower-case to upper-case or date validations can be done, you can use javascripts.
Server-side validations: These are the validations done in server-side using Java components (Form bean or in business logics) where the user has no chance to crookedly get through the system.

What are the advantages & differences between using validate() method in form over validations using validator framework.
Refer to the previous-answer.

Define the terms authentication and authorisation.
Authentication is the process/rule that validates if the user is allowed to access the system or not.
Authorization is the process/rule that validates if the user is allowed to access the particular part of the system or not. This occurs after user's successful authentication.

What are the components provided in J2EE to perform authentication and authorization?
Authentication – RequestProcessor and/or Filter.
Authorization - DTO, JDO or Java or Action classes.

Give the difference between between 'DispatchAction' and 'Action'.
A DispatchAction contains various different methods other than the standard execute() method in Action classes. These methods are executed based on some request parameter. For example, you can code in such a way that three buttons (namely Insert, Delete, Update) buttons correspond to different methods such as insert(), delete() and udpate(). The submit button in JSP would have the property that has the value which matches to any one of the methods defined in DispatchAction class.

What is pagination technique? How can you design them in struts?
Pagination is the technique where the bulk of results are split into different pages and only the information where the user can conveniently see are displayed in a page. (Like in Goooooogle). This can be achieved in many ways, but the simplest method is to have a query string (say http://www.testwebsite?pageNumber=2) would lead to information corresponding to resultset rows from 11 to 20. Assuming that you want to display 10 related rows of information, you can set the formula as follows:
Starting row = (pageNumber-1) * + 1 which is equal to 11.
Ending row = Starting row +
which is equal to 20.

How can you populate the drop-down list using form properties?
There are many ways for this. But the best method is to use
which defines collection that needs to be used to populate the drop-down list, the property to store the selected value and the collection that is used to display the labels (what we see in JSP page). For Example,

html:options collection="form-collection-property"
property="form-property"
labelProperty="form-another-collection-property"

What is the XML parser provided in struts? Can you use it for other purposes?
'Digester' framework. Yes we can use for our applications to store and parse our application-related data.

Difference between Struts 1.0 and 1.1
  • Perform() method was replaced by execute()
  • DynaActionForms are introduced.
  • Tiles Concept is introduced
  • We can write our own Controller by Inheriting RequestProcessor