0

嗨,我想知道是否存在避免此代码中重复代码的方法。现在我有一个名为 CustomerAction 的动作类,这个类处理请求的行为(它就像一个控制器),我有一个 CustomerPOJO,其属性包括 id、name、last_name 等。现在我必须向 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() {
}
}
4

1 回答 1

2

是的,使用ModelDriven,并使用 aCustomers作为模型。

http://struts.apache.org/2.x/docs/model-driven.html

您需要确保"modelDriven"拦截器在您的堆栈中。

初始化模型的方式/位置取决于您的特定使用场景;您可以在文档中显示的 getter 中执行此操作,prepare()如果您需要从数据库中重新加载它,则可以在方法中执行,等等。

我不确定您所说的“绕过操作”是什么意思。

请注意,这里使用accion参数实现的 ad-hoc 调度机制复制了 Struts 2 使用method动作配置的属性提供的功能。我不建议使用 ad-hoc 调度机制,因为它使理解程序流变得比必要的更加困难。

于 2013-01-30T19:08:17.437 回答