gpt4 book ai didi

java - 使用 java、thymeleaf 和 spring mvc 形成表格

转载 作者:行者123 更新时间:2023-12-01 11:47:54 25 4
gpt4 key购买 nike

我的问题:

在我的保存方法中,我收到一个 inscriptionsForm 但它包含的是 null; inscriptionsForm.getInscriptions() == null,为什么?

我尝试了多种方法,例如:

    <div th:each="inscription, stat : *{inscriptions}">
<div th:each="inscription, stat : *{inscriptionsForm.inscriptions}">
<div th:each="inscription : *{inscriptionsForm.inscriptions}">

但始终为空。

我的代码:

    /** The Class TrainingTypeListController. */
@Controller
public class InscriptionListController {

/** The Constant TRAINING_VIEW_NAME. */
private static final String TRAINING_VIEW_NAME = "training/inscriptions";

/** The TrainingTypeService. */
@Autowired
private TrainingService trainingService;

/** The trainingTypes. */
public static List<Inscription> inscriptions;

/** The trainingTypes. */
private Training training;

/**
* Instantiates a new training Controller.
*/
public InscriptionListController() {
// Default empty constructor.
}

/**
* Show TrainingType List.
*
* @param model
* the model
* @return the string the view
*/

@RequestMapping(value = "trainingList/inscriptions/{trainingId}")
public String trainings(@PathVariable Long trainingId, Model model) {

//List of inscriptions
inscriptions = trainingService.getInscriptionsByTrainingId(trainingId);
model.addAttribute("inscriptions", inscriptions);

training = trainingService.findById(trainingId);
model.addAttribute("training", training);


InscriptionsForm inscriptionsForm = new InscriptionsForm();
inscriptionsForm.setInscriptions(inscriptions);
model.addAttribute(inscriptionsForm);
//System.out.println("-> " + inscriptionsForm);
//System.out.println("-> " + inscriptionsForm.getInscriptions());

return TRAINING_VIEW_NAME;
}

/**
* List of inscriptions.
*
* @return List<Inscription>
*/
@ModelAttribute("inscriptions")
public List<Inscription> inscriptions() {
return inscriptions;
}

/**
* List of inscriptions.
*
* @return Inscription
*/
@ModelAttribute("training")
public Training training() {
return training;
}

@RequestMapping(value = "trainingList/inscriptions/save", method = RequestMethod.POST)
public String save(@Valid @ModelAttribute InscriptionsForm inscriptionsForm) {
System.out.println("formulario: " + inscriptionsForm);
System.out.println("inscriptionsForm: " + inscriptionsForm.getInscriptions());
List<Inscription> inscriptions = inscriptionsForm.getInscriptions();


System.out.println("****\n " + InscriptionListController.inscriptions);
System.out.println("name: " + InscriptionListController.inscriptions.get(0).getAccount().getName());
System.out.println("note: " + InscriptionListController.inscriptions.get(0).getNote());

if(null != inscriptions && inscriptions.size() > 0) {
InscriptionListController.inscriptions = inscriptions;
for (Inscription inscription : inscriptions) {
System.out.printf("%s \t %s \n", inscription.getAccount().getName());
}
}
System.out.println("---------------------------------------------- ");
return "redirect:/trainingList";
}

}

表格

/** The InscriptionsForm. */
public class InscriptionsForm {

/** The Inscription List. */
private List<Inscription> inscriptions;

/** The getInscriptions. */
public List<Inscription> getInscriptions() {
return inscriptions;
}

/** The setInscriptions. */
public void setInscriptions(List<Inscription> inscriptions) {
this.inscriptions = inscriptions;
}
}

HTML

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:tiles="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title th:text="#{inscriptions.title}"></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="../../../resources/css/bootstrap.min.css" rel="stylesheet"
media="screen" th:href="@{/resources/css/bootstrap.min.css}" />
<link href="../../../resources/css/core.css" rel="stylesheet"
media="screen" th:href="@{/resources/css/core.css}" />
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="../../../resources/js/bootstrap.min.js"
th:src="@{/resources/js/bootstrap.min.js}"></script>
</head>
<body>

<!-- Generic Header -->
<div th:replace="fragments/header :: header(active='training')">&nbsp;</div>


<fieldset>

<!-- Verify if exits Inscriptions -->
<div th:unless="${#lists.isEmpty(inscriptionsForm.inscriptions)}">

<h2 class="text-center"
th:text="#{inscription.inscriptionlist}+${training.name}">List
of Training Types</h2>


<!-- Table List of Inscriptions -->
<form method="post" th:action="@{/trainingList/inscriptions/save}"
th:object="${inscriptionsForm}">
<table class="table table-striped table-hover form-narrow ">
<thead>
<tr>
<th th:text="#{name}">Name</th>
<th th:text="#{inscription.attend}">Attend</th>
<th th:text="#{inscription.note}">Note</th>
<th th:text="#{inscription.pass}">Pass</th>
<th th:text="#{inscription.unsubscribe}">Baja</th>
</tr>
</thead>
<tbody>


<!-- Table Inscriptions -->
<tr th:each="inscription : ${inscriptionsForm.inscriptions}">
<td th:text="${inscription.account.name}">name</td>
<td><input type="checkbox"
th:checked="${inscription.attend}"
th:title="#{inscription.infoAttend}" /></td>


<td><textarea id="note" th:text="${inscription.note}"
th:placeholder="#{note}" rows="3" cols="40"> Note </textarea></td>
<td><input type="checkbox" th:checked="${inscription.pass}"
th:title="#{inscription.infoPass}" /></td>
<td><input type="checkbox"
th:checked="${inscription.unsubscribe}"
th:title="#{inscription.infoUnsubscribe}" /></td>
</tr>
</tbody>
</table>
<!-- Button Modify, visible if a checkbox enable is pressed -->
<div class="text-center">
<button type="submit" class="btn btn-success btn-lg"
th:text="#{inscription.confirm}">Confirm</button>
</div>
</form>
</div>

</fieldset>
</body>
</html>

最佳答案

我的错误出现在实体铭文中,因为默认构造函数不是公开的,它是 protected 。

th:field 捕获表中字段的值,并使用您放入表中的值创建一个新的铭文集属性:

th:field="*{inscriptions[__${stat.index}__].attend}"

我的html:

    <!-- Table List of Inscriptions -->
<form method="post" th:action="@{/trainingList/inscriptionList/save}"
th:object="${inscriptionsForm}">
<table class="table table-striped table-hover form-narrow ">
<thead>
<tr>
<th th:text="#{name}">Name</th>
<th th:text="#{inscription.attend}">Attend</th>
<th th:text="#{inscription.note}">Note</th>
<th th:text="#{inscription.pass}">Pass</th>
<th th:text="#{inscription.unsubscribe}">Baja</th>
</tr>
</thead>
<tbody th:each="inscription, stat : *{inscriptions}">

<tr>


<td th:text="${inscription.account.name}">name</td>

<td class="text-center"><input
th:field="*{inscriptions[__${stat.index}__].attend}"
th:value="${inscription.attend}" type="checkbox"
th:checked="${inscription.attend}"
th:title="#{inscription.infoAttend}" /></td>

<td><textarea class="form-control"
th:field="*{inscriptions[__${stat.index}__].note}" id="note"
th:text="${inscription.note}" th:placeholder="#{note}" rows="3"
cols="40"> Note </textarea></td>

<td class="text-center"><input
th:field="*{inscriptions[__${stat.index}__].pass}"
type="checkbox" th:checked="${inscription.pass}"
th:title="#{inscription.infoPass}" /></td>

<td class="text-center"><input
th:field="*{inscriptions[__${stat.index}__].unsubscribe}"
type="checkbox" th:checked="${inscription.unsubscribe}"
th:title="#{inscription.infoUnsubscribe}" /></td>
</tr>
</tbody>
</table>

<!-- Button Confirm, confirm the list of inscription -->
<div class="text-center">
<button type="submit" class="btn btn-success btn-lg" th:name="save"
th:text="#{inscription.confirm}"
th:title="#{inscription.infoConfirm}">Confirm</button>
</div>


</form>
</div>

关于java - 使用 java、thymeleaf 和 spring mvc 形成表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29009687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com