简体   繁体   中英

Form 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://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>

My error was in entity inscription because of the default contructor is not public, it was protected.

th:field catch the value of field in a table and create a new inscription a set attribute with value you put in a table:

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

My 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM