Solved

How to getResources() in a JSP page?

Posted on 2004-04-12
12
1,351 Views
Last Modified: 2012-05-04
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
Comment
Question by:thungrac02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10810770
try:

MessageResources messages = Resource.getResources(request);
String message = messages.getMessage(locale,"important.message");
0
 

Author Comment

by:thungrac02
ID: 10811047
Hi objects!
What are libraries must be imported?
0
 
LVL 92

Expert Comment

by:objects
ID: 10811081
org.apache.struts.util.MessageResources
org.apache.struts.validator.Resources
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:thungrac02
ID: 10811255
It still cannot resolve Resource.
0
 

Author Comment

by:thungrac02
ID: 10811663
In a JSP file, we cannot get a string in ApplicationResources.properties file???
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 10812105
sorry that should be:

MessageResources messages = Resources.getResources(request);
0
 

Author Comment

by:thungrac02
ID: 10819498
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
 
LVL 92

Expert Comment

by:objects
ID: 10819529
> I found that if I want  Resources.getResources(request)  takes effect, my file must extends Action class

why?
0
 

Author Comment

by:thungrac02
ID: 10819976
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
 
LVL 92

Expert Comment

by:objects
ID: 10819998
> 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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
Article by: Justin
In light of the WannaCry ransomware attack that affected millions of Windows machines, you might wonder if your Mac needs protecting. Yes, it does and here is how to do it.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question