Saturday, January 26, 2013

ForwardAction and IncludeAction in struts


   The org.apache.struts.actions.ForwardAction class enables you to forward request to the specified URL without going through Controller. ForwardAction is an utility class  used in cases where a user simply needs to forward the control to another JSP . Doing this directly violates the MVC principles.  So they have given this option for  action-mapping. Note that we do not  create any action class here. With ForwardAction , simply create an action mapping  in the Struts configuration file  and specify the location where the action should forward the request with the help of "parameter" attribute of action tag.
For example:

In JSP :

<html:link page="/Welcome.do">
           Click here
</html:link> 


In strus-config :

<action-mappings>
<action
  path="/Welcome"
  type="org.apache.struts.actions.ForwardAction"
  parameter="/Welcome.jsp"/>
</action-mappings> 


there is shortcut for above configuration. you can achieve same simply by:


<action path="/Welcome" include="/Welcome.jsp"/>


org.apache.struts.actions.IncludeAction is somewhat like ForwardAction but the resulting resource is included in the HTTP response instead of forwarding the request to. Actually this is of not much use. Its mainly used to integrate old/legacy applications with Struts transparently. More like integrating simple servlet into struts application. 

In Struts-config file:


<action path="/hello" parameter="/helloServlet"   type="org.apache.struts.actions.IncludeAction"> 
</action>

Jsp:


<jsp:include page="/hello.do"></jsp:include>


The parameter attribute indicates the actual resource that has to be included in the response. As mentioned earlier, the use of IncludeAction is limited to including responses from existing Servlet in the current page. This requires the use of <jsp:include> in the jsp page.


Thursday, January 17, 2013

DynaActionForm in struts with example.


      If you are writing Struts application, you are expected to write a FormBean for every page. But it's too tedious to write lot's of FormBeans with just getters and setters for the fields.
      You can avoid this by using DynaActionForm (Dynamic Form Beans) which are an extension of FormBeans that allow you to specify their fields inside the Struts configuration file itself instead creating concrete classes with getters and setters.
    For using this, you need to configure the form bean of DynaActionForm inside strust-config file. Do it this way :


<form-bean name="dynamicLoginForm" 
           type="org.apache.struts.action.DynaActionForm">
           <form-property name="userName" 
                          type="java.lang.String" 
                          initial="nils"></form-property>
           <form-property name="password" 
                          type="java.lang.String"></form-property>
</form-bean>

"name" attributes of "form-property" specify the field names and "type" specify the type of the field. you also can specify the initial value by "initial" attribute. And use dynamicLoginForm(name) of this form bean as value for name attribute of corresponding action just like normal FormBeans .


<action path="/login" 
            name="dynamicLoginForm">
</action>



    Now the values of fields of corresponding form on the webpage will be populated to the form which will be dynamically created. 
   So you need a way to access this values in Action class's execute() method. For that you need to use get() or getXXX() methods of DynaActionForm.
    Following snippet shows the way to access fields of DynaActionForm in execute() method of Action :

public ActionForward execute(ActionMapping mapping, 
                             ActionForm form,
                             HttpServletRequest request, 
                             HttpServletResponse response)
                      throws Exception {


     DynaActionForm theForm = (DynaActionForm)form;
     String userName = theForm.getString("userName");
     String password = (String)theForm.get("password");


}

Well, DynaActionForm is this much simple :)

What is resource bundle in struts and using multiple resource bundle files.

      Resource bundles allow Java applications to be easily internationalized by having application content placed into bundles. This content can then be read by the application at run time. Therefore, instead of having content hardcoded in the application, the application reads its content from the bundle. Struts has built-in support for working with Java's resource bundle mechanism. 

      Resource bundle resources can be accessed from JSPs to populate them with content. Resource bundle properties files simply contain key/value pairs. The resources are accessed by their key. The standard name for the resources bundle properties file in struts is MessageResources.properties. 
   Thes files may contain key/values like this login.empty=Empty username and passwordtitle=Struts Relearn
   
      These files are configured in struts-config.xml :

1
2
<message-resources parameter="com.nils.resources.MessageResources">
</message-resources>

keep in mind that the parameter attribute is a file name without .properties extension.You can access these values in JPSs like this 


1
<bean:message key="title" />

given that bean taglib is configured in the JSP.



     If you want to use multiple properties files with different values for the keys. Then  you just need to specify "key" attribute of message-resources in struts-config.xml entries.

1
2
3
4
5
6
<message-resources parameter="com.nilesh.resources.MessageResources" 
                   key="NormalCase">
</message-resources>
<message-resources parameter="com.nilesh.resources.AnotherMessageResources" 
                   key="UpperCase">
</message-resources>

        And while using them in JSP use "bundle" attribute of bean:message.


1
2
<bean:message key="title" bundle="NormalCase"/>
<bean:message key="title" bundle="UpperCase"/>

bundle attribute is mapped to key attribute of message-resources which specifies the properties file. 

        If you configure multiple message-resources and don't specify the keys for them both of them will have default key 

"org.apache.struts.action.MESSAGE"

so when they are used in JSP it will see two files configured with same key and will throw excpetion, this is the same exception you may see when message-resources is misconfigured:


SRVE0068E: Uncaught exception created in one of the service 
methods of the servlet /index.jsp in application StrutsEar. 
Exception created :
com.ibm.websphere.servlet.error.ServletErrorReport:
javax.servlet.jsp.JspException: Cannot find message resources under key 
org.apache.struts.action.MESSAGE.

Friday, January 11, 2013

How to use offline Google maps on android phone in India.

Recently Google launched offline google maps for android phones in India. This new feature allows you to store map of some area on your phone for later use. Say you are going to some new or remote area and not sure if GPS is available there or not, or your network provider provides network there or not, or say you don't use internet on your phone always and you need map under such circumstances then it's better to download map of required area in advance.

No doubt this feature is of great use. This service was available in US and many European countries and now it's made available in India too.

Below are simple steps to get offline google map on your android phone.

1. Have Proper version of Google maps app :

First of all you need to upgrade your google map app if it's older one. Now latest version available is 6.14.1 minimum, minimum  because this is what I upgraded to in 2012 october. You can check if you need to upgrade or not by Google map » Options » more » make offline available, if this option is available then you don't need to upgrade otherwise you need to. And important point to note - this feature is available on android version 2.2+, thank god I am just on edge. 
To upgrade this app goto google play store on your phone search for google map and upgrade if you need.

 
2.Make map available offline :

Once proper version of google map is available, open google maps application search for area whose map you want to be available offline on your android.
by using  options » search.
Once proper area's map is loaded select
options » more options » make available offline.




 
3.  Select Proper Area :

Now it will show rectangular area over the map to chose the area of the map to  download. screen should be showing size of the map of area you have selected in mb along with "select" button which would be disabled. Now pinch in and out over rectangular selection area to select area to download. Keep an eye on select button and size of the map. You can download a map only when select button is enabled. Generally minimum area of the map that you can download is  near about 67mb. keep trying to select size of rectangle so that select button is enabled. try keeping size of the map across 60 to 100 mb.


 4. Download map :
 
Once select button is available, click it and  your map will start downloading. 
wait till map is downloaded. Once it`s downloaded you are ready to use the map offline.







5.  Use offline Google map :

To use offline map, goto options » my places » OFFLINE tab there your offline map will be available. Select it and there you go with your offline map.

don't you worry about phone memory consumption this map doesn't get downloaded completely to your phone memory. I am not sure where exactly this offline map gets downloaded  on phone  but it doesn't  consume your phone memory. I am sure about this because my phone never has that much free phone memory. So cheers. 




6. Enjoy offline maps on your android.

Difference between DispatchAction and MappingDispatchAction in struts.

DispatchAction action class provides a mechanism for modularizing a set of related functions into single action. This eliminates the need to create separate, independent actions for each function.
MappingDispatchAction is subclass of DispatchAction used towards the same purpose. They are almost same except the way they are designed and used. Both of them rely on use on "parameter" attribute of action in action-mappings.

The main difference is about the way "parameter" attribute is used. In case of DispatchAction, value of  "parameter" attribute is used as attribute in request scope and value of this request attribute is mapped to the function name in the action class.

Eg. if it uses parameter="operation" then, operation attribute will be searched in request scope and it's value (say add) is mapped to the function name.
On the other hand, with MappingDispatchAction value of "parameter" attribute directly mapped to the function name.

Eg. if it uses parameter="add" then add is directly mapped to function name.



Because of above difference  the URL structure is also different. For DispatchAction url contains parameter information for configured parameter(operation). so url could be like

/user.do?operation=add

And for MappingDispatchAction url is simple one which need not to have any parameters for the purpose of function name mapping. So it could be like  

/user.do

Another difference from design point of view is about configuring actions in action-mappings. In case of  DispatchAction single action tag is used.


1
2
3
4
5
6
7
8
<action path="/user" 
         name="userForm"
         type="com.nilesh.action.UserAction" 
         parameter="operation"
         scope="request"
         input="/Welcome.jsp">
    <forward name="Success" path="/user.jsp"></forward>
</action>


and it's generally mapped from a single form having different operations with different values for configured "operation" parameter.
and For MappingDispatchAction we need to use multiple actions in action-mappings so the multiple forms with different values for action attribute of the html:forms.


1
2
3
4
5
6
7
8
9
<action path="/addUser" parameter="add"  type="com.nilesh.action.MappingDispatchUserAction">
<forward name="Success" path="/mappingDispatchUser.jsp"></forward>
</action>
<action path="/deleteUser" parameter="delete" type="com.nilesh.action.MappingDispatchUserAction">
<forward name="Success" path="/mappingDispatchUser.jsp"></forward>
</action>
<action path="/updateUser" parameter="update" type="com.nilesh.action.MappingDispatchUserAction">
<forward name="Success" path="/mappingDispatchUser.jsp"></forward>
</action>

So the difference is just about what you need and how to use.

Thursday, January 3, 2013

Great online tools worth using.

   These days "browser" is the most used application by most of the users on all kind of computers. People wants to do everything online. They want storage online, writing online, reading online, coding online, searching online and so on. We can say people are getting online more because they are getting more online. There are many great tools available online for doing things we used to do offline.
   Following are some of the great online tools or say apps, those are worth using. You might be using few of them already. 


1.  Evernote

Evernote
Evernote is a suite of software and services designed for notetaking and archiving. A "note" can be a piece of formatted text, a full webpage or webpage excerpt, a photograph, a voice memo, or a handwritten "ink" note. Notes can also have file attachments. Notes can be sorted into folders, then tagged, annotated, edited, given comments, searched and exported as part of a notebook. Evernote supports a number of operating system platforms (includingMicrosoft Windows, Mac OS X, Chrome OS, Android, iOS, Windows Phone, and WebOS), and also offers online synchronization and backup services.





The best way to save and view articles, videos and more. When you find something on the web that you want to view later, put it in Pocket. It automatically syncs to your phone, tablet or computer so you can view it any time, even without an internet connection.

JsFiddle is a playground for web developers, a tool which may be used in many ways. One can use it as an online editor for snippets build from HTML, CSS and JavaScript. The code can then be shared with others, embedded on a blog, etc. Using this approach, JavaScript developers can very easily isolate bugs.





4.  PasteBin
Pastebin.com is a website where you can store text for a certain period of time. The website is mainly used by programmers to store pieces of sources code or configuration information, but anyone is more than welcome to paste any type of text. The idea behind the site is to make it more convenient for people to share large amounts of text online.

Pixlr is a cloud-based set of image tools and utilities, including a number of photo editor, as screen grabber browser extension, and a photo sharing service. Founded in Sweden in 2008 by Ola Sevandersson , it was intended for non-professionals. It can be used on PCs, and on smartphones or tablets using an app.
The suite of apps range from simple and playful to advanced photo editing.

Dropbox is a file hosting service operated by Dropbox, Inc. that offers cloud storage, file synchronization, and clientsoftware. Dropbox allows users to create a special folder on each of their computers, which Dropbox then synchronises so that it appears to be the same folder (with the same contents) regardless of the computer it is viewed on. Files placed in this folder are also accessible through a website and mobile phone applications.

Google Drive is a file storage and synchronization service by Google. Google Drive is now the home of Google Docs, a suite of productivity applications, that offer collaborative editing on documents, spreadsheets, presentations, and more. 




Please feel free to suggest any other good online tool.