【问题标题】:why my spring validation annotations doesnt work为什么我的春季验证注释不起作用
【发布时间】:2013-09-06 13:52:03
【问题描述】:

我很简单,我把一个字符串放在哪里,我使用注释来验证值 它不起作用

这是我的课:

public class Destination
{
    @NotNull
    String address;

    public Destination(){}

    public String getAddress() {
        return address;
    }

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

这是我的控制器方法

@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 Status 500 - Request processing failed; nested exception is 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)

【问题讨论】:

  • 请向我们展示异常堆栈跟踪。
  • 用堆栈跟踪更新了我的问题
  • 我不知道为什么没有发生验证,但显然错误来自LocationManager.getLocation,所以告诉我们。
  • 也许我在目标类文件中为@notnull 注释导入了错误的类?我导入了import com.sun.istack.internal.NotNull; 好吗?它来自那里,因为它将destination.getAddress()作为空值并尝试获取空地址的绳索
  • 我对此表示怀疑。你应该有javax.validation.constraints.NotNull

标签: java spring validation spring-mvc nosuchelementexception


【解决方案1】:

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

【讨论】:

  • 好的,它就像那样工作,但它不显示错误消息,我认为会显示某种默认消息,或者因为我做了重定向?
猜你喜欢
  • 2013-02-02
  • 2018-09-04
  • 2016-01-19
  • 2016-06-18
  • 2023-03-20
  • 2015-12-16
  • 1970-01-01
  • 2017-03-02
  • 1970-01-01
相关资源
最近更新 更多