如何使用 Eclipse + Kotlin + tomcat 开发 Dynamic Web Server

如何使用 Eclipse + Kotlin + tomcat 开发 Dynamic Web Server ?


Eclipse 安装 Kotlin 插件后是无法开发 Dynamic Web Server 的, 除非再新建一个 Kotlin 项目, 然后将 bin 目录添加到 tomcat 的 CLASSPATH 中

要想完全使用 Kotlin 开发难度是很大滴, 不过, 如果你不在意的话, 是可以以 Java 注解 + Kotlin 的方式完成的

也就是说, Dispatcher 是无法扫描 Kotlin 类的, 所有 URL 请求都需要 Java 代理转发到 Kotlin

配置 tomcat

添加 Kotlin 运行库

添加 sping 库, Kotlin 项目

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cont.ex.LoginCont;

@Controller
public class IndexCont {
    
    static {
        System.out.println(IndexCont.class.getName() + " 类被初始化.");
    }

    @RequestMapping("index")
    public String index(Model m) {
        m.addAttribute("info", test.JustFun.Companion.getString("欢迎信息"));
        return new LoginCont().login(m);
    }

}
package test

class JustFun {
    companion object {
        fun getString(param: String) : String {
            when(param) {
                "欢迎信息" -> return "Welcome to develon's house"
            }
            return "Haven't soutou suru data"
        }
    }
}

访问/index.html

注意: Kotlin 项目修改后都需要运行一下才会被编译

猜你喜欢

转载自www.cnblogs.com/develon/p/11456054.html
今日推荐