java - 有什么办法可以避免这段代码中重复属性吗?

标签 java struts2

嗨,我想知道是否存在一种方法来避免此代码中的重复代码。现在我有一个名为 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/

相关文章:

javax.swing.JCheckBox setSelected 未从 GUI 调用

struts2 - Struts 2 中的 HTTP Referer header

java - Struts 2 和一个简单的 Facebook API

java - Struts2 - 具有多种方法的 Action ?

java - 如何通过属性文件访问Struts框架上数组列表的值

java - 如何指定主类名称并向我的 Java 程序提供参数?

java - google eclipse插件创建的log4j.properties和logging.properties有什么区别?

java - 如何监听键盘输入?比如方向键?

java - JSoup 选择器内部文本

java - struts2 中的多个应用程序属性,需要根据某些条件更改 java 中的属性文件?