hi,
I want to use a html select with a few options.
Those options are specified in resourcebundles for different locales.
I have a CustomForm class:
class CustomForm
{
public String[] getOptionValues()
{
return new String[]{"resourcekey1","resourcekey2"}
}
public String[] getOptionLabels()
{
String[] asValues = getOptionValues();
String[] asLabels = new String[_asValues .length];
org.apache.struts.util.MessageResources messages = servlet.getResources();
for (int i = 0; i < asWaarden.length; i++)
{
asLabels[i] = messages.getMessage(asValues [i]);
}
return asLabels;
}
}
i use this in a JSP page:
<struts:select property="optionvalue">
<struts:options property="optionValues" labelProperty="optionLabels"/>
</struts:select>
This goes well for the default locale, but if i let the user changes it's locale then
everything changes with it except the options in the select!!
This is because the messages.getMessage(asValues [i]); call the
message.getMessage(Locale locale ,xxxx)
with the locale as a null property.
And when the MessageResources class gets a null locale it uses the default locale of
the server.
So when the server has a locale nl_NL and the users selects a different one: en_US
everything else that uses <struts:message xxxx/> changes but not the options.
Can i get the handle to the current session object in a ActionForm bean?
Why not set the Session object also so that we have much more power/data at our hands?
So why nog change the protected ActionForm processActionForm into this:
protected ActionForm processActionForm(ActionMapping mapping, HttpServletRequest
request)
{
// Is there a form bean associated with this mapping?
String attribute = mapping.getAttribute();
if (attribute == null)
return (null);
// Look up the existing form bean, if any
if (debug >= 1)
log(" Looking for ActionForm bean under attribute '" + attribute + "'");
ActionForm instance = null;
HttpSession session = null;
if ("request".equals(mapping.getScope())) {
instance = (ActionForm) request.getAttribute(attribute);
} else {
session = request.getSession();
instance = (ActionForm) session.getAttribute(attribute);
}
if (instance != null)
{
instance.setSession(session); //ADDED
return (instance);
}
// Create a new form bean if we need to
if (debug >= 1)
log(" Creating new ActionForm instance");
String name = mapping.getName();
String className = null;
ActionFormBean formBean = findFormBean(name);
if (formBean != null)
className = formBean.getType();
if (className != null) {
try {
Class clazz = Class.forName(className);
instance = (ActionForm) clazz.newInstance();
instance.setServlet(this);
} catch (Throwable t) {
log("Error creating ActionForm instance of class '" +
className + "'", t);
}
}
if (instance == null)
return (null);
// Store the newly created bean in the appropriate scope
if (debug >= 1)
log(" Storing instance under attribute '" +
attribute + "'");
if ("request".equals(mapping.getScope()))
request.setAttribute(attribute, instance);
else
session.setAttribute(attribute, instance);
instance.setSession(session); //ADDED
return (instance);
}
Johan Compagner