java - 为什么我的 spring 验证注释不起作用

标签 java spring validation spring-mvc nosuchelementexception

我有一个简单的方法来放置一个字符串,并且我使用注释来验证该值 但它不起作用

这是我的类(class):

public class Destination
{
    @NotNull
    String address;

    public Destination(){}

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

这是我的 Controller 方法

@RequestMapping(value = "/search", method = RequestMethod.POST)
public String findDestination(@ModelAttribute("destination") @Valid Destination destination, BindingResult result, RedirectAttributes redirectAttrs) {
    if(result.hasErrors()) {
        return "redirect:/";
    }
    Location location = LocationManager.getLocation(destination.getAddress());
    Weather weather = WeatherManager.getWeather(location);
    redirectAttrs.addFlashAttribute("weather", weather);
    redirectAttrs.addFlashAttribute("location", location);
    return "redirect:/";
}

这是我在 jsp 文件中的表单:

    <form:form method="post" action="search" commandName="destination" class="form-horizontal">
    <div class="control-group">
        <div class="controls">
            <form:input path="address" placeholder="Enter destination address"/>
            <form:errors path="address" cssclass="error"></form:errors>
            <input type="submit" value="Search" class="btn"/>
            </form:form>
        </div>
    </div>

所以问题是它不验证我的输入 当我将其留空时,它仍然尝试从空目标对象获取位置对象的地址,并且出现异常 HTTP 状态 500 - 请求处理失败;嵌套异常是 java.util.NoSuchElementException

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.util.NoSuchElementException
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause

java.util.NoSuchElementException
    java.util.ArrayList$Itr.next(ArrayList.java:794)
    com.springapp.mvc.domain.LocationManager.getLocation(LocationManager.java:52)
    com.springapp.mvc.controller.HomeController.findDestination(HomeController.java:51)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

最佳答案

提交时的空 html 输入是空字符串,这可能就是您的验证不起作用的原因。您可以尝试添加@Size注释,即@Size(min=1)

关于java - 为什么我的 spring 验证注释不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18659490/

相关文章:

java - Hibernate 关系映射不是双向的

Java Spring Web 服务客户端故障处理

java - 我需要对基于 Spring 的 Rest Web 服务进行验证

php - 确保信息之后?是一个整数

java - Java 中接受任意类型作为参数的方法

java - 使用 <context :property-placeholder> in a factory method 定义的属性

java - 二维数组结构算法

wpf - 如何强制ItemsControl验证行更改?

java - 如何监控第三方库的方法执行?

java - usb4Java USB 错误 3 : Unable to open USB device: Access denied (insufficient permissions)