带有JavaScript UI的SpringMVC错误处理
SpringMVC ErrorHandling with JavaScript UI
我有一个SpringMVC 3.2服务,它带有HTML/endoUI UI,我无法向其返回消息。我填充的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;
}
我的验证器是:
@组件公共类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();
}
}
因此,最终我的问题是:从springmvc向javascript前端返回验证或其他错误消息的最佳方式是什么?我正在尝试使用JSON作为响应,所以我希望ErrorMessage也能这样返回。
在我的项目中,我按照您的描述进行操作-抛出异常并用@ExceptionHandler捕获它。但您不需要只返回一条字符串消息。创建您自己的异常类,该类可以将Map或List of errors作为参数(或其他任何您想用作错误模型的参数)。当发生错误时,用错误填充您的自定义exeception(让我们称之为RequestException),然后抛出它。错误将存储在您的exeception实例中。在RequestException的@ExceptionHandler中,从异常对象中获取错误,然后以方便的方式返回到前端。
我想我通过在异常中添加"@JsonAutoDetect"解决了这个问题。我使用的是Java Config,因此下面显示的内容将处理HTTP响应代码并返回JSON响应。
@JsonAutoDetect( fieldVisibility = Visibility.DEFAULT, getterVisibility = Visibility.DEFAULT, isGetterVisibility = Visibility.DEFAULT )
public class OfficeSetupException extends RuntimeException {
private List<String> errors;
private static final long serialVersionUID = -1L;
public OfficeSetupException( List<String> errors, String message ) {
super( message );
this.errors = errors;
}
public List<String> getErrors() {
return errors;
}
}
- javascript:如何在antlr生成的Lexer中进行错误处理
- 生成pdf或其他非html文件时的错误处理
- sailsjs在创建两个模型时的错误处理
- 为什么没有调用我的流错误处理程序
- 错误处理try&catch+回调
- EventSource中的错误处理
- Angular JS中的通用Ajax错误处理-拦截器
- Sinon Mocha节点错误处理
- 如果文件不存在,fs.watch是否有错误处理程序
- Javascript初始化和错误处理
- AngularJS错误处理:根据错误数组显示错误
- JQuery 图像错误处理
- BreezeJS中央错误处理程序和自动重试
- 文件下载html与错误处理FileNotFound
- 使用mongoose保存多个文档时的错误处理
- Node.js如何知道回调的第一个参数是否是错误处理程序
- Meteor JavaScript-错误处理
- AJAX自定义错误处理代码问题
- 正在获取丢失的错误处理程序套接字消息,无法读取属性'timerID'使用Node.js和Socket.i
- 为什么不't JavaScript库使用错误处理