[JAVA] Input in tabular format with Struts2.5.x (JSP and Thymeleaf compatible)

Struts2 and tabular input

One of the screen requests that is quite annoying when creating a system with a Web application is what is called ** tabular input **.

This looks pretty easy ** it was a hassle to implement in the first place **, but in fact it has been possible since Struts 2 was released.

However, from that point on, there were some implementations that stopped working depending on the version due to various factors such as security fixes and potential bugs, so I will introduce them here.

Tabular input (request) method

Struts2 stores request parameters in member variables of the Action class. When storing in a member variable, it automatically tries to convert to the type (class) of the member variable of Action class. The types (classes) that are automatically converted are as follows.

… In other words, most things are converted automatically. Also, since the field of the class is automatically searched and the value is stored, for example, prepare the following field in the Action class.

private List<SampleProduct> products;

public void setProducts(List<SampleProduct> products) {
  this.products = products ;
}

public List<String> getProducts () {
  return products;
}

By doing this, you can handle both the response to the screen and the request from the screen as an array, and you can also handle the class whose contents are prepared independently.

Example of input form using Thymeleaf

<table class="table table-bordered">
	<thead>
		<th>id</th>
		<th>product name</th>
	</thead>
	<tbody>
		<tr th:each="product,status : ${products}">
			<td>
				<input type="text" value="X-45" name="id" class="form-control" th:name="'products[' + ${status.getIndex()} + '].id'" th:value="${product.getId()}" />
			</td>
			<td>
				<input type="text" value="X-45" name="name" class="form-control" th:name="'products[' + ${status.getIndex()} + '].name'" th:value="${product.getName()}" />
			</td>
		</tr>
	</tbody>
</table>

You can repeatedly output List etc. by using <tr th: each>. Unlike JSTL etc., the object that manages the number of repetitions and the current line number can be referenced by the name declared on the right side separated by commas. It's easy.

In the above example, the actual output HTML is as follows.

<table class="table table-bordered">
	<thead>
		<th>id</th>
		<th>product name</th>
	</thead>
	<tbody>
		<tr>
			<td>
				<input type="text" class="form-control" name="products[0].id" value="0th id value" />
			</td>
			<td>
				<input type="text" class="form-control" name="products[0].name"  value="0th name value" />
			</td>
		</tr>
		<tr>
			<td>
				<input type="text" class="form-control" name="products[1].id" value="1st id value" />
			</td>
			<td>
				<input type="text" class="form-control" name="products[1].name"  value="1st name value" />
			</td>
		</tr>
........After that, repeat.......
	</tbody>
</table>

JSP input form example

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<s:form action="update" theme="simple">
<s:iterator value="products" var="product" status="status">
	<s:textfield name="products[%{#status.index}].id"></s:textfield>
	<s:textfield name="prodocts[%{#status.index}].name"></s:textfield>
</s:iterator>
<s:submit />
</s:form>
</body>
</html>

Recommended Posts

Input in tabular format with Struts2.5.x (JSP and Thymeleaf compatible)
Format Timestamp type in Thymeleaf
Draw screen with Thymeleaf in SpringBoot
Top 75+ JSP Interview Questions and Answers In 2020