displaytag 中文问题

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());
                }

            }

        }

方法二

 

增加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;
        }


    }


}

关在页面中增加以下

 

< 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从数据库中取出时并不进行转换编码,所以,我们需要进行转换,一种方式是修改其原码;另一各是

增加一个decorator方式,但对于前者,危险系统比较大,后者较安全,也更合理。

ps: 如果增加一个标签:如 srcEncode=??? desEncode=???这样的话,可以不再编写deocoader方式,更方便一些,如有兴趣,读者不妨试试,我也会在近期推出我的研究。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值