嗨,我想知道是否存在一种方法来避免此代码中的重复代码。现在我有一个名为 CustomerAction 的操作类,该类处理请求的行为(它就像一个 Controller ),并且我有一个具有 id、name、last_name 等属性的 CustomerPOJO。现在我必须向 CustomerAction 添加属性来处理提交的数据从形式上看。有什么方法可以绕过我的 CustomerPOJO 的操作吗?
public class CustomerAction {
private String nombre;
private String apellido;
private String dni;
private String fechaNac;
private String obraSocial;
private String nroAsociado;
private String plan;
private String password;
private String email;
private String telParticular;
private String telCelular;
private static final Log log = LogFactory
.getLog(CustomerAction.class);
public String execute() throws Exception {
if ("cancelar".equals(this.getAccion())) {
log.debug("Executing 'cancelar' action");
return "login";
}
if ("registro".equals(accion)) {
log.debug("Executing 'registro' action");
IReferenceDataBusinessDelegate ud = new ReferenceDataBusinessDelegate();
ud.signCustomer(this.getNombre(), this.getApellido(),
this.getDni(), this.getCorreo(), this.getContrasena());
return "login";
}
}
<小时/>
public class Customers implements java.io.Serializable {
private long id;
private String dni;
private String name;
private String lastName;
private String email;
private String password;
private String phone;
private String cellphone;
private Date birthDate;
private Date creationDate;
private Date lastAccessDate;
private byte active;
private Set<Profesionales> profesionaleses = new HashSet<Profesionales>(0);
private Set<Pacientes> pacienteses = new HashSet<Pacientes>(0);
public Customers() {
}
}
最佳答案
是的,使用ModelDriven
,并使用Customers
作为模型。
http://struts.apache.org/2.x/docs/model-driven.html
您需要确保“modelDriven”
拦截器位于您的堆栈中。
如何/在哪里初始化模型取决于您的特定使用场景;如果您需要从数据库重新加载它,您可以在 getter 中执行此操作,如文档中所示,在 prepare()
方法中执行此操作。
我不确定你所说的“绕过操作”是什么意思。
请注意,此处使用 accion
参数实现的临时调度机制与 Struts 2 使用操作配置的 method
属性提供的功能重复。我不建议使用临时调度机制,因为它会使理解程序流程变得不必要的困难。
关于java - 有什么办法可以避免这段代码中重复属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14611760/