Vaadin 12将对象传递给JavaScript函数:无法编码类

时间:2019-04-03 08:26:45

标签: javascript kotlin vaadin12

Vadin 12,Kotlin项目

在我的myPage.html中,我使用了javascript:

 myObject.redirectToCheckout({
                    sessionId: "1111_2222",
                }).

因此,我需要从Vaadin 12中调用javaScript函数redirectToCheckout并传递正确的参数作为对象。 这是我的Vaadin代码段:

import com.vaadin.flow.component.dependency.HtmlImport
import com.vaadin.flow.component.dependency.JavaScript
import com.vaadin.flow.component.html.Div
import com.vaadin.flow.router.Route
import com.vaadin.flow.server.VaadinRequest
import java.io.Serializable

@Route(value = "redir")
@HtmlImport("styles/myPage.html")
class RedirectForm : Div() {
    init {
        val request = VaadinRequest.getCurrent()
        val paramGoto = request.getParameter("goto")
        val redirect = Redirect("$paramGoto")
        UI.getCurrent().getPage().executeJavaScript("myObject.redirectToCheckout($0)", redirect) // **error here**
    }

    inner class Redirect : Serializable {
        var sessionId: String

        constructor(sessionId: String) {
            this.sessionId = sessionId
        }
    }
}

但我收到错误消息:

Caused by: java.lang.IllegalArgumentException: Can't encode class com.myproject.view.RedirectForm$Redirect to json
    at com.vaadin.flow.internal.JsonCodec.encodeWithoutTypeInfo(JsonCodec.java:165)
    at com.vaadin.flow.internal.JsonCodec.encodeWithTypeInfo(JsonCodec.java:80)
    at com.vaadin.flow.component.page.Page.executeJavaScript(Page.java:338)
    at com.myproject.view.RedirectForm.<init>(RedirectView.kt:28)
    ... 50 common frames omitted

1 个答案:

答案 0 :(得分:2)

使用JsonObject代替Redirect

val request = VaadinRequest.getCurrent()
val paramGoto = request.getParameter("goto")
val json = Json.createObject()
json.put("sessionId", "$paramGoto")
UI.getCurrent().getPage().executeJavaScript("myObject.redirectToCheckout($0)", json)