[JAVA] Übergeben Sie die Formularklasse mit Spring-Boot vom Controller an Symleaf

Zweck

Mindestkonfiguration beim Übergeben eines Objekts vom Controller an die in Thymeleaf angezeigte Vorlage

Annahme

Verfahren

Eine Form Klasse sein

--Die Form-Klasse ist kein Ripository, sondern eine Klasse von Objekten, die beim Austausch zwischen dem Controller und der Ansicht übergeben wird, indem ein Wert für eine normale Java-Klasse festgelegt wird.

ContractListForm.java


package com.contract.web;

import lombok.Data;

@Data
public class ContractListForm{
	private String anken_kbn; //Artikelklassifizierung
	private String anken_name; //Projekttitel
}

Controller sein

--Erstellen Sie eine Instanz der Form-Klasse und haben Sie sie im Controller

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()); //Suchformularinstanz
		return mav;

	}

Eine Vorlagendatei sein

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>

――Das Feld darin ist

Wenn diese Einstellung nicht vorgenommen wird, tritt der folgende Fehler auf.


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?

Verwirrter Punkt

――Ich war verwirrt, als ich das Repository-Objekt erhielt. Ich habe es wie folgt geschrieben.

index.html


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

Die Anforderungsmethode des hier gesendeten Controllers ist tatsächlich dieselbe wie zuvor, und es war tatsächlich so

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()); //Suchformularinstanz
		return mav;

	}
mav.addObject("contractList", hdrEntityList);
<tr th:each="obj : ${contractList}">

--Erhalten in th: each, Variable: $ {im Instanznamen}

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

--Variable name.Field name in th: text, um den Wert anzuzeigen --- ---.


Zusammenfassung

Beispiel Beispielの取り方 Wie man das Feld nimmt
Form(POJO)Objekt th:object="${contractListF}" th:field="*{anken_kbn}
Entitätsobjekt th:each="obj : ${contractList}" th:text="${obj.anken_kbn}"

Recommended Posts

Übergeben Sie die Formularklasse mit Spring-Boot vom Controller an Symleaf
Wechseln Sie von JSP + JSTL zu Thymeleaf
[Kotlin] Drei Möglichkeiten, um Klasse von KClass zu bekommen
[Rails] Zuweisen von Variablen vom Controller zu JavaScript
Übergang von Struts2 zu Spring MVC (Controller)
[SpringBoot] So schreiben Sie einen Controller-Test
Von (Formular)
Verwandeln Sie sich von einer normalen Klasse in einen Lambda-Stil
SpringBoot nützliche Websitesammlung (von Zeit zu Zeit aktualisiert)
Migrieren Sie von Java zu Server Side Kotlin + Spring-Boot
So erhalten Sie eine Klasse von Element in Java
So trennen Sie .scss nach Controller in Rails