我需要一些帮助,如果我需要开始我的项目,我遇到这样的错误。原因是什么呢?
控制台:
No primary or single unique constructor found for interface org.springframework.ui.Model
代码:
package com.Elesgerli.MarketShop_Azer.Controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.Elesgerli.MarketShop_Azer.Entity.Cashier;
import com.Elesgerli.MarketShop_Azer.Service.cashierService;
import com.Elesgerli.MarketShop_Azer.Service.productService;
@Controller
public class CashierController{
@Autowired
private cashierService cservice;
@Autowired
private productService pservice;
@PostMapping("/saveCashier")
public String save( @RequestBody @ModelAttribute Model m ,Cashier c) {
List<Cashier> cashiers=cservice.getAllCashier();
m.addAttribute("cashier", c);
cservice.save(c);
return "redirect:/cashierList";
}
@RequestMapping("/delete/{id}")
public String deleteUser(@PathVariable Integer id) {
cservice.deletebyId(id);
return "redirect:/cashierList";
}
}
我想通过运行我的项目来添加一个新产品,但是它给出了这样的错误。
In "/savecasier "控制器实例化Model,然后创建一个包含所有收银员列表的Model属性,如下所示:
@PostMapping("/saveCashier")
public String save(@ModelAttribute Cashier c, //Object of cashier created as c with details retrieved from the client side
Model model
//Model instantiated
) {
cservice.save(c); // cashier saved
List<Cashier> cashiers=cservice.getAllCashier(); //fetched list of all cahiers
model.addAttribute("cashiers", cashiers) // List of all cashiers added as attribute
return "redirect:/cashierList";
}
如果你不能理解@ModelAttribute收银员c,那么使用RequestParam从客户端获取数据,然后实例化一个新的收银员对象,并使用使用RequestParam检索的数据设置它的字段。然后保存它并将所有收银员的列表添加到模型属性中,如下所示:
@PostMapping("/saveCashier")
public String save( @RequestParam ("name") String name, //Similary for other properties of cashier like date of birth or anything
Model model //Model instantiated
) {
Cashier c = new Cashier();
c.setName(name);
//Similarly for other fields
cservice.save(c); // cashier saved
List<Cashier> cashiers=cservice.getAllCashier(); //fetched list of all cahiers
model.addAttribute("cashiers", cashiers) // List of all cashiers added as attribute
return "redirect:/cashierList";
}