Spring boot 如何解决控制器配置上的问题?
我的spring boot项目中的类控制器中的anotation有问题。我应该用什么方法Spring boot 如何解决控制器配置上的问题?,spring-boot,Spring Boot,我的spring boot项目中的类控制器中的anotation有问题。我应该用什么方法 @Controller @RequestMapping("/products") public class ProductController { /** * Only a Test */ @Autowired private ProductService productService; 您可能至少应该有CRUD方法,比如: @GetMapping public String i
@Controller
@RequestMapping("/products")
public class ProductController {
/**
* Only a Test
*/
@Autowired
private ProductService productService;
您可能至少应该有CRUD方法,比如:
@GetMapping
public String index(Model model) {
List<Product> products = productService.searchAll();
model.addAttribute("products", products);
return "product/index";
}
@GetMapping("/addProduct")
public String add(Product product, Model model) {
logger.info("Creating new 'Product' on data source");
this.productService.inserir(product);
return "product/addProduct";
}
@GetMapping("/editProduct/{id}")
public String edit(@PathVariable("id") Long id, Model model) {
logger.info("Find 'Product' Id: {} on data source", id);
Product product = this.productService.searchById(id);
model.addAttribute("product", product);
return "product/editProduct";
}
@GetMapping("/deleteProduct/{id}")
public String delete(@PathVariable("id") Long id, Model model) {
logger.info("Deleting 'Product' Id:{} from data source", id);
this.productService.deletar(id);
return index(model);
}
@PostMapping("/saveProduct")
public String save(@Valid Product product, BindingResult result, Model model) {
if (result.hasErrors()) {
return add(product, model);
}
if (product.getId() != 0) {
logger.info("Updating 'Product' Id: {} on data source", product.getId());
this.productService.atualizar(product);
} else {
logger.info("Creating new 'Product' on data source");
this.productService.inserir(product);
}
return index(model);
}
@GetMapping
公共字符串索引(模型){
列出产品=productService.searchAll();
model.addAttribute(“产品”,产品);
返回“产品/索引”;
}
@GetMapping(“/addProduct”)
公共字符串添加(产品、型号){
logger.info(“在数据源上创建新的“产品”);
this.productService.inserir(产品);
返回“产品/添加产品”;
}
@GetMapping(“/editProduct/{id}”)
公共字符串编辑(@PathVariable(“id”)长id,模型){
info(“在数据源上查找'Product'Id:{}”,Id);
Product Product=this.productService.searchById(id);
model.addAttribute(“产品”,产品);
返回“产品/编辑产品”;
}
@GetMapping(“/deleteProduct/{id}”)
公共字符串删除(@PathVariable(“id”)长id,模型){
info(“从数据源中删除'Product'Id:{}”,Id);
this.productService.deletar(id);
收益指数(模型);
}
@后映射(“/saveProduct”)
公共字符串保存(@Valid Product,BindingResult,Model){
if(result.hasErrors()){
退货增加(产品、型号);
}
if(product.getId()!=0){
info(“在数据源上更新'Product'Id:{}”,Product.getId());
this.productService.atualizar(产品);
}否则{
logger.info(“在数据源上创建新的“产品”);
this.productService.inserir(产品);
}
收益指数(模型);
}
您可能至少应该有CRUD方法,比如:
@GetMapping
public String index(Model model) {
List<Product> products = productService.searchAll();
model.addAttribute("products", products);
return "product/index";
}
@GetMapping("/addProduct")
public String add(Product product, Model model) {
logger.info("Creating new 'Product' on data source");
this.productService.inserir(product);
return "product/addProduct";
}
@GetMapping("/editProduct/{id}")
public String edit(@PathVariable("id") Long id, Model model) {
logger.info("Find 'Product' Id: {} on data source", id);
Product product = this.productService.searchById(id);
model.addAttribute("product", product);
return "product/editProduct";
}
@GetMapping("/deleteProduct/{id}")
public String delete(@PathVariable("id") Long id, Model model) {
logger.info("Deleting 'Product' Id:{} from data source", id);
this.productService.deletar(id);
return index(model);
}
@PostMapping("/saveProduct")
public String save(@Valid Product product, BindingResult result, Model model) {
if (result.hasErrors()) {
return add(product, model);
}
if (product.getId() != 0) {
logger.info("Updating 'Product' Id: {} on data source", product.getId());
this.productService.atualizar(product);
} else {
logger.info("Creating new 'Product' on data source");
this.productService.inserir(product);
}
return index(model);
}
@GetMapping
公共字符串索引(模型){
列出产品=productService.searchAll();
model.addAttribute(“产品”,产品);
返回“产品/索引”;
}
@GetMapping(“/addProduct”)
公共字符串添加(产品、型号){
logger.info(“在数据源上创建新的“产品”);
this.productService.inserir(产品);
返回“产品/添加产品”;
}
@GetMapping(“/editProduct/{id}”)
公共字符串编辑(@PathVariable(“id”)长id,模型){
info(“在数据源上查找'Product'Id:{}”,Id);
Product Product=this.productService.searchById(id);
model.addAttribute(“产品”,产品);
返回“产品/编辑产品”;
}
@GetMapping(“/deleteProduct/{id}”)
公共字符串删除(@PathVariable(“id”)长id,模型){
info(“从数据源中删除'Product'Id:{}”,Id);
this.productService.deletar(id);
收益指数(模型);
}
@后映射(“/saveProduct”)
公共字符串保存(@Valid Product,BindingResult,Model){
if(result.hasErrors()){
退货增加(产品、型号);
}
if(product.getId()!=0){
info(“在数据源上更新'Product'Id:{}”,Product.getId());
this.productService.atualizar(产品);
}否则{
logger.info(“在数据源上创建新的“产品”);
this.productService.inserir(产品);
}
收益指数(模型);
}