jsf+hibernate+sqlserver乱码问题

lupingzhang 2007-06-13
前面的问题已经解决.
现在可以在jsf页面中通过hibernate读取sqlserver数据.但是现在有个问题是中文乱码
我用网上的解决办法:
1、自定义转器
package util;
import java.util.Map;
import javax.faces.component.UIComponent;
import javax.faces.convert.Converter;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;
public class StringConverter implements Converter {
public Object getAsObject(FacesContext context, UIComponent component,
   String newValues) throws ConverterException {
  String newstr =" " ;
  if (newValues == null) {
   newValues =" ";
  }
  byte[] byte1 = null;
  try {
   byte1 = newValues.getBytes("ISO-8859-1");
   newstr = new String(byte1, "GB2312");
   UIInput input=(UIInput)component;//
   input.setSubmittedValue(newstr);
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }

  return newstr;

}

public String getAsString(FacesContext context, UIComponent component,
   Object Values) throws ConverterException {
  return (String) Values;
}
}

2、注册转换器
faces-config.xml片段
<converter>
  <converter-id>stringconverter</converter-id>
  <converter-class>util.StringConverter</converter-class>
</converter>

3、在页面使用转换器
              <h:column>
<f:facet name="header">
<h:outputText value="#{msgs.projectname}" />
</f:facet>
<h:outputText value="#{results.projectname}"/>
<f:converter converterId="stringconverter"/>
</h:column>
<h:column>
但是运行时tomcate报错:
exception

org.apache.jasper.JasperException: javax.faces.component.UIColumn
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:322)
com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:147)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:117)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)


root cause

java.lang.ClassCastException: javax.faces.component.UIColumn
javax.faces.webapp.ConverterTag.doStartTag(ConverterTag.java:111)
org.apache.jsp.xiangmuxi_jsp._jspx_meth_f_converter_0(xiangmuxi_jsp.java:417)
org.apache.jsp.xiangmuxi_jsp._jspx_meth_h_column_2(xiangmuxi_jsp.java:343)
org.apache.jsp.xiangmuxi_jsp._jspx_meth_h_dataTable_0(xiangmuxi_jsp.java:153)
org.apache.jsp.xiangmuxi_jsp._jspx_meth_f_view_0(xiangmuxi_jsp.java:103)
org.apache.jsp.xiangmuxi_jsp._jspService(xiangmuxi_jsp.java:69)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:322)
com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:147)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:117)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)

应该怎样修改呢?
guooo 2008-10-13
写一个过滤器不是更好?
Global site tag (gtag.js) - Google Analytics