在Wicket上混合Scala和Java [英] Mixing Scala and Java on Wicket
问题描述
我们使用Wicket作为我们的前端框架,到目前为止,我们的应用程序是纯Java.但是,我想在我们的一些新类上开始使用Scala.旧代码将保留在Java中.
We use Wicket as our front end framework and until now our application is pure Java. However, I would like to start using a bit of Scala on some of our new classes. The old code would stay in Java.
在安装了maven-scala-plugin,scala库,scalatest依赖项并下载它们之后,我不得不面对一个问题,即我不知道如何在Wicket中将Scala与Java混合使用.到目前为止,我只在Wicket中看到过有关纯Java或纯Scala项目的教程.
After installing the maven-scala-plugin, scala-library, scalatest dependencies and downloading them I had to face the problem that I do not know how to mix Scala with Java in Wicket. Until now I have only seen tutorials about either pure Java or pure Scala projects in Wicket.
在WebApplication的扩展类中,您拥有
In the extended class of WebApplication you have something such as
public Class<? extends Page> getHomePage() {
return HomePage.class;
}
如果您使用的是Scala,则可能会出现类似
and if you are using Scala you would have something such as
def getHomePage = classOf[HomePage]
如果我有一个名为HomePageScala.scala的Scala类,如何从Java代码中调用它?
If I have a Scala class called HomePageScala.scala how can I call it from the java code?
如何使用调用Scala类的Java代码创建BookmarkablePageLink? 例如
How can I create a BookmarkablePageLink using Java code calling a Scala class? e.g
add(new BookmarkablePageLink<HomePageScala>("homePageScala", HomePageScala.class));
这实际上是否可行,还是我必须使用100%java或100%scala?
Is this actually even possible or do I have to use either 100% java or 100% scala?
非常感谢您!
推荐答案
可以在Scala和Java代码之间进行交叉引用.如果您使用Maven,请尝试此配置.在Scala 2.8.0中进行了测试:
It's possible to make cross reference between Scala and Java code. If you use maven, try this config. Tested with Scala 2.8.0:
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.14.1</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<phase>compile</phase>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
<phase>test-compile</phase>
</execution>
<execution>
<phase>process-resources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
<args>
<arg>-target:jvm-1.5</arg>
<!-- to support mix java/scala only -->
<arg>-make:transitivenocp</arg>
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<compilerVersion>1.6</compilerVersion>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
我不确定,但是认为没有行家,您应该尝试以下scalac参数:
I'm not sure, but think that without maven you should to try this scalac argument:
-make:transitivenocp
这篇关于在Wicket上混合Scala和Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!