Minimum configuration when passing an object from Controller to the template displayed in Thymeleaf
--Thymeleaf has been installed
--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
}
--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;
}
--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?
――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>
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