jeudi 12 mars 2015

Forma Table with java, thymeleaf and spring mvc

My problem:


In my method save, i receive a inscriptionsForm but it contains is null; inscriptionsForm.getInscriptions() == null, Why?


I have tried multiple ways such as:



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


but is always null.


My code:



/** 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";
}

}


FORM



/** 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://ift.tt/wfNV60"
xmlns:tiles="http://ift.tt/wfNV60"
xmlns:sec="http://ift.tt/1vQCupq">
<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://ift.tt/rs8qB8"></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>

Aucun commentaire:

Enregistrer un commentaire