为什么要解除触发方法?

发布于 2024-09-13 08:03:26 字数 1518 浏览 3 评论 0原文

Lift 框架中有一个简单的表单和一个带有处理该表单的 render 方法的类:

 def render(xhtml:NodeSeq) = {
        var name = ""
        var role = ""
        var human = ""
        def register = {
            val person = new Person
            person.name = name
            person.role = role
            person.human = if (human == "yes") "true" else "false"
            model.create(person)
            S.redirectTo("/index")
        }
        bind("user",
        xhtml,
        ("name" -> SHtml.text(name, name = _)),
        ("role" -> SHtml.text(role, role = _)),
        ("human" -> SHtml.text(human,human = _)),
        ("submit" -> SHtml.submit("Register",register)))
    }

当我运行这个示例时,例如。我访问绑定到相应表单的 url,出现以下错误:

Message: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number
...
...
...
    epsilon.sjbs.CrudModel$class.create(Sjbs.scala:14)
    epsilon.sjbs.PersonModel.create(Sjbs.scala:7)
    epsilon.snippet.PersonSnippet.register$1(Snippet.scala:33)
    epsilon.snippet.PersonSnippet.render(Snippet.scala:41)

即使没有单击提交,仅访问 url,注册方法也会运行。为什么?
编辑:这是我原来的实体

@NamedQuery(name = "findAll", query = "select x from Person x")
@Entity class Person extends Id with Name{
    @OneToMany(mappedBy="person", cascade=Array(CascadeType.REMOVE))
    var participated:java.util.List[Participant] = new java.util.ArrayListParticipant
    var role:String = Role.User
    var human:String = _
}

there's a simple form in Lift framework and a Class with render method that handles the form:

 def render(xhtml:NodeSeq) = {
        var name = ""
        var role = ""
        var human = ""
        def register = {
            val person = new Person
            person.name = name
            person.role = role
            person.human = if (human == "yes") "true" else "false"
            model.create(person)
            S.redirectTo("/index")
        }
        bind("user",
        xhtml,
        ("name" -> SHtml.text(name, name = _)),
        ("role" -> SHtml.text(role, role = _)),
        ("human" -> SHtml.text(human,human = _)),
        ("submit" -> SHtml.submit("Register",register)))
    }

When I run this example, eg. I access the url which is binded to the according form, I get following error:

Message: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number
...
...
...
    epsilon.sjbs.CrudModel$class.create(Sjbs.scala:14)
    epsilon.sjbs.PersonModel.create(Sjbs.scala:7)
    epsilon.snippet.PersonSnippet.register$1(Snippet.scala:33)
    epsilon.snippet.PersonSnippet.render(Snippet.scala:41)

this looks like that the register method is ran even there's no click on submit, just accessing the url. Why?
EDIT:this is my original entity

@NamedQuery(name = "findAll", query = "select x from Person x")
@Entity class Person extends Id with Name{
    @OneToMany(mappedBy="person", cascade=Array(CascadeType.REMOVE))
    var participated:java.util.List[Participant] = new java.util.ArrayListParticipant
    var role:String = Role.User
    var human:String = _
}

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

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

发布评论

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

评论(2

傻比既视感 2024-09-20 08:03:26

我认为 Person. human 是一个 Boolean,而不是 String...

看起来您正在使用类似于 Mapper 的东西。如果您使用 Mapper,我会这样做:

def render(xhtml:NodeSeq) = {
  val person = Person.create
  bind(
    "user",
    xhtml,
    "name" -> SHtml.text(person.name, person.name(_)),
    "role" -> SHtml.text(person.role, person.role(_)),
    "human" -> SHtml.text(
      if (person.human) ? "yes" else "no",
      (human) => person.human(if (human == "yes") true else false)
    ),
    "submit" -> SHtml.submit("Register", () => {
      person.save
      S.redirectTo("/index")
    })
  )
}

当然只是个人喜好,但这样您就不会使用显式的 vars 并且可以轻松地重用它来编辑现有的 ...

I think that Person.human is a Boolean, not a String...

It looks like you're using something that looks like Mapper. If you were using Mapper, I'd do it like so:

def render(xhtml:NodeSeq) = {
  val person = Person.create
  bind(
    "user",
    xhtml,
    "name" -> SHtml.text(person.name, person.name(_)),
    "role" -> SHtml.text(person.role, person.role(_)),
    "human" -> SHtml.text(
      if (person.human) ? "yes" else "no",
      (human) => person.human(if (human == "yes") true else false)
    ),
    "submit" -> SHtml.submit("Register", () => {
      person.save
      S.redirectTo("/index")
    })
  )
}

Just personal preference of course, but that way you're not using an explicit vars and can easily reuse it to edit an existing Person...

浪漫人生路 2024-09-20 08:03:26

正如 pr1001 所写的 person. human 可能是布尔值。

未调用 register,但 person. human 已被评估,因为人类字符串是在 register 方法之外设置的。您只需更改分配:

person.human = if (human == "yes") true else false

As pr1001 wrote person.human is likely to be boolean.

register is not called, but person.human is already evaluated because human string is set out of register method. You have only to change the assignment:

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