掲示板

Display an error message to the user from BaseModelListener subclass.

7年前 に Sean Megeath によって更新されました。

Display an error message to the user from BaseModelListener subclass.

New Member 投稿: 14 参加年月日: 15/03/23 最新の投稿
When any user is added or removed as a member of a site, I make a REST call to a non-Liferay site, in the class I have that extends BaseModelListener<User>. If that site is unavailable or the REST call fails for other reasons, I capture the exception and log an error message using slf4j. Now, I'm required to display a message to the user on the Liferay page (edit_site_assignments_users.jsp) and roll back the change on Liferay. It doesn't appear as though I can use SessionErrors to display to a liferay-ui:error tag, since my BaseModelListener<User> doesn't get a HttpServletRequest, HttpSession, PortletRequest, or PortletSession. Any suggestions on what I should do to present the problem to the user? Will I need to abandon using a model listener for this?
thumbnail
7年前 に David H Nebinger によって更新されました。

RE: Display an error message to the user from BaseModelListener subclass.

Liferay Legend 投稿: 14916 参加年月日: 06/09/02 最新の投稿
Throw an exception. It will trigger the rollback.

If your exception extends PortalException, the MVCPortlet the login portlet extends will inject this exception into SessionErrors automagically.

Therefore the only other thing you need is a JSP fragment to override the JSP to add the <liferay-ui:error /> to display the appropriate message.
7年前 に Sean Megeath によって更新されました。

RE: Display an error message to the user from BaseModelListener subclass.

New Member 投稿: 14 参加年月日: 15/03/23 最新の投稿
Thanks for the reply! Here's what I interpreted your response to mean:
I created a class that extended PortalException, and gave that as the "cause" to the ModelListenerException I throw in the onAfterAddAssociation of my class that extends BaseModelListener<User>. In the webapps\ROOT\html\portlet\sites_admin\edit_site_assignments_users.jsp, I added a page import tag for my class that extends PortalException, then added a liferay-ui:error tag:
<liferay-ui:error exception="<%= ExtendedPortalException.class %>" message="Problem adding user to site." />
When I went to add a user to a site, I just got an error message drawn where the site membership interface was, saying "Site Memberships is temporarily unavailable." Which is what I was getting when I was just throwing the ModelListenerException and not having the liferay-ui:error tag. I'm wondering if I'm missing something basic here. Does anything stand out to you as a problem?
thumbnail
7年前 に David H Nebinger によって更新されました。

RE: Display an error message to the user from BaseModelListener subclass.

Liferay Legend 投稿: 14916 参加年月日: 06/09/02 最新の投稿
Snap I wasn't thinking and was looking at login portlet, but I'm thinking the same rules should apply for the site memberships since it is also based on the Liferay MVCPorlet implementation.

You may need to create a command action override for the action servicing the request, it may need to dig into the causes to find if yours is in there and throw it manually.