在 Wicket 上混合 Scala 和 Java

发布于 2024-09-30 12:30:28 字数 794 浏览 2 评论 0原文

我们使用 Wicket 作为我们的前端框架,到目前为止我们的应用程序是纯 Java 的。然而,我想开始在我们的一些新类中使用一些 Scala。旧代码将保留在 Java 中。

安装 maven-scala-plugin、scala-library、scalatest 依赖项并下载它们后,我不得不面对我不知道如何在 Wicket 中将 Scala 与 Java 混合的问题。到目前为止,我只在 Wicket 中看到过有关纯 Java 或纯 Scala 项目的教程。

在 WebApplication 的扩展类中,您有诸如此类的内容

public Class<? extends Page> getHomePage() {
    return HomePage.class;
}

,如果您使用 Scala,您将具有诸如此类的内容

def getHomePage = classOf[HomePage]

如果我有一个名为 HomePageScala.scala 的 Scala 类,我如何从 java 代码中调用它?

如何使用调用 Scala 类的 Java 代码创建 BookmarkablePageLink? 例如

add(new BookmarkablePageLink<HomePageScala>("homePageScala", HomePageScala.class));

,这实际上可能吗?还是我必须使用 100% java 或 100% scala?

预先非常感谢您!

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.

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.

In the extended class of WebApplication you have something such as

public Class<? extends Page> getHomePage() {
    return HomePage.class;
}

and if you are using Scala you would have something such as

def getHomePage = classOf[HomePage]

If I have a Scala class called HomePageScala.scala how can I call it from the java code?

How can I create a BookmarkablePageLink using Java code calling a Scala class?
e.g

add(new BookmarkablePageLink<HomePageScala>("homePageScala", HomePageScala.class));

Is this actually even possible or do I have to use either 100% java or 100% scala?

Thank you very much in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖风昔人 2024-10-07 12:30:28

可以在 Scala 和 Java 代码之间进行交叉引用。如果你使用maven,试试这个配置。使用 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>

我不确定,但认为没有 Maven,你应该尝试这个 scalac 参数:

-make:transitivenocp

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>

I'm not sure, but think that without maven you should to try this scalac argument:

-make:transitivenocp
无语# 2024-10-07 12:30:28

我会将项目分成多模块构建。

root
     -- scala  (jar)   
     -- java   (jar) <dependency><artifactId>scala</artifactId></dependency>
     -- webapp (war) <dependency><artifactId>java</artifactId></dependency>

这样 Java 代码就可以轻松引用 Scala 代码。
这两个 JAR 都将包含在 Web 应用程序中。

缺点:Java 可以引用 Scala 代码,但反之则不行(反之亦然)。

I would separate the project into a multi-module build.

root
     -- scala  (jar)   
     -- java   (jar) <dependency><artifactId>scala</artifactId></dependency>
     -- webapp (war) <dependency><artifactId>java</artifactId></dependency>

That way the Java code can easily reference the Scala code.
Both JARs would be included in the webapp.

The drawback: Java could reference Scala code, but not the other way around (or vice-versa).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文