Spring验证不会引发任何错误

时间:2016-10-14 04:33:46

标签: spring-mvc spring-validator

我尝试了一切可能的方法来解决它,但我失败了。所以,我有我的控制器工作,我也编写了代码来处理验证,但即使我发送了一些无效的数据也没有抛出任何错误。我刚刚应用了@notNull和@notEmpty验证来测试。这是我的码。请帮我弄清楚它有什么问题?

registerForm.jsp

<form:form method="POST" action="addRegistration" commandName="regForm">

   <table>
    <tr>
        <td><form:label path="name">Name : </form:label></td>
        <td><form:input path="name" /></td>
        <td align="left"><form:errors path="name" cssClass="error"/></td>
    </tr>
    <tr>
        <td><form:label path="age">Age : </form:label></td>
        <td><form:input path="age" /></td>
        <td align="left"><form:errors path="age" cssClass="error"/></td>
    </tr>
    <tr>
        <td><form:label path="email">Email : </form:label></td>
        <td><form:input path="email" /></td>
        <td align="left"><form:errors path="email" cssClass="error"/></td>
    </tr>
    <tr>
        <td><form:label path="refer">Refer : </form:label></td>
        <td><form:input path="refer" /></td>
        <td align="left"><form:errors path="refer" cssClass="error"/></td>
    </tr>

    <tr>
        <td colspan="2">
            <input type="submit" value="Submit"/>
        </td>
    </tr>
</table>  
</form:form>

SpringFormDem-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

   <context:component-scan base-package="registration" />
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>
   <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">

        <property name="basename" value="/WEB-INF/messages" />

    </bean>

 <bean id="myBeansValidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
</beans>

RegisterationController.java

@Controller
public class RegistartionController {

    @RequestMapping(value="/registrationForm", method = RequestMethod.GET)
    public String registrationForm(Model model)
    {
        model.addAttribute("regForm", new RegistationDetails()); 
        //return new ModelAndView("registrationForm", "command", new RegistationDetails());
        return "registrationForm";
    }

    @RequestMapping(value = "/addRegistration", method = RequestMethod.POST)
    public String addRegistration(@ModelAttribute("regForm") @Valid RegistationDetails register, BindingResult result, ModelMap model)
    {
        String ret;
        System.out.println(result.toString());
      if(result.hasErrors())
      {
          ret = "registrationForm";
      }
      else
      {
        model.addAttribute("name", register.getName());
          model.addAttribute("age", register.getAge());
          model.addAttribute("email", register.getEmail());
          model.addAttribute("refer", register.getRefer());

      ret = "displayResult";
      }
      return ret;

   }
}

RegistartionDetail.java

public class RegistationDetails {

    @NotNull(message = "Your name can not be null")
    @NotEmpty(message = "Your name can not be null")
    private String name;

    @NotNull(message = "Your email can not be null")
    @NotEmpty(message = "Your email can not be null")
    private String email;

    @NotNull(message = "Your refer can not be null")
    @NotEmpty(message = "Your refer can not be null")
    private String refer;

    @NotNull(message = "Your age can not be null")
    @NotEmpty(message = "Your age can not be null")
    private String age;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the refer
     */
    public String getRefer() {
        return refer;
    }

    /**
     * @param refer the refer to set
     */
    public void setRefer(String refer) {
        this.refer = refer;
    }

    /**
     * @return the age
     */
    public String getAge() {
        return age;
    }

    /**
     * @param age the age to set
     */
    public void setAge(String age) {
        this.age = age;
    }
}

解决方案:

从评论中我发现我的配置文件丢失了:

<mvc:annotation-driven />

当我将这一行添加到代码中时,它给我一个错误:

  

java.lang.IllegalStateException:ContainerBase.addChild:start:   org.apache.catalina.LifecycleException:   org.apache.catalina.LifecycleException:org.xml.sax.SAXParseException;   lineNumber:11; columnNumber:30;前缀&#34; mvc&#34;对于元素   &#34; MVC:注解驱动&#34;不受约束

要解决此错误,我需要在配置文件中添加几行:

<beans 
   ...
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="
   ...
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
</beans>

谢谢大家。

3 个答案:

答案 0 :(得分:0)

你需要有一个bindingresult来检查是否有错误。 一个快速的谷歌给了我这个例子: http://codetutr.com/2013/05/28/spring-mvc-form-validation/

答案 1 :(得分:0)

首次检查您的代码如何传递您的详细信息以进行验证,查看您的表单您没有为用户设置文本字段以传递任何值.....如果我在文本编辑器中运行您的表单没有字段,您是否尝试将输入类型设置为文本。类型=&#34;文本&#34;

... e.g

答案 2 :(得分:0)

解决方案:

这是适合我的解决方案。

从评论中我发现我的配置文件丢失了:

<mvc:annotation-driven />

当我将这一行添加到代码中时,它给我一个错误:

  

java.lang.IllegalStateException:ContainerBase.addChild:start:   org.apache.catalina.LifecycleException:   org.apache.catalina.LifecycleException:org.xml.sax.SAXParseException;   lineNumber:11; columnNumber:30;元素的前缀“mvc”   “mvc:annotation-driven”没有约束

要解决此错误,我需要在配置文件中添加几行:

<beans 
   ...
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="
   ...
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
</beans>

谢谢大家。

相关问题