Fórumok
Passing parameters in SessionMessages
Chris Becker, módosítva 9 év-val korábban
Passing parameters in SessionMessages
Regular Member Bejegyzések: 112 Csatlakozás dátuma: 2010.06.11. Legújabb bejegyzések
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:
And in the body of the JSP, there is this tag:
In my 118n.properties file is the following message declaration:
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:
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:
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
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
Meera Prince, módosítva 9 év-val korábban
RE: Passing parameters in SessionMessages
Liferay Legend Bejegyzések: 1111 Csatlakozás dátuma: 2011.02.08. Legújabb bejegyzések
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
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
Chris Becker, módosítva 9 év-val korábban
RE: Passing parameters in SessionMessages
Regular Member Bejegyzések: 112 Csatlakozás dátuma: 2010.06.11. Legújabb bejegyzésekMeera 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
Andew Jardine, módosítva 9 év-val korábban
RE: Passing parameters in SessionMessages
Liferay Legend Bejegyzések: 2416 Csatlakozás dátuma: 2010.12.22. Legújabb bejegyzések
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, módosítva 9 év-val korábban
RE: Passing parameters in SessionMessages
New Member Bejegyzés: 1 Csatlakozás dátuma: 2014.06.16. Legújabb bejegyzések
according to the com.liferay.taglib.ui.MessageTag class in WEB-INF/lib/util-taglib.jar, the value from the key inserted with the
method is the one obtained by the MessageTag class when rendering the html:
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:
LanguageUtil.format(
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(
Pankaj Kathiriya, módosítva 9 év-val korábban
RE: Passing parameters in SessionMessages
Liferay Master Bejegyzések: 722 Csatlakozás dátuma: 2010.08.05. Legújabb bejegyzések
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.