Friday 19 December 2008

Hints for a complete STRUTS Application (1)

Source code is available from arsaral(at)yahoo.com

1.st Hint: Running a program at the very beginning of a STRUTS application.

It is necessary to prepare the working environment before the standard STRUTS application begins to function through its menus and screens. Opening databases, some logging etc. may be done using this approach. Please note, I do not refer to authentication at this point. Some of these tasks may be done before the authentication or after it, depending on the implementation.

Struts-config.xml

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

<action-mappings>
<action path="/init" name="init" type="com.actions.InitAction">
<forward path="/mainmenu.jsp" name="start">
</action>
</action-mappings>


Here, mainmenu.jsp is a jsp file residing in the WEB-INF directory. This can be any jsp which may be used
for the main menu navigation of the STRUTS application.

In the src folder of the application, there is a com and in that there is an actions folder where
InitAction.java resides:

InitAction source follows:

package com.actions;

import javax.servlet.http.*;
import org.apache.struts.action.*;

public class InitAction extends Action {

// The constructor method for this class
public InitAction() {
}

// This sets the list as a session bean
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

// Any code to prepare the environment for the new born application goes here…

ActionForward forward = mapping.findForward("start");
return forward;
}
}

As seen above, the “start” ActionForward keyword trigs the execution of the mainmenu.jsp in the

struts-config.xml.