display tag 在显示数据库编码为非GBK时会出现乱码,现用两种方式解决
1.修改原码:
修改文件org.displaytag.model.column文件中以下行
public
Object getValue(boolean decorated) throws ObjectLookupException, DecoratorException
... {
Object object = null;
// a static value has been set?
if (this.cell.getStaticValue() != null)
...{
object = this.cell.getStaticValue();
}
else if (this.header.getBeanPropertyName() != null)
...{
// if a decorator has been set, and if decorator has a getter for the requested property only, check
// decorator
if (decorated
&& this.row.getParentTable().getTableDecorator() != null
&& this.row.getParentTable().getTableDecorator().hasGetterFor(this.header.getBeanPropertyName()))
...{
object = LookupUtil.getBeanProperty(this.row.getParentTable().getTableDecorator(), this.header
.getBeanPropertyName());
}
else
...{
// else check underlining object
object = LookupUtil.getBeanProperty(this.row.getObject(), this.header.getBeanPropertyName());
// 20070924,这里是将原码进行转换
/try ...{
if(object instanceof String) ...{
object = new String(((String)object).getBytes("ISO8859_1"),"GBK");
}
} catch (UnsupportedEncodingException e) ...{
object = LookupUtil.getBeanProperty(this.row.getObject(), this.header.getBeanPropertyName());
}
}
}
方法二
... {
Object object = null;
// a static value has been set?
if (this.cell.getStaticValue() != null)
...{
object = this.cell.getStaticValue();
}
else if (this.header.getBeanPropertyName() != null)
...{
// if a decorator has been set, and if decorator has a getter for the requested property only, check
// decorator
if (decorated
&& this.row.getParentTable().getTableDecorator() != null
&& this.row.getParentTable().getTableDecorator().hasGetterFor(this.header.getBeanPropertyName()))
...{
object = LookupUtil.getBeanProperty(this.row.getParentTable().getTableDecorator(), this.header
.getBeanPropertyName());
}
else
...{
// else check underlining object
object = LookupUtil.getBeanProperty(this.row.getObject(), this.header.getBeanPropertyName());
// 20070924,这里是将原码进行转换
/try ...{
if(object instanceof String) ...{
object = new String(((String)object).getBytes("ISO8859_1"),"GBK");
}
} catch (UnsupportedEncodingException e) ...{
object = LookupUtil.getBeanProperty(this.row.getObject(), this.header.getBeanPropertyName());
}
}
}
方法二
增加decorater,如下
package com.mycompany.common.displaytag.wrap;
import java.io.UnsupportedEncodingException;
import org.displaytag.decorator.ColumnDecorator;
import org.displaytag.exception.DecoratorException;
public class Encode2GB implements ColumnDecorator ... {
public final String decorate(Object columnValue) throws DecoratorException ...{
// TODO Auto-generated method stub
try ...{
return new String(((String)columnValue).getBytes("ISO8859_1"), "GBK");
} catch (UnsupportedEncodingException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
import java.io.UnsupportedEncodingException;
import org.displaytag.decorator.ColumnDecorator;
import org.displaytag.exception.DecoratorException;
public class Encode2GB implements ColumnDecorator ... {
public final String decorate(Object columnValue) throws DecoratorException ...{
// TODO Auto-generated method stub
try ...{
return new String(((String)columnValue).getBytes("ISO8859_1"), "GBK");
} catch (UnsupportedEncodingException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
关在页面中增加以下
<
display:table
name
="test"
>
< display:column property ="id" title ="ID" />
< display:column property ="name" group ="2" decorator ="com.mycompany.common.displaytag.wrap.Encode2GB" />
< display:column property ="email" />
< display:column property ="status" />
< display:column property ="description" title ="Comments" />
</ display:table >
< display:column property ="id" title ="ID" />
< display:column property ="name" group ="2" decorator ="com.mycompany.common.displaytag.wrap.Encode2GB" />
< display:column property ="email" />
< display:column property ="status" />
< display:column property ="description" title ="Comments" />
</ display:table >
这样就可以正确显示中文了
总结:display从数据库中取出时并不进行转换编码,所以,我们需要进行转换,一种方式是修改其原码;另一各是
增加一个decorator方式,但对于前者,危险系统比较大,后者较安全,也更合理。
ps: 如果增加一个标签:如 srcEncode=??? desEncode=???这样的话,可以不再编写deocoader方式,更方便一些,如有兴趣,读者不妨试试,我也会在近期推出我的研究。