Scala 类型与泛型不匹配
Scala type mismatch with generics
我是 Scala 的新手,我正在尝试将一些 JSF 控制器转换为 Scala 以进行一些测试。
代码如下:
abstract class BaseListBean[TENTITY <: AnyRef, TID <: AnyRef] extends Serializable {
@BeanProperty var current:TENTITY = _
@BeanProperty var lazyDataModel: LazyDataModel[TENTITY] = _
def initLazyDataModel(): LazyDataModel[TENTITY] = {
val model:LazyDataModel[TENTITY] = new LazyDataModel[TENTITY]() {
override def load(first: Int, pageSize: Int, sortField: String, sortOrder: SortOrder, filters: util.Map[String, AnyRef]): util.List[TENTITY] = {
val paginationInfo:PaginationInfo = new PaginationInfo()
paginationInfo.setFromRecord(first)
paginationInfo.setPageSize(pageSize)
paginationInfo.setSortBy(Array(sortField))
paginationInfo.setDirections(if (SortOrder.ASCENDING == sortOrder) Array(Direction.Asc) else Array(Direction.Desc))
val advisoryPaginatedResult:PaginatedResult[TENTITY] = getBaseCRUD.read(paginationInfo)
setRowCount(advisoryPaginatedResult.getTotalItems.toInt)
advisoryPaginatedResult getItems
}
override def getRowKey(obj: TENTITY): TID = getEntityKey(obj)
}
return model;
}
def init() = {
setLazyDataModel(initLazyDataModel())
}
def getEntityKey(obj: TENTITY) : TID
def getBaseCRUD() : BaseCRUD[TENTITY, TID]
def customInit() = {
}
}
编译时的错误是:
Error:(24, 34) type mismatch;
found : org.primefaces.model.org.primefaces.model.LazyDataModel[TENTITY]
required: org.primefaces.model.org.primefaces.model.LazyDataModel[TENTITY]
val model:LazyDataModel[TENTITY] = new LazyDataModel[TENTITY]() {
我正在寻找解决方案,但问题仍然存在。我尝试用这样更简单的代码重现:
abstract class ExampleGenerics[T <: AnyRef, V <: AnyRef] {
@BeanProperty var list:util.AbstractMap[T, V] = _
def initList() : util.AbstractMap[T, V] = {
val model:util.AbstractMap[T, V] = new util.AbstractMap[T, V]() {
override def entrySet(): util.Set[Entry[T, V]] = {
return null;
}
}
return model
}
def init() = {
setList(initList());
}
}
但这行得通。有人可以帮忙吗?谢谢
我发现了问题:
这是LazyDataModel<T>
的头像:
public abstract class LazyDataModel<T> extends DataModel<T> implements SelectableDataModel<T>, Serializable { /*...*/ }
class继承自DataModel<T>
。
在我的 pom.xml 我有
<dependency>
<groupId>javax.faces</groupId>
<artifactId>javax.faces-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
还有
<dependency>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
<scope>provided</scope>
</dependency>
class DataModel<T>
在 jsf 2 包中,但在 javaee-api 中也有 DataModel
class(没有泛型)。所以编译器出错了。
解决办法是修改pom,添加如下依赖:
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>ejb-api</artifactId>
<version>3.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>javax.faces-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
我无法使用 jee6 api 依赖项,因为我使用的是兼容 jee5 的应用服务器并升级了 JSF2.0。
我是 Scala 的新手,我正在尝试将一些 JSF 控制器转换为 Scala 以进行一些测试。 代码如下:
abstract class BaseListBean[TENTITY <: AnyRef, TID <: AnyRef] extends Serializable {
@BeanProperty var current:TENTITY = _
@BeanProperty var lazyDataModel: LazyDataModel[TENTITY] = _
def initLazyDataModel(): LazyDataModel[TENTITY] = {
val model:LazyDataModel[TENTITY] = new LazyDataModel[TENTITY]() {
override def load(first: Int, pageSize: Int, sortField: String, sortOrder: SortOrder, filters: util.Map[String, AnyRef]): util.List[TENTITY] = {
val paginationInfo:PaginationInfo = new PaginationInfo()
paginationInfo.setFromRecord(first)
paginationInfo.setPageSize(pageSize)
paginationInfo.setSortBy(Array(sortField))
paginationInfo.setDirections(if (SortOrder.ASCENDING == sortOrder) Array(Direction.Asc) else Array(Direction.Desc))
val advisoryPaginatedResult:PaginatedResult[TENTITY] = getBaseCRUD.read(paginationInfo)
setRowCount(advisoryPaginatedResult.getTotalItems.toInt)
advisoryPaginatedResult getItems
}
override def getRowKey(obj: TENTITY): TID = getEntityKey(obj)
}
return model;
}
def init() = {
setLazyDataModel(initLazyDataModel())
}
def getEntityKey(obj: TENTITY) : TID
def getBaseCRUD() : BaseCRUD[TENTITY, TID]
def customInit() = {
}
}
编译时的错误是:
Error:(24, 34) type mismatch;
found : org.primefaces.model.org.primefaces.model.LazyDataModel[TENTITY]
required: org.primefaces.model.org.primefaces.model.LazyDataModel[TENTITY]
val model:LazyDataModel[TENTITY] = new LazyDataModel[TENTITY]() {
我正在寻找解决方案,但问题仍然存在。我尝试用这样更简单的代码重现:
abstract class ExampleGenerics[T <: AnyRef, V <: AnyRef] {
@BeanProperty var list:util.AbstractMap[T, V] = _
def initList() : util.AbstractMap[T, V] = {
val model:util.AbstractMap[T, V] = new util.AbstractMap[T, V]() {
override def entrySet(): util.Set[Entry[T, V]] = {
return null;
}
}
return model
}
def init() = {
setList(initList());
}
}
但这行得通。有人可以帮忙吗?谢谢
我发现了问题:
这是LazyDataModel<T>
的头像:
public abstract class LazyDataModel<T> extends DataModel<T> implements SelectableDataModel<T>, Serializable { /*...*/ }
class继承自DataModel<T>
。
在我的 pom.xml 我有
<dependency>
<groupId>javax.faces</groupId>
<artifactId>javax.faces-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
还有
<dependency>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
<scope>provided</scope>
</dependency>
class DataModel<T>
在 jsf 2 包中,但在 javaee-api 中也有 DataModel
class(没有泛型)。所以编译器出错了。
解决办法是修改pom,添加如下依赖:
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>ejb-api</artifactId>
<version>3.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>javax.faces-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
我无法使用 jee6 api 依赖项,因为我使用的是兼容 jee5 的应用服务器并升级了 JSF2.0。