Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Chris Becker
Passing parameters in SessionMessages
May 29, 2014 8:31 AM
Answer

Chris Becker

Rank: Regular Member

Posts: 110

Join Date: June 11, 2010

Recent Posts

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:
1<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%>

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


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

1user.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:

 1showUpdateSuccessfulMessage(request,userAddEditDto);
 2
 3    private void showUpdateSuccessfulMessage(ActionRequest request,UserAddEditDto userAddEditDto) {
 4        String messageKey = MSG_KEY_USER_UPDATED_SUCCESSFULLY;
 5        if (userAddEditDto.getAddEditMode() == AddEditMode.NEW) {
 6            messageKey = MSG_KEY_USER_CREATED_SUCCESSFULLY;
 7        }
 8        showGlobalInfo(request, messageKey, userAddEditDto.getScreenName());
 9    }

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:
1private void showGlobalInfo(ActionRequest request,String messageId, Object messageParams ) {
2        SessionMessages.add(request, messageId, messageParams);
3   
4    }


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
Meera Prince
RE: Passing parameters in SessionMessages
May 29, 2014 9:06 AM
Answer

Meera Prince

Rank: Liferay Master

Posts: 862

Join Date: February 8, 2011

Recent Posts

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
Pankaj Kathiriya
RE: Passing parameters in SessionMessages
May 29, 2014 9:09 AM
Answer

Pankaj Kathiriya

Rank: Liferay Master

Posts: 666

Join Date: August 4, 2010

Recent Posts

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.
Chris Becker
RE: Passing parameters in SessionMessages
May 29, 2014 2:57 PM
Answer

Chris Becker

Rank: Regular Member

Posts: 110

Join Date: June 11, 2010

Recent Posts

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:
1public static void add(
2        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
Andew Jardine
RE: Passing parameters in SessionMessages
May 29, 2014 3:09 PM
Answer

Andew Jardine

Rank: Expert

Posts: 485

Join Date: December 22, 2010

Recent Posts

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
RE: Passing parameters in SessionMessages
October 9, 2014 8:26 PM
Answer

Jared Nagle

Rank: New Member

Posts: 1

Join Date: June 15, 2014

Recent Posts

according to the com.liferay.taglib.ui.MessageTag class in WEB-INF/lib/util-taglib.jar, the value from the key inserted with the

 1public static void add(HttpSession session, String key, Object value) {
 2    Map<String, Object> map = _getMap(session, true);
 3   
 4     if (map == null) {
 5       return;
 6     }
 7    
 8     map.put(key, value);
 9   }


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

 1
 2      if (this._arguments == null) {
 3        if (!this._localizeKey) {
 4          value = this._key;
 5        }
 6        else if (this._unicode) {
 7          value = UnicodeLanguageUtil.get(this.pageContext, this._key);
 8        }
 9        else {
10          value = LanguageUtil.get(this.pageContext, this._key);
11        }
12
13      }
14      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:

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


LanguageUtil.format(