added method to get I18nMessages from servlet request

This commit is contained in:
Sebastian Sdorra
2014-03-16 10:29:30 +01:00
parent d9579c3473
commit e59eb6a458

View File

@@ -43,6 +43,8 @@ import java.lang.reflect.Field;
import java.util.Locale; import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
/** /**
* The I18nMessages class instantiates a class and initializes all {@link String} * The I18nMessages class instantiates a class and initializes all {@link String}
* fields with values from a resource bundle. The resource bundle must have the * fields with values from a resource bundle. The resource bundle must have the
@@ -85,6 +87,22 @@ public final class I18nMessages
return get(msgClass, Locale.ENGLISH); return get(msgClass, Locale.ENGLISH);
} }
/**
* Same as {@link #get(java.lang.Class, java.util.Locale)}, with locale
* from servlet request ({@link HttpServletRequest#getLocale()}).
*
*
* @param msgClass message class
* @param request servlet request
* @param <T> type of message class
*
* @return
*/
public static <T> T get(Class<T> msgClass, HttpServletRequest request)
{
return get(msgClass, request.getLocale());
}
/** /**
* Returns a instance of the given message class with all message fields * Returns a instance of the given message class with all message fields
* initialized. * initialized.
@@ -156,7 +174,7 @@ public final class I18nMessages
Class msgClass, Object instance) Class msgClass, Object instance)
throws IllegalArgumentException, IllegalAccessException throws IllegalArgumentException, IllegalAccessException
{ {
for (Field field : msgClass.getFields()) for (Field field : msgClass.getDeclaredFields())
{ {
if (field.getType().isAssignableFrom(String.class)) if (field.getType().isAssignableFrom(String.class))
{ {