1

JSPページに表示される検証エラーメッセージを取得できません。これが私のコントローラーです:

@Controller
@RequestMapping("/secure")
@SuppressWarnings({"All"})
public class OperationController {

    @ModelAttribute("crawler/operation")
    public OperationForm operationForm() {
        return new OperationForm();
    }

    @RequestMapping(value = "crawler/operation/create", method = RequestMethod.POST)
    public String processCrawlerOperationForm(@Valid OperationForm operationForm, BindingResult result, Map model) {
        if (result.hasErrors()) {
            HashMap<String, String> errors = new HashMap<String, String>();
            for (FieldError error : result.getFieldErrors()) {
                errors.put(error.getField(), error.getDefaultMessage());
            }
            model.put("errors", errors);
            return "crawler/operation";
        }
        //Some logic
        return "crawler/operation";
    }

}

エラーが発生することは100%確信しています。エラーがあることを確認するために、このコードをデバッグしています。

私のフォームクラス:

public class OperationForm {

    @NotEmpty
    private String operationName;

    @NotEmpty
    private String author;

    @NotEmpty
    private String configurationId;

    public String getOperationName() {
        return operationName;
    }
    //Getters and setters
}

私のJSPスプリングフォーム:

<form:form action="${pageContext.servletContext.contextPath}/secure/crawler/operation/create.htm" commandName="crawler/operation">
    <table border="0" cellspacing="12">
        <tr>
             <td>
                 <spring:message code="application.operationForm.operationName"/>
             </td>
             <td>
                <form:input path="operationName"/>
             </td>
             <td class="error">
                <form:errors path="operationName"/>
             </td>
        </tr>
        <tr>
             <td>
                 <spring:message code="application.operationForm.author"/>
             </td>
             <td>
                <form:input path="author"/>
             </td>
             <td class="error">
                <form:errors path="author"/>
             </td>
        </tr>
        <tr>
             <td>
                 <spring:message code="application.operationForm.configuration"/>
             </td>
             <td>
                <form:input path="configurationId"/>
             </td>
             <td class="error">
                <form:errors path="configurationId"/>
             </td>
        </tr>
        <tr>
             <td>
                 <input class="button" type="submit" value="Vykdyti"/>
             </td>
        </tr>
    </table>
</form:form>

私は何が間違っているのですか?

4

1 に答える 1

2

エラーのリストをループしてモデル オブジェクトに挿入する代わりに、validate をチェックしてビューを返すだけです。

if(result.hasErrors())
  return "crawler/operation";
...

また、コントローラー メソッドに @ModelAttribute アノテーションを追加します。

public String processCrawlerOperationForm(@ModelAttribute("form") @Valid OperationForm operationForm, BindingResult result, Map model)

ModelAttribute アノテーションの値が、フォームを最初に表示するときにモデル オブジェクトに設定する値と一致することを確認してください。上記の operationForm メソッドを使用してモデルを生成している場合は、その値が何であれ設定します。それをより使いやすい値に変更すると、Spring が / で混乱する可能性があるため、「フォーム」のような単純なものに変更します。

于 2012-05-31T17:31:11.933 に答える