0

メッセージを返すことができない HTML/endoUI UI を備えた SpringMVC 3.2 サービスがあります。私が入力した RequestAttributes は Javascript には見えないようです。私が見た SpringMVC エラー処理の例はすべて、html または jsp リダイレクトを使用しています。

以下にサンプルを示します。

@Controller
@RequestMapping( "/officeSignUp" )
public class OfficeSignUpController
..
@RequestMapping( value = "/stepOne", method = RequestMethod.POST )
@ResponseBody
public String officeCreationStepOne( @Valid @ModelAttribute( "officeSetup" ) OfficeSetup officeSetup,
        BindingResult result, Model model, RedirectAttributes attributes ) {
    String results;

    results = newOfficeValidator.validateStepOne( officeSetup, result );

    NewCustomerSignup newCustSignup = newOfficeValidator.validateNewOwner( officeSetup );

    model.addAttribute( newCustomerSignupRepository.save( newCustSignup ) );

    return results;

}

私のバリデーターは次のとおりです。

@Component public class NewOfficeValidator {

public String validateStepOne( OfficeSetup officeSetup, BindingResult result ) {

List<String> errors = new ArrayList<String>();
if (result.hasErrors()) {
    for (ObjectError error : result.getAllErrors()) {
        errors.add( error.getDefaultMessage() );
    }
    errors.add( "redirect: " + VIEW_STEP_ONE );
           // RuntimeException with a list of strings in it
    throw new OfficeSetupException( errors, "Validation in StepOne" );
  }


    return VIEW_STEP_TWO;
}

BaseController で例外をキャッチし、エラー メッセージを取得して使用します。

@ExceptionHandler
@ResponseStatus( HttpStatus.BAD_REQUEST )
@ResponseBody
public ErrorMessage handleOfficeSetupException( OfficeSetupException ex ) {

    List<String> errors = ex.getErrors();
    StringBuilder sb = new StringBuilder();
    for (String error : errors) {
        sb.append( error );
    }
        // this is just a bean with a list of Strings
    return new ErrorMessage( errors );
}

例外がスローされると、json または文字列の応答ではなく、次を含む tomcat html メッセージが表示されます。

<h1>HTTP Status 400 - Received OfficeSetupException </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Received OfficeSetupException </u></p><p><b>description</b> <u>The request sent by the client was syntactically incorrect (Received OfficeSetupException ).</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.27</h3></body></html>

そしてJavaScriptで:

if(stepString == "Step 2"){ click on step 2
       if(viewModelStepOne.validateStepOne(s) == true){ client side validation
           viewModelStepOne.saveStepOne(s);if above validation passes, send request to server,
       if(serverValidationFailed){if server returns error, do not move to step 2
            s.preventDefault();
       }else{ move to step 
          this.disable(this.tabGroup.children("li").eq(2),true);
          this.enable(this.tabGroup.children("li").eq(3),true);
       }
    }else{ s.preventDefault();
    }
}

したがって、最終的に私の質問は次のとおりです。Spring MVC から JavaScript フロントエンドに検証またはその他のエラー メッセージを返す最良の方法は何ですか? 応答に JSON を使用しようとしているので、ErrorMessage がそのまま返されることが期待されます。

4

2 に答える 2

1

私のプロジェクトでは、あなたが説明したように実行します-例外をスローし、@ExceptionHandler でキャッチします。ただし、文字列メッセージだけを返す必要はありません。引数としてエラーの Map または List (またはエラー モデルとして使用したいもの) を取ることができる独自の例外クラスを作成します。エラーが発生したら、カスタム execption (RequestException と呼びましょう) にエラーを入力してからスローします。エラーは例外インスタンスに保存されます。RequestException の @ExceptionHandler で、例外オブジェクトからエラーをフェッチし、便利な方法でフロントエンドに戻ります。

于 2013-08-28T07:45:55.393 に答える