需要布尔转换为Struts2的是,否,空白值 [英] Need Boolean converter Struts2 for Yes, No, Blank Values
问题描述
我需要根据UI表单元素上选定的值来保存数据。
< S:选择关键=invoice.productSold列表=$ {} productSold/>
productSold值下拉列表中选择(空白),是,否。
我BooleanConverter
公共类BooleanConverter扩展StrutsTypeConverter {@覆盖
/ *从形式* /
公共对象convertFromString(地图背景下,的String []值,类toClass){
字符串值=值[0];
如果(价值==){
的System.out.println(空);
返回null;
}
如果(否.equalsIgnoreCase(值)||0.equals(值)){
返回假;
}否则如果(是.equalsIgnoreCase(值)||1.equals(值)){
返回真;
}其他{
返回null;
}
}@覆盖
公共字符串convertToString(地图背景,对象o){
布尔值=(布尔)O;
返回将String.valueOf(值);
}}公共类发票{布尔productSold;
<吸气>
<二传手>
}
问题
- 我的状态下拉菜单中选择我为'是'和更新;更新后我的下拉没有显示是。它显示为空白。
- 当选择是的形式和方法ConverToString返回ConvertFromString方法返回真假。
Action类
公共UpdateAction扩展ActionSupport的{
保护列表与LT;弦乐> productSold;
公开名单<串GT; getProductSold(){
清单<串GT; myOptions =新的ArrayList<串GT;();
myOptions.add();
myOptions.add(是);
myOptions.add(否); 返回myOptions;
}
您不需要一个自定义的布尔转换器,使用Struts2的国际化功能获取的布尔值'好'的文本。
把真=是
和假=否
您的邮件属性,然后使用 listValue 选择>
标签调用的getText
方法来获得消息<$:S; code>的&LT属性C $ C>真正
和假
。
&LT; S:选择关键=invoice.productSold列表=productSoldlistValue =%的gettext(上)}/&GT;
BTW,你不需要使用内部列表
属性得到值栈值的任何特殊符号。
BTW之二:你并不需要这个 productSold
方法你的动作在里面,而不是你可以定义你是/使用OGNL记法没有列出任何列表右侧的JSP {...}
。
&LT; S:选择关键=invoice.productSold列表='{,真,假}'listValue =%的gettext(上)}/&GT;
I need to save data based on value selected on UI form element
<s:select key="invoice.productSold" list="${productSold}" />
productSold dropdown list of values ''(blank), 'Yes', 'No'.
My BooleanConverter
public class BooleanConverter extends StrutsTypeConverter {
@Override
/*From form*/
public Object convertFromString(Map context, String[] values, Class toClass) {
String value = values[0];
if (value == "") {
System.out.println("null");
return null;
}
if ("No".equalsIgnoreCase(value) || "0".equals(value)) {
return "false";
} else if ("Yes".equalsIgnoreCase(value) || "1".equals(value)) {
return "true";
} else {
return null;
}
}
@Override
public String convertToString(Map context, Object o) {
Boolean value = (Boolean) o;
return String.valueOf(value);
}
}
public class Invoice{
Boolean productSold;
<getter>
<setter>
}
Issues
- My form dropdown am selecting to 'Yes' and updating; after update my dropdown is not showing "Yes". It displays blank.
- ConvertFromString method returning "true" when Selecting "Yes" on form and ConverToString method returning "false".
Action class
public UpdateAction extends ActionSupport{
protected List<String> productSold;
public List<String> getProductSold() {
List<String> myOptions= new ArrayList<String>();
myOptions.add("");
myOptions.add("Yes");
myOptions.add("No");
return myOptions;
}
You do not need a custom boolean converter, use Struts2 internationalization feature for getting 'nice' texts for booleans.
Put true = Yes
and false = No
to your messages properties and then use listValue
attribute of <s:select>
tag to call getText
method to get messages for true
and false
.
<s:select key="invoice.productSold" list="productSold" listValue="%{getText(top)}"/>
BTW you do not need to use any "special" notations inside list
attribute to get value from the value stack.
BTW no.2: You do not need this productSold
method inside your action, instead you can define your yes/no list right in JSP using OGNL notation for lists {...}
.
<s:select key="invoice.productSold" list='{"", true, false}' listValue="%{getText(top)}"/>
这篇关于需要布尔转换为Struts2的是,否,空白值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!