Forums de discussion

Passing parameters in SessionMessages

thumbnail
Chris Becker, modifié il y a 9 années.

Passing parameters in SessionMessages

Regular Member Publications: 112 Date d'inscription: 11/06/10 Publications récentes
Hello all;

I am creating a portlet in Liferay 6.2 using Spring MVC portlet framework. I wish to provide a 'user updated successfully' message to a JSP page, and in doing so pass a parameter to the string to indicate which user was updated. So far, I have been successful in getting the message to display, but unsuccessful in getting the parameter in the message to display.

I will start from the front end, and work to the back. In my JSP, I have the following taglib declaration:
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%>

And in the body of the JSP, there is this tag:
<liferay-ui:success key="user.updated.successfully" message="user.updated.successfully" />


In my 118n.properties file is the following message declaration:

user.updated.successfully=User profile updated successfully for user {0}


In my controller class, where the SuccessMessage.add(... ) call is made, I have the following Key declaration:

/**
* Key for message that a user was successfully updated.
*/
public static final String MSG_KEY_USER_UPDATED_SUCCESSFULLY = "user.updated.successfully";

I then call a private method, which takes the PortletRequest and a UserDTO object as parameters:

showUpdateSuccessfulMessage(request,userAddEditDto);

	private void showUpdateSuccessfulMessage(ActionRequest request,UserAddEditDto userAddEditDto) {
		String messageKey = MSG_KEY_USER_UPDATED_SUCCESSFULLY;
		if (userAddEditDto.getAddEditMode() == AddEditMode.NEW) {
			messageKey = MSG_KEY_USER_CREATED_SUCCESSFULLY;
		}
		showGlobalInfo(request, messageKey, userAddEditDto.getScreenName());
	}

UserAddEditDto.getScreenName returns a String with the screenname of the user being updated.
This method calls yet another private method that adds the message and params to the SessionMessage object:
private void showGlobalInfo(ActionRequest request,String messageId, Object messageParams ) {
		SessionMessages.add(request, messageId, messageParams);
	
	}


I am able to successfully display the success message , but the parameter is not replaced in the string, so the result is
User profile updated successfully for user {0}.

Does anyone know how to populate placeholders in messages with values?

Any/all guidance appreciated... thanks!

--Chris
thumbnail
Meera Prince, modifié il y a 9 années.

RE: Passing parameters in SessionMessages

Liferay Legend Publications: 1111 Date d'inscription: 08/02/11 Publications récentes
Hi
try as follows

messages with variable values

property message

categories-count-message=Document found more than {0} {1}.

Display

<%= LanguageUtil.format(pageContext, "categories-count-message",new String[]{String.valueOf(categoryBasedCount),String.valueOf(CategoryName)})


Use LanguageUtil.format(--) and pass parameters in String array

Regards,
Meera Prince
thumbnail
Chris Becker, modifié il y a 9 années.

RE: Passing parameters in SessionMessages

Regular Member Publications: 112 Date d'inscription: 11/06/10 Publications récentes
Meera Prince:
Hi
try as follows

messages with variable values

property message

categories-count-message=Document found more than {0} {1}.

Display

<%= LanguageUtil.format(pageContext, "categories-count-message",new String[]{String.valueOf(categoryBasedCount),String.valueOf(CategoryName)})


Use LanguageUtil.format(--) and pass parameters in String array

Regards,
Meera Prince


Hi Meera;

Thanks for the reply!

I am looking to avoid the use of scriplets, however, and am instead looking for a way to populate the parameters using the SessionMessages.add method, with the following signature:
public static void add(
		PortletRequest portletRequest, String key, Object value)



I believe that the purpose of this value parameter is for just this purpose; however, I cannot get the parameter to populate in the message.

Regards,

--Chris
thumbnail
Andew Jardine, modifié il y a 9 années.

RE: Passing parameters in SessionMessages

Liferay Legend Publications: 2416 Date d'inscription: 22/12/10 Publications récentes
What about using the LanguageUtil on the server side to retrieve the string and populate it before you put it into the Session Messages?
Jared Nagle, modifié il y a 9 années.

RE: Passing parameters in SessionMessages

New Member Envoyer: 1 Date d'inscription: 16/06/14 Publications récentes
according to the com.liferay.taglib.ui.MessageTag class in WEB-INF/lib/util-taglib.jar, the value from the key inserted with the

public static void add(HttpSession session, String key, Object value) {
    Map<string, object> map = _getMap(session, true);
    
     if (map == null) {
       return;
     }
     
     map.put(key, value);
   }</string,>


method is the one obtained by the MessageTag class when rendering the html:


      if (this._arguments == null) {
        if (!this._localizeKey) {
          value = this._key;
        }
        else if (this._unicode) {
          value = UnicodeLanguageUtil.get(this.pageContext, this._key);
        }
        else {
          value = LanguageUtil.get(this.pageContext, this._key);
        }

      }
      else if ...


It is thus safe to format the value yourself in server code and pass it into this method as long as you do not provide arguments to the message tag in the jsp/jsf page.

so you should have a method call like this on the server side:


    // second param for the add method is the key of the message
    String formattedMessage = LanguageUtil.format(serviceContext.getLocale(), "the length of field {0} is too long, maximum length = {1}", Arrays.asList("firstName", 32));
    SessionMessages.add(serviceContext.getRequest(), "maximum-length-exceeded-for-field", formattedMessage);


LanguageUtil.format(
thumbnail
Pankaj Kathiriya, modifié il y a 9 années.

RE: Passing parameters in SessionMessages

Liferay Master Publications: 722 Date d'inscription: 05/08/10 Publications récentes
AFAIK there is one attribute named as 'arguments' for liferay-ui:success tag or liferay-ui:message tag. It is used to specify placeholder values.