aspose file tools*
The moose likes Struts and the fly likes Pagecontext object in Action class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Pagecontext object in Action class" Watch "Pagecontext object in Action class" New topic
Author

Pagecontext object in Action class

Santosh Maskar
Ranch Hand

Joined: Jul 02, 2003
Posts: 226
In this following code I need Object of type PageContext in the following code to pass in the RequestUtil.message(), is there any way to get the pagecontext object,

public class MyAction extends Action
{
public ActionForward doExecute(ActionMapping mapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) {
//initilization code here

mstrName = RequestUtils.message(pageContextobj, resourceKey, null, "PrpName");

}

}
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The PageContext object is for use only within a JSP, so you can't get it in an Action class. However, the Action class that your Action extends has some methods that will accomplish the same thing as the RequestUtils.message() method does.

use this code instead:

mstrName = getResources(request).getMessage("PrpName");


Merrill
Consultant, Sima Solutions
Santosh Maskar
Ranch Hand

Joined: Jul 02, 2003
Posts: 226
Originally posted by Merrill Higginson:
However, the Action class that your Action extends has some methods that will accomplish the same thing as the RequestUtils.message() method does.

use this code instead:

mstrName = getResources(request).getMessage("PrpName");


this is returning the ???en_US.firstname??? instead of Firt Name
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I just tried the above code snippet again, and it retrieves the contents of the message for me. Are you 100% sure the message key PrpName exists in the ApplicationResources.properties file?

This code gets the message using the default locale. If you want to get the message using the locale of the current user, use this code:

String mstrName = getResources(request).getMessage(getLocale(request), "PrpName");

Both of the aboe code snippets retrieved the contents of the message when I tried them for a message key I know exists in my ApplicationResources.properties file.
Santosh Maskar
Ranch Hand

Joined: Jul 02, 2003
Posts: 226
Thank you Merrill

Originally posted by Merrill Higginson:

String mstrName = getResources(request).getMessage(getLocale(request), "PrpName");

Both of the aboe code snippets retrieved the contents of the message when I tried them for a message key I know exists in my ApplicationResources.properties file.


I think my resource bundle file is not loading
but when i use the Tag lib using the following call I can get the resource bundle file.
pageContext.setAttribute(resourceKey, resourceBundle, PageContext.APPLICATION_SCOPE);


Is there any way to set the resourcekey and bundlefile name so that I can get these two things in my action class.


I am thining If I can set the request.setAttribute()

Please comment!!!11
[ May 01, 2006: Message edited by: Santosh Maskar ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'm scratching my head as to why this doesn't work for you. Here's one more thing to try. The getResources method has another overloaded version that takes the HttpServletRequest object and a string indicating which resource bundle to use as parameters. Try this:

msg = getResources(request, "en_US").getMessage("key");
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Pagecontext object in Action class