实现目标:从页面上输入123456.789,页面回显成金额格式:123,456.789
JavaBean:
package
ch12;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public
class
User
...
{
private Double salary;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public Double getSalary() ...{
return salary;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setSalary(Double salary) ...{
this.salary = salary;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
Action:
package
ch12;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.Iterator;
import
java.util.List;
import
java.util.Map;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
com.opensymphony.xwork.ActionSupport;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public
class
TestConvert
extends
ActionSupport
...
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private User user;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public User getUser() ...{
return user;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setUser(User user) ...{
this.user = user;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String execute() throws Exception ...{
return SUCCESS;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
xwork.xml
<
action
name
="testConvert"
class
="ch12.TestConvert"
>
<
result
name
="success"
>
/ch12/TestConvert.jsp
</
result
>
</
action
>
自定义转化器:最重要的
这个转换器是对类定义的,为类的属性定义转换器,所以,命名方式为:ClassName-conversion.properties
内容为:salary=ch12.SalaryConvert 表示User类的salary属性使用ch12.SalaryConvert 这个类进行转换
需要注意的时,如果是页面输入和页面输出双向都用到salary属性,需要判断cls的类型,定义相应的转换规则
见代码注释
package
ch12;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
java.text.DecimalFormat;
import
java.util.Map;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
ognl.DefaultTypeConverter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public
class
SalaryConvert
extends
DefaultTypeConverter
...
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public Object convertValue(Map map, Object obj, Class cls) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if(cls==String.class)...{ //从action显示数据到页面
DecimalFormat fmt = new DecimalFormat("##,###,###,###,##0.00");
return fmt.format((Double)obj);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
}else if(cls==Double.class)...{ //从页面提交到action
return new Double(((String[])obj)[0]);
}
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
}
如果是从action到jsp,则进行数字格式转换
如果是从jsp到action,则把输入的String转换成Double,因为User类的salary属性是Double类型
运行jsp,输入123456.789,提交,页面显示结果为123,456.789