Hints for a complete STRUTS Application (4)
Source code is available from arsaral(at)yahoo.com
4th Hint: selectRow and Master-Detail connection with a record structure
This program uses the Struts - iterator – selectRow facility structure in the 3rd example. The 3rd example passed only a string, this example uses a class record structure to view ina seperate screen. The next example will add new, update and delete facilities. Logging, use of message bundle and messages, imported menus etc. are excluded to make things very simple. Future examples will also handle these.
index.jsp
<BODY>
<logic:forward name="setList"></logic:forward>
</BODY>
Struts-config.xml
<struts-config>
<!-- ========== Global Forward Definitions ============================== -->
<global-forwards>
<forward name="setList" path="/setList.do"/>
</global-forwards>
<!-- ========== Action Mapping Definitions ============================== -->
<action-mappings>
<action name="setList" path="/setList"
type="com.ars.actions.SetListAction">
<forward name="success" path="/listProcess.jsp"></forward>
</action>
<action name="viewDetail" path="/viewDetail"
type="com.ars.actions.ViewDetailAction">
<forward name="success" path="/viewDetailProcess.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="arsstruts.ApplicationResources"/>
</struts-config>
As seen, first you will run index.jsp than index.jsp will run setList.do and that will run SetListAction.java.
SetListAction.java
package com.ars.actions;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import com.ars.beans.Person;
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 {
Person person = null;
HttpSession session = request.getSession();
java.util.ArrayList list = new java.util.ArrayList();
for (int i = 0; i < 13; i++) {
person = new Person();
person.setFirstName("firstName" + i);
person.setLastName("lastName" + i);
person.setCity("city" + i);
person.setCountry("country" + i);
list.add(person);
}
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 listProcess.jsp which is given below.
listProcess.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="0" cellspacing="0" cellpadding="0" align="center" width="70%" style="border-collapse:collapse;">
<tr bgcolor="#98AFCC">
<th>First Name</th>
<th>Last Name</th>
<th>City</th>
<th>Country</th>
</tr>
<logic:iterate id="person" name="baseList">
<tr>
<td>
<bean:write name="person" property="firstName" />
</td>
<td>
<bean:write name="person" property="lastName" />
</td>
<td>
<bean:write name="person" property="city" />
</td>
<td>
<bean:write name="person" property="country" />
</td>
<td>
<html:link page="/viewDetail.do" paramName="person" paramProperty="firstName"
paramId="firstID">View</html:link>
</td>
</tr>
</logic:iterate>
</table>
</logic:present>
</body>
</html:html>
listProcess.jsp uses an html:link with pameters to run /viewDetail.do of struts-config.xml.
It passes the firstName of the selected row to ViewDetailAction.
package com.ars.actions;
import com.ars.beans.Person;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public final class ViewDetailAction extends Action {
// The constructor method for this class
public ViewDetailAction() {
}
// 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 firstNameID= request.getParameter("firstID");
java.util.ArrayList list = (java.util.ArrayList) session.getAttribute("baseList");
Person personDetail = new Person();
Person personCheck = new Person();
for (Object personCheckObject : list){
personCheck = (Person) personCheckObject;
if (personCheck.getFirstName().compareTo(firstNameID) > -1 ){
personDetail.setFirstName(personCheck.getFirstName());
personDetail.setLastName(personCheck.getLastName());
personDetail.setCity(personCheck.getCity());
personDetail.setCountry(personCheck.getCountry());
break;
}
}
session.setAttribute("baseDetail",personDetail);
ActionForward forward = mapping.findForward("success");
return forward;
}
}
viewDetailAction uses the list session object “baseList” that had been created by SetListAction to get the detail
information related to the firstID that has been passed as request param. It prepares the “baseDetail” and passes it as a session attribute to viewDetailProcess.jsp.
viewDetailProcess.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"%>
<logic:present name="baseDetail">
<html:html>
<header>
<title>view detail page</title>
<html:base/>
</header>
<body>
<table>
<tr>
<td colspan="2">Page for Viewing the Personal Info</td>
</tr>
<tr>
<td>First Name :</td>
<td>
<bean:write name="baseDetail" property="firstName"/>
</td>
</tr>
<tr>
<td>Last Name :</td>
<td>
<bean:write name="baseDetail" property="lastName"/>
</td>
</tr>
<tr>
<td>City :</td>
<td>
<bean:write name="baseDetail" property="city"/>
</td>
</tr>
<tr>
<td>Country :</td>
<td>
<bean:write name="baseDetail" property="country"/>
</td>
</tr>
</table>
</body>
</html:html>
</logic:present>
The next hint will implement new, update and delete functions. Source and executable is available on request from arsaral( at )yahoo.com