ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
我们查看5.1与3.1的规范。在其基础上增加一个ID字段。 # 新建UpadtaAction. ``` package com.mengyunzhi.javaee.action.klass; import com.mengyunzhi.javaee.action.Action; import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator; import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator; import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator; public class UpdateAction extends Action { private static final long serialVersionUID = 1L; private Long id; private String name; private Long teacherId; public Long getId() { return id; } @RequiredFieldValidator(message = "必须传入id字段值") public void setId(Long id) { this.id = id; if (this.id == 1000L) { this.addFieldError("id", "传入的班级ID不存在或已删除"); } } public String getName() { return name; } @RequiredStringValidator(message = "名称不能为空") @StringLengthFieldValidator(minLength = "2", maxLength = "8", trim = true, message = "名称必须介于2-8之间") public void setName(String name) { this.name = name; } public void setTeacherId(Long teacherId) { if(teacherId == 200) { this.addFieldError("teacherId", "传入的teacherId无效"); } this.teacherId = teacherId; } public Long getTeacherId() { return this.teacherId; } public String execute() { return SUCCESS; } } ``` 测试: ![https://box.kancloud.cn/9b2447b9d42f2bdad48fc1f18f51015e_442x467.gif](https://box.kancloud.cn/9b2447b9d42f2bdad48fc1f18f51015e_442x467.gif) # BUG 其实我们在上面,相当于将 SaveAction的代码又重新写了一遍。那么一个问题就来了,为什么我们不直接继承SaveAction呢?的确,在教程的第一个版本中,我也是这么做的。但是。。。却发生了莫名的BUG。当我们使用继承后,在chrome和firefox中发起请求时,却意外的接收不到Header参数,从而导致发生:服务端不允许跨域的BUG。至于BUG产生的原因,就留给我们以后研究中,现阶段,我们只需要记往:所有的触发器,都只能继承于Action,如果不这么做,将会发生不允许跨域的错误。 >git checkout -f step12.5.2