为什么要解除触发方法?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为
Person. human
是一个Boolean
,而不是String
...看起来您正在使用类似于 Mapper 的东西。如果您使用 Mapper,我会这样做:
当然只是个人喜好,但这样您就不会使用显式的
vars
并且可以轻松地重用它来编辑现有的人
...I think that
Person.human
is aBoolean
, not aString
...It looks like you're using something that looks like Mapper. If you were using Mapper, I'd do it like so:
Just personal preference of course, but that way you're not using an explicit
vars
and can easily reuse it to edit an existingPerson
...正如 pr1001 所写的 person. human 可能是布尔值。
未调用 register,但 person. human 已被评估,因为人类字符串是在 register 方法之外设置的。您只需更改分配:
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: