ktorio / ktor

Framework for quickly creating connected applications in Kotlin with minimal effort
https://ktor.io
Apache License 2.0
13.06k stars 1.07k forks source link

html dsl form can not find action url #923

Open youngxhui opened 5 years ago

youngxhui commented 5 years ago

Ktor Version

1.1.2

Ktor Engine Used(client or server and name)

netty

JVM Version, Operating System and Relevant Context

jvm 1.8 os windows 10 kotlin 1.3.20

Feedback

I use html dsl create form, but it can not find the action url this is code

Application.kt

@KtorExperimentalLocationsAPI
@Location("/user")
class User {
    @Location("/login")
    data class UserLogin(val username: String, val password: String)

    @Location("/register")
    data class UserRegister(val username: String, val password: String)
}

fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)

@KtorExperimentalLocationsAPI
@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
    install(Locations)

    routing {
        user()

        get("/") {
            call.respondHtml {
                head {
                    title {
                        +"Ktor 入门"
                    }
                }
                body {
                    a {
                        href = "/register"
                        +"注册"
                    }

                    form("/user/login", method = FormMethod.post, encType = FormEncType.multipartFormData) {
                        input {
                            type = InputType.text
                            name = "username"
                        }
                        br
                        input {
                            type = InputType.password
                            value = ""
                            name = "password"
                        }
                        br
                        input {
                            type = InputType.submit
                            value = "login"
                        }
                    }
                }
            }
        }

        get("/login") {
            call.respond("success")
        }

    }
}
@KtorExperimentalLocationsAPI
fun Route.user() {
    post<User.UserLogin> {
        call.respondText("登录成功")
    }

    post<User.UserRegister> { user ->
        run {
            call.respondText("用户 ${user.username} ${user.password}")
        }
    }
}

in browers ,can not find the url

image

but i used the .http file , can get response

image

oleg-larshin commented 4 years ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.