【问题标题】:In spring boot project, When I click submit button, how can I solve Whitelabel Error Page Error,在spring boot项目中,当我点击提交按钮时,如何解决Whitelabel Error Page Error,
【发布时间】:2017-10-16 15:11:01
【问题描述】:

当我点击提交按钮时,我收到此错误消息

Whitelabel 错误页面 此应用程序没有明确的映射 /error,因此您将其视为后备。 6 月 30 日星期二 17:24:02 CST 2015 出现意外错误(类型=未找到,状态=404)。不 消息可用

这是我的代码。

 package com.tourpackage.controllers;

    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.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;


    import com.tourpackage.model.TourPackage;
    import com.tourpackage.repository.TourPackageMongoRepository;
    import com.tourpackage.repository.VehicleTypeMongoRepository;

    @Controller
    public class TourPackageController {

        @Autowired
        TourPackageMongoRepository packageRepository;
        VehicleTypeMongoRepository vehicleTypeRepository;

        @RequestMapping("/tourpackage")
        public String tourpackage(Model model){
            model.addAttribute("packagelist", packageRepository.findAll());
            return "index";
        }

        @RequestMapping("/addNewTour")
        public String addNewTour(Model model){
            model.addAttribute("packagelist", packageRepository.findAll());
            return "tourpack";
        }


        @RequestMapping(value="/addPackage", method = RequestMethod.POST)
        public String addPackage(@ModelAttribute TourPackage tourpack) {
            packageRepository.save(tourpack);
            return "redirect:tourpackage";
        }

    }

【问题讨论】:

    标签: spring-boot


    【解决方案1】:

    当您没有为 ErrorController 指定实现时,Spring Boot 会自动将 Basic ErrorController 注册为 Spring Bean。 所以, 如果要返回路径/error的自定义内容,请参考以下代码:

    @RestController 公共类 MyController 实现 ErrorController{

    private static final String PATH = "/error";
    
    @RequestMapping(value = PATH)
    public String error() {
        return "Error handling";
    }
    
    @Override
    public String getErrorPath() {
        return PATH;
    }
    

    }

    否则, 如果你想禁用它,你可以参考这篇文章: http://www.logicbig.com/tutorials/spring-framework/spring-boot/disable-default-error-page/

    【讨论】:

    • 您是否在 main() 类中指定了所有带有注释 @ComponentScan 的控制器和服务类?
    猜你喜欢
    • 2020-07-30
    • 2019-09-10
    • 2019-05-03
    • 2020-01-26
    • 1970-01-01
    • 2019-04-04
    • 2020-04-15
    • 2018-07-22
    • 2021-03-01
    相关资源
    最近更新 更多