Spring MVC Thymeleaf: не может возвращать значения из формы в контроллер - PullRequest
0 голосов
/ 06 августа 2020

Я пытаюсь вернуть значения из формы с помощью тимелеафа, но он не возвращает никакого значения. Я передаю объектный магазин из контроллера в шаблонизатор Thymeleaf. При возврате объекта в контроллер с помощью @ModelAttribute он возвращает значение null, и я не могу понять, как передать значения из формы в контроллер. Моя форма и контроллер приведены ниже

       The object 'shop' contains 5 fields, shopType (String) nullable, dayType (String) nullable, 
        rentType (String) nullable, dayType (String) nullable, rent (Double)
      -->
 <div class="container my-container">
        <form class="form" action="#" th:object="${shop}" th:method="post"  th:action="@{/addshop}">

            <div class="form-group">
                <label for="shoptype">Type of Shop</label>
                <input type="input" class="form-control" th:field="*{shopType}" id="shoptype">
            </div>  <!-- input field to take value of shopType -->
            <div class="form-group">
                <label for="rentaltype">Rental Type</label>
                <input type="input" class="form-control" th:field="*{rentType}" id="rentaltype">
            </div> <!-- input field to take value of RentType -->
            <div class="form-group">
                <label for="daytype">Days on which available</label>
                <input type="input" class="form-control" th:field="*{dayType}" id="daytype">
            </div>
            <div class="form-group">
                <label for="rent">Rent</label>
                <input type="input" class="form-control" th:field="*{rent}" id="rent">
            </div>
            <div class="form-group">
                    <label for="quantity">Properties Available</label>
                    <input type="input" class="form-control" th:field="*{available}" id="quantity">
            </div>

            <div class="form-group">
                <button type="submit" class="btn btn-primary">Add Shop</button>
            </div>
        </form>
    </div>

Мой контроллер выглядит как

package com.main1.main1.controller.admin;


import com.main1.main1.models.Shop;
import com.main1.main1.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller 
public class AddShopController {

    @Autowired
    IShopService shopService; > //Service to access database

    @GetMapping("/addshop")
    public ModelAndView showRegisterPage(){
        ModelAndView mv = new ModelAndView("admin/addShop");
        Shop shop = new Shop();
        mv.addObject("shop", shop);

        return mv;
    }

    //function to handle POST requests
    @PostMapping("/addshop")
    public ModelAndView addShop(@ModelAttribute("shop") Shop shop){
        System.out.println(shop); //Prints null
        shopService.addShop(shop);
        ModelAndView mv = new ModelAndView("redirect:/admin");

        return mv;

    }
}

Может ли кто-нибудь сказать, что я делаю неправильно?

...