繁体   English   中英

Spring Boot错误消息不起作用

[英]Spring Boot error message doesn't work

我正在编写我的第一个春季启动应用程序而且我遇到了这个问题。 我无法向用户显示错误消息。 没有该数据的对象不会保存在数据库中,这没关系。 但显示错误消息是问题所在。 当我调试我得到错误大小= 0

这是模特

@Size(min = 1, message = "Address is invalid.")
@NotNull
@Column
private String address;

调节器

@RequestMapping(value = "/create", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String createNewBusiness(@Valid @ModelAttribute("business") Business business,
                                BindingResult result, Model model) {
    model.addAttribute("userEmail", getUserEmail());
    logger.info("/business/create:" + business.toString());
    LocationResponse locationResponse = geoService.getCoords(business.getAddress());

    if (locationResponse.getStatus().equals("OK")) {
        business.setLatitude(locationResponse.getResults().get(0).getGeometry().getLocation().getLat());
        business.setLongitude(locationResponse.getResults().get(0).getGeometry().getLocation().getLng());
        business.setUserId(getUserId());

        businessService.createNew(business);

        model.addAttribute("business", business);

    } else {
        business.setAddress(null);
        model.addAttribute("business", business);
    }

    if (result.hasErrors()) {
        List<FieldError> errors = result.getFieldErrors();
        for (FieldError error : errors ) {
            System.out.println (error.getObjectName() + " - " + error.getDefaultMessage());
        }
        return "newBusiness";
    }

    return "business";
}

Thymeleaf

<div class="input-field left m-0 w-100">
    <i class="fa fa-map-marker prefix grey-text" aria-hidden="true"></i>
    <input placeholder="Address" id="inputAddress" name="address" type="text" class="validate my-0" th:required="true">
    <label th:errors="*{address}" th:if="${#fields.hasErrors('address')}" >Invalid address</label>
</div>

您是否在@SpringBootApplication定义了Validator

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

    @Bean
    public Validator validator() {
        return new LocalValidatorFactoryBean();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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