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
thungrac02Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
objectsConnect With a Mentor Commented:
sorry that should be:

MessageResources messages = Resources.getResources(request);
0
 
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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
objectsCommented:
org.apache.struts.util.MessageResources
org.apache.struts.validator.Resources
0
 
thungrac02Author Commented:
It still cannot resolve Resource.
0
 
thungrac02Author Commented:
In a JSP file, we cannot get a string in ApplicationResources.properties file???
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
All Courses

From novice to tech pro — start learning today.