mirror of
https://github.com/apache/struts.git
synced 2026-08-31 19:35:40 +00:00
Added useful WARNs to the TextProviderHelper when a message resource is not found and the default value is
used/evaluated WW-2592 git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@647647 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -83,8 +83,8 @@ public class Label extends UIBean {
|
||||
if (value != null) {
|
||||
addParameter("nameValue", findString(value));
|
||||
} else if (key != null) {
|
||||
// get the label from a TextProvider
|
||||
String providedLabel = TextProviderHelper.getText(key, "", stack);
|
||||
// get the label from a TextProvider (default value is the key)
|
||||
String providedLabel = TextProviderHelper.getText(key, key, stack);
|
||||
addParameter("nameValue", providedLabel);
|
||||
} else if (name != null) {
|
||||
String expr = name;
|
||||
|
||||
@@ -626,8 +626,8 @@ public abstract class UIBean extends Component {
|
||||
}
|
||||
|
||||
if(this.label == null) {
|
||||
// lookup the label from a TextProvider
|
||||
providedLabel = TextProviderHelper.getText(key, "", stack);
|
||||
// lookup the label from a TextProvider (default value is the key)
|
||||
providedLabel = TextProviderHelper.getText(key, key, stack);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package org.apache.struts2.util;
|
||||
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import com.opensymphony.xwork2.util.logging.Logger;
|
||||
import com.opensymphony.xwork2.util.logging.LoggerFactory;
|
||||
import com.opensymphony.xwork2.TextProvider;
|
||||
|
||||
import java.util.Iterator;
|
||||
@@ -12,6 +14,8 @@ import java.util.LinkedList;
|
||||
*/
|
||||
public class TextProviderHelper {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(TextProviderHelper.class);
|
||||
|
||||
/**
|
||||
* <p>Get a message from the first TextProvider encountered in the stack.
|
||||
* If the first TextProvider doesn't provide the message the default message is returned.</p>
|
||||
@@ -19,25 +23,48 @@ public class TextProviderHelper {
|
||||
* <p>This method was refactored from {@link org.apache.struts2.components.Text} to use a
|
||||
* consistent implementation across UIBean components.</p>
|
||||
* @param key the message key in the resource bundle
|
||||
* @param defaultMessage the message to return if not found
|
||||
* @param defaultMessage the message to return if not found (evaluated for OGNL)
|
||||
* @param args an array args to be used in a {@link java.text.MessageFormat} message
|
||||
* @param stack the value stack to use for finding the text
|
||||
*
|
||||
* @return the message if found, otherwise the defaultMessage
|
||||
*/
|
||||
public static String getText(String key, String defaultMessage, List<String> args, ValueStack stack) {
|
||||
String msg = defaultMessage;
|
||||
String msg = null;
|
||||
TextProvider tp = null;
|
||||
|
||||
for (Iterator iterator = stack.getRoot().iterator(); iterator.hasNext();) {
|
||||
Object o = iterator.next();
|
||||
|
||||
if (o instanceof TextProvider) {
|
||||
TextProvider tp = (TextProvider) o;
|
||||
msg = tp.getText(key, defaultMessage, args, stack);
|
||||
tp = (TextProvider) o;
|
||||
msg = tp.getText(key, null, args, stack);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (msg == null) {
|
||||
// evaluate the defaultMesage as an OGNL expression
|
||||
msg = stack.findString(defaultMessage);
|
||||
if (msg == null) {
|
||||
// use the defaultMessage literal value
|
||||
msg = defaultMessage;
|
||||
}
|
||||
|
||||
if (LOG.isWarnEnabled()) {
|
||||
if (tp != null) {
|
||||
LOG.warn("The first TextProvider in the ValueStack ("+tp.getClass().getName()+") could not locate the message resource with key '"+key+"'");
|
||||
} else {
|
||||
LOG.warn("Could not locate the message resource '"+key+"' as there is no TextProvider in the ValueStack.");
|
||||
}
|
||||
if (msg.equals(defaultMessage)) {
|
||||
LOG.warn("The default value expression '"+defaultMessage+"' was evaluated and did not match a property. The literal value '"+defaultMessage+"' will be used.");
|
||||
} else {
|
||||
LOG.warn("The default value expression '"+defaultMessage+"' evaluated to '"+msg+"'");
|
||||
}
|
||||
}
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user