Friday 19 December 2008

Hints for a complete STRUTS Application (3)

Hints for a complete STRUTS Application (3)
Source code is available from arsaral(at)yahoo.com

3rd Hint: selectRow and Master-Detail connection in a simple Struts example

This program uses the Struts - iterator structure in the 2nd example and builds on it the selectRow facility. It adds a hyperlink named view on each line. When you click on this hyperlink it brings the related record on a detail screen. To make it extremely simple, only a string is used in this example. Nex example will use a complete record.

selectItemList.jsp
<BODY>
<logic:forward name="selectItemList"></logic:forward>
</BODY>
</HTML>

Struts-config.xml
<global-forwards>
<forward name="selectItemList" path="/selectItemList.do"/>
</global-forwards>
<action-mappings>
<action name="selectItemList" path="/selectItemList"
type="com.masslight.actions.SetListAction">
<forward name="success" path="/selectItemProcess.jsp"></forward>
</action>
<action name="setDetail" path="/setDetail"
type="com.masslight.actions.SetDetailAction">
<forward name="success" path="/detailProcess.jsp"></forward>
</action>
</action-mappings>

As seen, first you will run selectItemList.jsp than selectItemList.jsp will run selectItemList.do and that will run SetListAction.java.

package com.actions;

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

public final class SetListAction extends Action {

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

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

HttpSession session = request.getSession();

java.util.ArrayList list = new java.util.ArrayList();
list.add("item 1");
list.add("item 2");
list.add("item 3");

session.setAttribute("baseList",list);

ActionForward forward = mapping.findForward("success");
return forward;
}
}
As seen above, SetListAction.java prepares a list named baseList and then forwards the action with “success” to struts-config.xml. struts-config.xml than runs selectItemProcess.jsp which is given below.

selectItemProcess.jsp
<%@ page language="java"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html>
<header>
<title>Iterator</title>
<html:base/>
</header>
<body>
<logic:present name="baseList">
<table border="2px" bgcolor="#CCCC99" cellspacing="1">
<logic:iterate id="iteratorItem" name="baseList">
<tr>
<td>Item Value:</td>
<td>
<bean:write name="iteratorItem"/>
</td>
<td>
<html:link page="/setDetail.do" paramName="iteratorItem"
paramId="iteratorItem">View</html:link>
</td>
</tr>
</logic:iterate>
</table>
</logic:present>

<html:submit value="Continue"/>
</body>
</html:html>

selectItemProcess.jsp uses an html:link with pameters to run /setDetail.do of struts-config.xml.
This runs SetDetailAction.

com.masslight.actions.SetDetailAction
package com.masslight.actions;

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

public final class SetDetailAction extends Action {

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

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

HttpSession session = request.getSession();

String detailInfo= request.getParameter("iteratorItem");

session.setAttribute("baseDetail",detailInfo);

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

The ActionForward success runs the detailProcess.jsp which displays the detail, which happens to be the same word alone, for the sake of simplicity.

detailProcess.jsp
<%@ page language="java"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html>
<header>
<title>Iterator</title>
<html:base/>
</header>
<body>
<bean:write name="baseDetail"/>
<br/>
<html:submit value="Continue"/>
</body>
</html:html>