Showing posts with label Struts. Show all posts
Showing posts with label Struts. Show all posts

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

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.