我正在尝试为TextField实现自定义转换器,以将其转换为自定义数据类型(CellElement)。我正在按照Book of Vaadin中的教程进行操作。
当我在TextField上调用setPropertyDataSource()
时,调试器将抛出java.lang.ClassCastException: java.lang.String cannot be cast to com.example.demo.CellElement
board = new Board();//this initializes all the cells
// connect the tile to the display
for( int col = 0; col < 9; col++ )
for( int row = 0; row < 9; row++ )
{
TextField cell_text = new TextField();
cell_text.setConverter(new StringToCellElementConverter());
Property temp_ce = board.getCellElement(col, row);
//error on next line
cell_text.setPropertyDataSource(temp_ce);
grid.addComponent( cell_text, col, row );
}
我认为在实现
stringToCellElementConverter
类时犯了一个错误,但是我无法确定是什么。我遵循了调试器步骤,并在AbstractField
:内的这段代码中引发错误:@Override
public void setPropertyDataSource(Property newDataSource) {
...
// Gets the value from source. This requires that a valid converter has
// been set.
try {
if (dataSource != null) {
//next line throws error!
T fieldValue = convertFromModel(getDataSourceValue());
setInternalValue(fieldValue);
}
setModified(false);
if (getCurrentBufferedSourceException() != null) {
setCurrentBufferedSourceException(null);
}
} catch (final Throwable e) {
setCurrentBufferedSourceException(new Buffered.SourceException(
this, e));
setModified(true);
throw getCurrentBufferedSourceException();
}
这是自定义Converter的代码:
public class StringToCellElementConverter implements Converter<String, CellElement> {
private static final long serialVersionUID = -3138155389689170574L;
@Override
public CellElement convertToModel(String value,
Class<? extends CellElement> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
return new CellElement(Integer.parseInt(value), true);
}
@Override
public String convertToPresentation(CellElement value,
Class<? extends String> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
String ret_val = value.getValue();
return ret_val;
}
@Override
public Class<CellElement> getModelType() {
return CellElement.class;
}
@Override
public Class<String> getPresentationType() {
return String.class;
}
}
CellElement的代码。我可能在此类中遇到一些配置问题。它最初不是为此目的而设计的:
public class CellElement implements Property
{
private static final long serialVersionUID = -6205569838194226408L;
private Integer value;
private boolean readOnly;
private boolean selected;
public CellElement( Integer value, boolean readOnly )
{
this.value = value;
this.readOnly = readOnly;
this.selected = false;
}
public boolean isSelected()
{
return selected;
}
public void setSelected( boolean selected )
{
this.selected = selected;
}
public Integer getIntegerValue()
{
return value;
}
public int getIntValue()
{
return value.intValue();
}
@Override
public String getValue()
{
return value.toString();
}
@Override
public void setValue(Object newValue)
throws ReadOnlyException, ConversionException {
if (readOnly)
throw new ReadOnlyException();
// Already the same type as the internal representation
if (newValue instanceof Integer)
value = (Integer) newValue;
// Conversion from a string is required
else if (newValue instanceof String)
try {
value = Integer.parseInt((String) newValue);
} catch (NumberFormatException e) {
throw new ConversionException();
}
else
// Don't know how to convert any other types
throw new ConversionException();
}
@Override
public Class getType() {
return this.getClass();
}
@Override
public boolean isReadOnly() {
return this.readOnly;
}
@Override
public void setReadOnly(boolean newStatus) {
this.readOnly = newStatus;
}
}
这是堆栈跟踪::
com.vaadin.data.Buffered$SourceException
at com.vaadin.ui.AbstractField.setPropertyDataSource(AbstractField.java:637)
at com.example.demo.Sudoku.init(Sudoku.java:134)
at com.vaadin.ui.UI.doInit(UI.java:639)
at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:222)
at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:74)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1402)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:305)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to com.example.demo.CellElement
at com.example.demo.StringToCellElementConverter.convertToPresentation(StringToCellElementConverter.java:1)
at com.vaadin.data.util.converter.ConverterUtil.convertFromModel(ConverterUtil.java:94)
at com.vaadin.ui.AbstractField.convertFromModel(AbstractField.java:708)
at com.vaadin.ui.AbstractField.convertFromModel(AbstractField.java:693)
at com.vaadin.ui.AbstractField.setPropertyDataSource(AbstractField.java:629)
... 28 more
Nov 21, 2014 4:01:31 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.example.demo.Sudoku$Servlet] in context with path [/VaadinUltimate] threw exception [com.vaadin.server.ServiceException: com.vaadin.data.Buffered$SourceException] with root cause
java.lang.ClassCastException: java.lang.String cannot be cast to com.example.demo.CellElement
at com.example.demo.StringToCellElementConverter.convertToPresentation(StringToCellElementConverter.java:1)
at com.vaadin.data.util.converter.ConverterUtil.convertFromModel(ConverterUtil.java:94)
at com.vaadin.ui.AbstractField.convertFromModel(AbstractField.java:708)
at com.vaadin.ui.AbstractField.convertFromModel(AbstractField.java:693)
at com.vaadin.ui.AbstractField.setPropertyDataSource(AbstractField.java:629)
at com.example.demo.Sudoku.init(Sudoku.java:134)
at com.vaadin.ui.UI.doInit(UI.java:639)
at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:222)
at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:74)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1402)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:305)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
最佳答案
一切似乎还好。由于您没有提供堆栈跟踪,所以我最好的选择是转换器中的异常处理。
@Override
public CellElement convertToModel(String value,
Class<? extends CellElement> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
return new CellElement(Integer.parseInt(value), true);
}
@Override
public String convertToPresentation(CellElement value,
Class<? extends String> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
String ret_val = value.getValue();
return ret_val;
}
如果
Integer.parseInt
或value.getValue
失败,这两种方法都将引发异常。尝试根据您的企业登录信息添加尝试/捕获流程
try {
return new CellElement(Integer.parseInt(value), true);
} catch (Exception e) {
//value is null or NaN
return new CellElement(0, true);
}
对于第一个或
if (value == null) {
return "";
} else {
return value.getValue();
}
第二。
问候