Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1482
  • Last Modified:

How to getResources() in a JSP page?

In my JSP page, I want to get a text in ApplicationResources.properties file. The code is somehow likes this below:

Locale locale = (Locale) session.getAttribute(Action.LOCALE_KEY);
MessageResources messages = servlet.getResources();
String message = getMessage(locale,"important.message");

---> cannot resolve the object  servlet  and function  getMessage().

how to solve this problem?

Thanks in advance.
thungrac02
0
thungrac02
Asked:
thungrac02
  • 5
  • 5
1 Solution
 
objectsCommented:
try:

MessageResources messages = Resource.getResources(request);
String message = messages.getMessage(locale,"important.message");
0
 
thungrac02Author Commented:
Hi objects!
What are libraries must be imported?
0
 
objectsCommented:
org.apache.struts.util.MessageResources
org.apache.struts.validator.Resources
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
thungrac02Author Commented:
It still cannot resolve Resource.
0
 
thungrac02Author Commented:
In a JSP file, we cannot get a string in ApplicationResources.properties file???
0
 
objectsCommented:
sorry that should be:

MessageResources messages = Resources.getResources(request);
0
 
thungrac02Author Commented:
Hi objects!

I found that if I want  Resources.getResources(request)  takes effect, my file must extends Action class: public class TestAction extends Action {...}

so in a JSP file we cannot get a String from ApplicationResources.properties file, unfortunately.
0
 
objectsCommented:
> I found that if I want  Resources.getResources(request)  takes effect, my file must extends Action class

why?
0
 
thungrac02Author Commented:
Because getResources() is a method  of Class Action.
My solution is: create a .java file

import com.ibm.ett.util.struts.RecordAction;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.util.MessageResources;
import java.util.Locale;

public class Properties extends RecordAction {
    HttpServletRequest request = null;
    public String getMessage(String key) {
        MessageResources messages = getResources(request);
        String message = messages.getMessage(key);
        return message;
    }
    public String getMessage(Locale locale, String key) {
        MessageResources messages = getResources(request);
        String message = messages.getMessage(locale, key);
        return message;
    }
    public Properties(HttpServletRequest request){
        this.request = request;
    }
}

then in my JSP file, I can get a String from ApplicationResources.properties file:

      Properties properties = new Properties(request);
      String foreTitle = properties.getMessage("forTitle.reportB11");

and it works well.
0
 
objectsCommented:
> Because getResources() is a method  of Class Action.

It is but thats not the method I used above :)
I used the getResources() method in the Resouces class.
 
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now