mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
WW-3833 adds new getFormatted method to support localization and conversion errors
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1364110 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -22,6 +22,7 @@ import com.opensymphony.xwork2.util.logging.Logger;
|
||||
import com.opensymphony.xwork2.util.logging.LoggerFactory;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
@@ -134,6 +135,25 @@ public class ActionSupport implements Action, Validateable, ValidationAware, Tex
|
||||
return getTextProvider().getText(key, defaultValue, args, stack);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dedicated method to support I10N and conversion errors
|
||||
*
|
||||
* @param key message which contains formatting string
|
||||
* @param expr that should be formatted
|
||||
* @return formatted expr with format specified by key
|
||||
*/
|
||||
public String getFormatted(String key, String expr) {
|
||||
Map<String, Object> conversionErrors = ActionContext.getContext().getConversionErrors();
|
||||
if (conversionErrors.containsKey(expr)) {
|
||||
String[] vals = (String[]) conversionErrors.get(expr);
|
||||
return vals[0];
|
||||
} else {
|
||||
final ValueStack valueStack = ActionContext.getContext().getValueStack();
|
||||
final Object val = valueStack.findValue(expr);
|
||||
return getText(key, Arrays.asList(val));
|
||||
}
|
||||
}
|
||||
|
||||
public ResourceBundle getTexts() {
|
||||
return getTextProvider().getTexts();
|
||||
}
|
||||
|
||||
@@ -313,8 +313,35 @@ public class ActionSupportTest extends XWorkTestCase {
|
||||
assertEquals(rb, mas.getTexts(MyActionSupport.class.getName()));
|
||||
}
|
||||
|
||||
public void testFormattingSupport() throws Exception {
|
||||
ActionContext.getContext().setLocale(new Locale("da"));
|
||||
MyActionSupport mas = new MyActionSupport();
|
||||
ActionContext.getContext().getValueStack().push(mas);
|
||||
|
||||
mas.setVal(234d);
|
||||
|
||||
String formatted = mas.getFormatted("format.number", "val");
|
||||
|
||||
assertEquals("234,0", formatted);
|
||||
}
|
||||
|
||||
public void testFormattingSupportWithConversionError() throws Exception {
|
||||
ActionContext.getContext().getConversionErrors().put("val", new String[]{"4567def"});
|
||||
ActionContext.getContext().setLocale(new Locale("da"));
|
||||
MyActionSupport mas = new MyActionSupport();
|
||||
ActionContext.getContext().getValueStack().push(mas);
|
||||
|
||||
mas.setVal(234d);
|
||||
|
||||
String formatted = mas.getFormatted("format.number", "val");
|
||||
|
||||
assertEquals("4567def", formatted);
|
||||
}
|
||||
|
||||
private class MyActionSupport extends ActionSupport {
|
||||
|
||||
private Double val;
|
||||
|
||||
@Override
|
||||
public String doDefault() throws Exception {
|
||||
return "santa";
|
||||
@@ -325,6 +352,14 @@ public class ActionSupportTest extends XWorkTestCase {
|
||||
super.validate(); // to have code coverage
|
||||
addActionMessage("validation was called");
|
||||
}
|
||||
|
||||
public Double getVal() {
|
||||
return val;
|
||||
}
|
||||
|
||||
public void setVal(Double val) {
|
||||
this.val = val;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
@@ -6,3 +6,5 @@
|
||||
hello=Hello World
|
||||
hello.0=Hello World {0}
|
||||
hello.1=Hello World. This is {0} speaking {1}
|
||||
|
||||
format.number = {0,number,#0.0##}
|
||||
|
||||
Reference in New Issue
Block a user