[JAVA] Pass Form class from Controller to Thymeleaf by Spring-Boot

Purpose

Minimum configuration when passing an object from Controller to the template displayed in Thymeleaf

Premise

--Thymeleaf has been installed

procedure

Being a Form class

--The Form class is not a repository, but a class of objects that is passed when exchanging between a controller and a view by putting a value on an ordinary java class.

--The java class can be a POJO class --Getter and Setter are absolutely necessary --Lanbok can be used. Just add @Data and the accessor is okay

ContractListForm.java


package com.contract.web;

import lombok.Data;

@Data
public class ContractListForm{
	private String anken_kbn; //Item classification
	private String anken_name; //Project Title
}

Being a Controller

--Create an instance of Form class and have it in the controller --Pass an instance of Form class to the screen

--Generate the previous class with the method with @ModelAttribute annotation This annotated method is automatically called when the method with @RequestMapping is called (when the request is received). Create an instance of the form class with this annotated method The instance created here will be registered in the request scope -Pass the object to the screen with the @RequestMapping method. In the addObject method, define the instance name on the screen side with the first argument, receive from the screen with this name Pass the object that is actually passed in the second argument

ContractListsController


    @ModelAttribute
	ContractListForm setUpForm() {
        return new ContractListForm();
    }

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView index(ModelAndView mav) {
		mav.setViewName("index");
		mav.addObject("contractListF", new ContractListForm()); //Instance for search form
		return mav;

	}

Being a template file

--First, receive the object with the form tag

index.html


<form method="post" action="/Contract/search" th:object="${contractListF}">

index.html


<input type="text" th:field="*{anken_kbn}" class="form-control" aria-describedby="basic-addon2"></input>
<input type="text" th:field="*{anken_name}" class="form-control" aria-describedby="basic-addon2"></input>

――The field in it is

If this setting is not done, the following error will occur. --Although the accessor is defective, the same error message is displayed even when the above rule is not actually established.


org.springframework.beans.NotReadablePropertyException: Invalid property 'contractListForm' of bean class [com.contract.web.ContractListForm]: Bean property 'contractListForm' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

Confused point

――I was confused when I received the repository object, I wrote it as follows.

index.html


<tr th:each="obj : ${contractList}">
				<td th:text="${obj.anken_kbn}"></td>
				<td th:text="${obj.anken_mei}"></td>

The request method of the controller sent here is actually the same as before, and it was actually like this

ContractListsController.java


@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView index(ModelAndView mav) {
		mav.setViewName("index");
		Iterable<TMitsumoriHdrEntity> hdrEntityList = hdrDao.findAll();
		mav.addObject("contractList", hdrEntityList);
		mav.addObject("contractListF", new ContractListForm()); //Instance for search form
		return mav;

	}

--Here, the list of Entity obtained from dao is passed with the name *** contractList *** as follows.

mav.addObject("contractList", hdrEntityList);

--In this case, the Entity is received in the list and you get it in a loop, in that case

<tr th:each="obj : ${contractList}">

--Received by th: each, variable: $ {instance name} --For that variable

 <td th:text="${obj.anken_kbn}"></td>

--Variable name.Field name in th: text to display the value


Summary

instance instanceの取り方 How to take the field
Form(POJO)Object th:object="${contractListF}" th:field="*{anken_kbn}
Entity object th:each="obj : ${contractList}" th:text="${obj.anken_kbn}"

Recommended Posts

Pass Form class from Controller to Thymeleaf by Spring-Boot
Switch from JSP + JSTL to Thymeleaf
[Kotlin] 3 ways to get Class from KClass
[Rails] Assigning variables from controller to JavaScript
Transition from Struts2 to Spring MVC (Controller)
[SpringBoot] How to write a controller test
From (form)
Transform from a normal class to a lambda expression
SpringBoot useful site collection (updated from time to time)
Migrate from Java to Server Side Kotlin + Spring-boot
How to get Class from Element in Java
How to separate .scss by controller in Rails