How to bind request parameters in list format with bean property in Spring

How to bind list format request parameters

You can bind the request parameters when the bean property is in list format with the following code.


<body>
	<form action="/sample/bean" method="post">
		<table>
			<tr>
				<td>Input 1:price:</td>
				<td><input type="text" name="sampleBean.childBean[0].price"></td>
				<td>Input 1:unit:</td>
				<td><input type="text" name="sampleBean.childBean[0].unit"></td>
			</tr>
			<tr>
				<td>Input 2:price:</td>
				<td><input type="text" name="sampleBean.childBean[1].price"></td>
				<td>Input 2:unit:</td>
				<td><input type="text" name="sampleBean.childBean[1].unit"></td>
			</tr>
		</table>
	</form>
</body>
@Controller
public class SampleController {

	@RequestMapping(value="/sample/bean", method=RequestMethod.POST)
	public String goUserCreateErrorPage(SampleBean sampleBean) {
		return "sample";
	}
}
public class SampleBean {
  private String childBeanUnit;
  private List<SampleChildBean> childBean;
}
※getter,setter is abbreviation

public class SampleChildBean {
	private String price;
	private String unit;
}
※getter,setter is abbreviation

point

In this case, there is a point in how to define the name attribute of input. If you explain one by one, Explanation 1: Argument name of the binding target specified in the argument of @RequestMapping method on the Controller side Explanation 2: Specify the property name defined in the class of the argument to be bound. Since it is a list, specify the index number as well. Explanation 3: Finally, specify the property of the property class specified in Explanation 2 (is it complicated?)

<input type="text" name="sampleBean.childBean[0].price">
Commentary 1 Commentary 2 Commentary 3

At the end

It's good to write it before I forget it, but the explanation is not good enough. Let's think of a better explanation when we have time, lol

Recommended Posts

How to bind request parameters in list format with bean property in Spring
How to bind to property file in Spring Boot
How to get parameters in Spark
Let's find out how to receive in Request Body with REST API of Spring Boot
Convert request parameter to Enum in Spring
How to include Spring Tool in Eclipse 4.6.3?
How to get the setting value (property value) from the database in Spring Framework
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
Tests with @Parameters in Spring 4.3 should have no parameters
How to add a classpath in Spring Boot
One way to redirect_to with parameters in rails
How to define multiple orm.xml in Spring4, JPA2.1
`bind': Address already in use --bind (2) for 127.0.0.1:3000 (Errno :: EADDRINUSE) How to deal with the error
How to save a file with the specified extension under the directory specified in Java to the list
How to implement UICollectionView in Swift with code only
How to sort in ascending / descending order with SQLite
How to call functions in bulk with Java reflection
How to switch Tomcat context.xml with WTP in Eclipse
[Java] How to omit spring constructor injection with Lombok
How to deal with No template for interactive request
How to use Z3 library in Scala with Eclipse
Encode query parameters with Uri Components Builder in spring
Organized how to interact with the JDK in stages
[How to insert a video in haml with Rails]
How to use CommandLineRunner in Spring Batch of Spring Boot
How to use JDD library in Scala with Eclipse
How to test file upload screen in Spring + Selenium
How to query Array in jsonb with Rails + postgres
How to use In-Memory Job repository in Spring Batch
How to use the same Mapper class in multiple data sources with Spring Boot + MyBatis
How to request a CSV file as JSON with jMeter
How to connect the strings in the List separated by commas
Authenticate 3 or more parameters in JSON format using Spring Security
Extract SQL to property file with jdbcTemplate of spring boot
How to get values in real time with TextWatcher (Android)
Overwrite bean definition in spring xml configuration file with another xml
[Easy] How to automatically format Ruby erb file with vsCode
How to call and use API in Java (Spring Boot)
Serializing org.joda.time.DateTime to JSON in ISO 8601 format comes with milliseconds
To receive an empty request with Spring Web MVC @RequestBody
Mapping to a class with a value object in How to MyBatis
Implement writing and reading to Property List (.plist) in Swift
[Rails5] japanMap link How to write parameters in js.erb file
How to set up a proxy with authentication in Feign
How to control transactions in Spring Boot without using @Transactional
[Java] How to search for a value in an array (or list) with the contains method
How to number (number) with html.erb
How to update with activerecord-import
How to deal with 405 Method Not Allowed error in Tomcat + JSP
I want to understand the flow of Spring processing request parameters
How to make a jar file with no dependencies in Maven
How to realize huge file upload with TERASOLUNA 5.x (= Spring MVC)
How to create your own Controller corresponding to / error with Spring Boot
How to set and use profile in annotation-based Configuration in Spring framework
How to run a job with docker login in AWS batch
Only this! How to prevent forgetting code format in team development
How to get boolean value with jQuery in rails simple form
How to rename a model with foreign key constraints in Rails
How to output a list of strings in JSF as comma-separated strings
How to encrypt and decrypt with RSA public key in Java