4
votes

I would like to handle all portlet exceptions in one controller. This facility is proposed by spring using @ControllerAdvice.

I wonder if this feature still availabe and applicable in portlet context.

Note that I test it but the method handling the exception does not fire.

thanks in advance.

EDIT 1 Here is some code snippets

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;
}
}

This way the method handling the exception in ExceptionControllerAdvice is not fired when the service returns false or when doing an ajax call and initParams == null.

In addition, when I put handleInitException(InitException e) in portlet controller and throw an InitException in renderOne the exception is handled and rendering exception.jsp view. However, in this case when an exception is thrown in ajax call (getInitParams) the exception handler method is executed but not rendering the exception view.

So to resume, I do not know if ControllerAdvice is available in portlet context or if I am missing something. I do not understand also why in ajax processing the exception view is not rendered although the exception handler method was executed.

1
Share some code please: What have you tried yet, what's expected, how does it not work?Olaf Kock
@OlafKock I've updated the post with some code snippets...Thanks for your timejavaxiss
For Ajax processing (client side) you'll need to interpret the result yourself- are you doing this? Liferay will not render anything, just send back the result. And it's up to your client side Javascript code to do something, e.g. update the DOM. If you don't do this, there's no automatic behaviourOlaf Kock

1 Answers

0
votes

Take a look at this blog:

Where Vitor Silva describes an elegant approach to Spring portlets in Liferay with REST and an a rich client architecture using Extjs. (search for ExceptionHandler)

If you are feeling ambitious you can also read this blog and make a Spring Boot portlet: