我有一个简单的MVC控制器,我用我的自定义注释对其进行了注释:
@PreAuthorize("hasRole('GESTION_BENEFICIAIRE')")
@AuthentificationForte(otp = "#{args[0]}",transactionId="#{args[1]}")
@RequestMapping(value = "/ajouter", method = { RequestMethod.POST, RequestMethod.GET })
public String addBeneficiaire(@ModelAttribute("beneficiaireForm") BeneficiaireForm beneficiaireForm,
BindingResult result, Model model, Principal principal) {
[...]
}我的自定义注释与一个方面相关联,该方面在验证失败时抛出一个RuntimeException。
@Around(value = "@annotation(annotation)")
public Object verifyOtp(final ProceedingJoinPoint jointPoint,
final AuthentificationForte annotation) throws Throwable {
try {
if (authentificationForteEnabled) {
[...]
} else {
throw new AuthentificationForteException();
}
} else {
return jointPoint.proceed();
}
} finally {
}
}因此,现在的行为是,当验证失败时,我被重定向到一个500错误页面。我的目标是留在相同的页面上,并向BindingResult添加一个被拒绝的消息:
result.rejectValue("suiteRib", "BeneficiaireForm.InvalidRib");我还没有找到这样做的方法,我找到的唯一方法是更改我的所有逻辑,不使用注释,同时在控制器代码中使用带有try/catch的验证服务。
有没有办法处理这个问题,当方面抛出这个异常时,访问绑定结果并添加错误消息?
发布于 2014-01-22 11:15:48
绝对是这样。
这里有一个操作args的示例:Aspectj overwrite an argument of a method
您还可以将服务自动连接到aspect类中(请记住将其标记为@Configurable)。
如果您事先知道这些参数,那么我认为它们可以包含在point cut定义中,在这种情况下,它们可以直接在around方法中引用。这是非常好的,因为参数是强类型的。
你可以在这里阅读更多内容:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html
发布于 2015-08-14 04:41:58
回答你的问题可能太晚了,但有两种方法可以解决这个问题:
Spring JSON在你的方面中有一个关于Controller ()的try catch,当你得到运行时异常时,你可以从方面返回响应(就像一个显示错误原因的通用或者带有error message.)
我们目前有一个应用程序,我们混合使用这种方法来处理异常。
https://stackoverflow.com/questions/21266306
复制相似问题