RegexpValidator不会阻止空字符串

时间:2013-08-30 11:21:31

标签: java regex validation vaadin

我的验证表单vaadin有问题,如果我没有在textfield中插入任何内容(内容只是0-8之间的数字),表单生成广告异常

Validator onlyNumber= new RegexpValidator("[0-8]","only number between 0 and 8") {
                    @Override
                    protected boolean isValidString(String value) {
                        if (value == "" || value.equals("") || value.isEmpty())
                            return false;
                        else
                            return value.matches("[0-8]");
                    }
                };  

此例外

Exception

com.vaadin.data.Property$ConversionException: java.lang.reflect.InvocationTargetException

at com.vaadin.data.util.MethodProperty.convertValue(MethodProperty.java:695)

at com.vaadin.data.util.MethodProperty.setValue(MethodProperty.java:664)

at com.vaadin.ui.AbstractField.commit(AbstractField.java:268)

at com.vaadin.ui.Form.commit(Form.java:339)

at it.ads.pta.web.planning.TabellaDinamica.commitButtonClick(TabellaDinamica.java:574)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:520)

at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:164)

at com.vaadin.ui.AbstractComponent.fireEvent(AbstractComponent.java:1219)

at com.vaadin.ui.Button.fireClick(Button.java:567)

at com.vaadin.ui.Button.changeVariables(Button.java:223)

at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.changeVariables(AbstractCommunicationManager.java:1481)

at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariableBurst(AbstractCommunicationManager.java:1425)

at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariables(AbstractCommunicationManager.java:1350)

at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleUidlRequest(AbstractCommunicationManager.java:780)

at com.vaadin.terminal.gwt.server.CommunicationManager.handleUidlRequest(CommunicationManager.java:325)

at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:501)

at it.ads.pta.web.servlet.SpringApplicationServlet.service(SpringApplicationServlet.java:95)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)

at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)

at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)

at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)

at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)

at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)

at java.lang.Thread.run(Thread.java:662)

Caused by: java.lang.reflect.InvocationTargetException

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)

at java.lang.reflect.Constructor.newInstance(Constructor.java:513)

at com.vaadin.data.util.MethodProperty.convertValue(MethodProperty.java:692)

... 37 more

Caused by: java.lang.NumberFormatException: For input string: ""

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Integer.parseInt(Integer.java:470)

at java.lang.Integer.<init>(Integer.java:660)

... 42 more

2 个答案:

答案 0 :(得分:1)

   @Override
   protected boolean isValidString(String value) {
         if (value==null || value.isEmpty() || value.trim().equals("") )
               return false;
         else
              return value.trim().matches("[0-8]");
   }

答案 1 :(得分:1)

也许将正则表达式更改为[0-8]?使用问号使其成为可选项会使其更好。你不需要那样扩展验证器。