2016-10-14 7 views
1

可能な限り修正を試みましたが失敗しました。だから私は私のコントローラが動作していると私はまた、検証を処理するコードを書かれていますが、私はいくつかの無効なdata.Iを送信した場合でもエラーをスローしていない私はちょうど今test.Hereです@notNullと@コード。何が間違っているのか調べてください。春の検証でエラーが発生しない

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; 
    } 
} 

SOLUTION:

<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; The prefix "mvc" for element "mvc:annotation-driven" is not bound

<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> 

みんなありがとう:

は、私は設定ファイルに数行を追加するために必要な、このエラーを解決するには。

+0

例外はありますか?あなたはnullまたは空のデータがバックエンドに渡っていることを確信していますか? 'binding result(result)'オブジェクトに含まれているものをチェックしましたか? –

+0

はい、私の "displayresult.jsp"は、登録ページのユーザーが入力したデータを含む返されたオブジェクトからデータを読み込む空の値を表示しています。 エラーがあるかどうかを確認しようとしましたが、System.out.println(result.toString())としてtoString()メソッドを呼び出しましたが、常にorg.springframework.validation.BeanPropertyBindingResult:0エラーが返されます。 –

+0

オブジェクトに「NotNull」または「NotEmpty」がどのように来るかよりもデータがある場合はtrueですか? 'name'フィールドには何も入力しないでください。' RegistationDetails(regiater) 'オブジェクトは、名前フィールドにnullを表示し、それを表示する必要がある結果オブジェクトをチェックします。@NotNullエラー –

答えて

0

SOLUTION:ここ

が私のために働いたソリューションです。私は私の設定ファイルが欠落していたことが分かったコメントから

<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; The prefix "mvc" for element "mvc:annotation-driven" is not bound

このエラーを解決するためには、私は構成ファイルにいくつかの行を追加する必要がありました。

<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> 

ありがとうございました。

0

エラーがあるかどうかを調べるには、バインディング結果が必要です。あなたが任意の値を渡すために、ユーザのためのテキストフィールドを設定されていないフォームを見て、検証するためにあなたの詳細情報を渡しているか、あなたのコードの最初の検査から http://codetutr.com/2013/05/28/spring-mvc-form-validation/

+0

私はそれを私のコントローラに持っています。 public String addRegistration(@ModelAttribute( "regForm")@Valid RegistrationsDetails register、BindingResult result、ModelMap model) –

+1

mvc:注釈駆動型(または '@ EnableWebMVC'設定ですか?また、彼のコメントで@Samが言うことを参照してください。 –

+0

を追加すると、次のようなエラーが表示されます:アプリケーションの読み込み中に例外が発生しました:java.lang.IllegalStateException:ContainerBase.addChild:start:org.apache.catalina.LifecycleException:org.apache .catalina.LifecycleException:org.xml.sax.SAXParseException; lineNumber:11; columnNumber:30;要素 "mvc:annotation-driven"の接頭辞 "mvc"はバインドされていません。 –

0

: A簡単にGoogleは私にこの例を与えました。 ....テキストエディタでフォームを実行しても、フィールドは存在しません。入力タイプをテキストに設定することを試みましたか?タイプ= "テキスト" 例えば

...

+1

関連する問題