我尝试在显示标记标题键中使用应用程序资源属性。什么都没显示。请帮忙继续。我正在使用struts2。
答案 0 :(得分:5)
在display tag.properties中,我已经使用下面的代码解析了它
locale.provider=com.sample.web.resource.I18nStruts2Adapter
locale.resolver=com.sample.web.resource.I18nStruts2Adapter
package com.sample.web.resource;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.views.jsp.TagUtils;
import org.displaytag.Messages;
import org.displaytag.localization.I18nResourceProvider;
import org.displaytag.localization.LocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import java.util.Iterator;
import java.util.Locale;
public class I18nStruts2Adapter implements LocaleResolver, I18nResourceProvider {
/**
* prefix/suffix for missing entries.
*/
public static final String UNDEFINED_KEY = "???"; //$NON-NLS-1$
/**
* logger.
*/
private static Log log = LogFactory.getLog(I18nStruts2Adapter.class);
/**
* @see LocaleResolver#resolveLocale(javax.servlet.http.HttpServletRequest)
*/
public Locale resolveLocale(HttpServletRequest request) {
Locale result = null;
ValueStack stack = ActionContext.getContext().getValueStack();
Iterator iterator = stack.getRoot().iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
if (o instanceof LocaleProvider) {
LocaleProvider lp = (LocaleProvider) o;
result = lp.getLocale();
break;
}
}
if (result == null) {
log.debug("Missing LocalProvider actions, init locale to default");
result = Locale.getDefault();
}
return result;
}
/**
* @see I18nResourceProvider#getResource(String, String,
* javax.servlet.jsp.tagext.Tag , javax.servlet.jsp.PageContext)
*/
public String getResource(String resourceKey, String defaultValue, Tag tag,
PageContext pageContext) {
// if resourceKey isn't defined either, use defaultValue
String key = (resourceKey != null) ? resourceKey : defaultValue;
System.out.println("Key :"+key);
String message = null;
ValueStack stack = TagUtils.getStack(pageContext);
Iterator iterator = stack.getRoot().iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
if (o instanceof TextProvider) {
TextProvider tp = (TextProvider) o;
message = tp.getText(key);
break;
}
}
// if user explicitely added a titleKey we guess this is an error
if (message == null && resourceKey != null) {
log.debug(Messages
.getString("Localization.missingkey", resourceKey)); //$NON-NLS-1$
message = UNDEFINED_KEY + resourceKey + UNDEFINED_KEY;
}
return message;
}
}
答案 1 :(得分:1)
请显示一些代码并进行故障排除。
使用<s:text name="my.key"/>
确保您的标签在该页面上正确加载。如果显示my.key的值,则表示您已正确加载语言文件。
如果这样做,那么titleKey="my.key"
的{{1}}属性也应该有效。 titleKey也是案例感,它不是标题键。