Sitemap
Bhagvan Kommadi

Bhagvan Kommadi

Founder, Quantica Computacao, A member of the MIT Technology Review Global Panel, a community of business professionals.

Working with Tabular Data using Struts Framework

2 min readJan 25, 2020

There are many scenarios where we work with tabular data. Sometimes we not only display tabular data but also make it editable hence have to be saved and updated.
This article talks about how to do the above in struts framework.

Press enter or click to view image in full size
Class Diagram

In the above diagram, A tableForm has indexed property rows and Row consists of indexed property column. There are static variables number of rows in TableForm and number of columns in Row. Before we display the table in a jsp we set the size of the rows and columns updating the static variables.

TableForm.setRowsize(2);
Row.setColsize(2);

In struts-config.xml, we configure the TableAction & TableForm:

<?xml version=”1.0" encoding=”ISO-8859–1" ?>

<!DOCTYPE struts-config PUBLIC
“-//Apache Software Foundation//DTD Struts Configuration 1.0//EN”
“http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
<form-beans>

<form-bean name=”tableForm”
type=”TableForm”/>
</form-beans>

<! — ========== Action Mapping Definitions ============================== →
<action-mappings>
<action path=”/table”
type=”TableAction”
name=”tableForm”
scope=”request”
validate=”false”>
<forward name=”success” path=”/Bean.jsp”/>
<forward name=”failure” path=”/Table.jsp”/>
</action>

Get Bhagvan Kommadi’s stories in your inbox

Join Medium for free to get updates from this writer.

</action-mappings>

</struts-config>

The Table.jsp will access the indexed properties to display and update:

<%@ taglib uri=”/WEB-INF/struts-html.tld” prefix=”html” %>
<html:html locale=”true”>
<html:form action=”/table” >
<center>
<table>
<tr>
<td> <html:text property=”rows[0].columns[0].value” /> </td>
<td> <html:text property=”rows[0].columns[1].value” /></td>

</tr>
<tr>
<td> <html:text property=”rows[1].columns[0].value” /> </td>
<td> <html:text property=”rows[1].columns[1].value” /></td>
</tr>
</table>
</center>
<center> <html:submit property=”submit” value=”Submit”/> </center>
</html:form>
</html:html>

When the data is submitted and updated, the Table Action will be able to access the tabular values as shown below:

public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

TableForm aForm = (TableForm) form;

System.out.println(aForm.getRows(0).getColumns(0).getValue());
System.out.println(“00 processed”);
System.out.println(aForm.getRows(0).getColumns(1).getValue());
System.out.println(aForm.getRows(1).getColumns(0).getValue());
System.out.println(aForm.getRows(1).getColumns(1).getValue());
}

The code can be downloaded from the github repository : https://github.com/bhagvank/strutstable

--

--

Bhagvan Kommadi
Bhagvan Kommadi

Written by Bhagvan Kommadi

Founder, Quantica Computacao, A member of the MIT Technology Review Global Panel, a community of business professionals.