我想在一个 Controller 中处理所有 portlet 异常。这个工具是 spring 使用@ControllerAdvice 提出的。
我想知道这个特性在 portlet 上下文中是否仍然可用和适用。
请注意,我对其进行了测试,但处理异常的方法不会触发。
提前致谢。
编辑 1
这是一些代码片段
SpringMvcPortlet.java
@Controller
@RequestMapping("VIEW")
public class SpringMvcPortlet {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringMvcPortlet.class);
@Autowired
private MyService myService;
@RenderMapping
public String view(final RenderRequest request, final RenderResponse response) {
return "view";
}
@RenderMapping(params = "action=renderOne")
public String renderOne(final RenderRequest request, final RenderResponse response) {
boolean result = myService.doSomething();
if (!result){
throw new InitException("CAN NOT INITIALIZE APP")
}
return "renderOne";
}
//Doing an Ajax call here
@ResourceMapping("initParams")
public void getInitParams(ResourceRequest request, ResourceResponse response) throws InitException{
final JSONObject initParams = constructJsonObject();
if (initParams == null){
throw new InitException("CAN NOT INITIALIZE APP")
}
try {
response.getWriter().write(initParams.toString());
} catch (IOException e) {
LOGGER.error("ERROR :: "+e)
}
}
}
ExceptionControllerAdvice.java
@ControllerAdvice("com.xxx.yyy.portlet")
@RequestMapping(value="/")
public class ExceptionControllerAdvice {
@ExceptionHandler(InitException.class)
public ModelAndView handleInitException(InitException e) {
ModelAndView mav = new ModelAndView("exception");
mav.addObject("name", e.getClass().getSimpleName());
mav.addObject("message", e.getMessage());
return mav;
}
}
这样处理
ExceptionControllerAdvice
中的异常的方法当服务返回 false 或执行 ajax 调用和 initParams == null
时不会触发.另外,当我把
handleInitException(InitException e)
在 portlet Controller 中并在 renderOne 中抛出一个 InitException 异常被处理和呈现 exception.jsp
看法。但是,在这种情况下,当在 ajax 调用 ( getInitParams
) 中抛出异常时,会执行异常处理程序方法,但不会呈现异常 View 。所以要继续,我不知道 ControllerAdvice 在 portlet 上下文中是否可用,或者我是否遗漏了什么。我也不明白为什么在 ajax 处理中,尽管执行了异常处理程序方法,但未呈现异常 View 。
最佳答案
看看这个blog :
Vitor Silva 描述了 Liferay 中使用 REST 的 Spring portlet 的优雅方法和使用 Extjs 的富客户端架构。 (搜索 ExceptionHandler)
如果您有雄心壮志,也可以阅读此 blog并制作一个 Spring Boot portlet:
关于spring-mvc - 处理 Spring MVC portlet 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35486326/