Question

I have a little (i hope) problem trying to use Doubleselect element from Struts2 jQuery plugin. I have followed the sample without problems, and the behaviour is as expected when I add a new record (add new record to database), but when i try to edit an existing one the second select does not load the stored value for the record being edited.

Any help?

Code and configuration follows:

JSP code

<s:form id="ingresoForm" action="saveIngreso" method="post" validate="true" cssClass="well form-horizontal">
    <s:hidden key="ingreso.id"/>
    <s:hidden key="cliente" id="cliente"/>

    <div class="type-text">
            <label for="cliente">Cliente: </label>
            <s:url var="remoteurl" action="ajax/clienteProyectoSelectSource"/>
            <sj:select
                href="%{remoteurl}"
                id="clienteSelect"
                onChangeTopics="reloadsecondlist"
                name="ingreso.cliente.id"
                list="clientes"
                listKey="id"
                listValue="nombre"
                emptyOption="false"
                headerKey="-10"
                headerValue="Por favor seleccione un cliente"/>
        </div>
        <div class="type-text">
            <label for="Proyecto">Proyecto: </label>
            <sj:select
                href="%{remoteurl}"
                id="proyectoSelect"
                formIds="ingresoForm"
                reloadTopics="reloadsecondlist"
                name="ingreso.proyecto.id" 
                list="proyectos"
                listKey="id"
                listValue="nombre"
                emptyOption="false"
            />
        </div>

Action Code

public class ClienteProyectoSelectSourceAjaxAction extends BaseAction {

private List<Cliente> clientes;
private List<Proyecto> proyectos;
private String cliente;
private GenericManager<Cliente, Long> clienteManager;


@Override
public String execute() {

    clientes = clienteManager.getAll();

    if (cliente != null && cliente.length() > 0 && !cliente.equals("-10")) {
        proyectos = clienteManager.get(new Long(cliente)).getProyectos();
    }
    return Action.SUCCESS;
}

Action Declaration

<package name="example" extends="json-default"  namespace="/ajax">
    <action name="clienteProyectoSelectSource" class="com.queres.smtm.webapp.action.ajax.ClienteProyectoSelectSourceAjaxAction">
        <result type="json"/>
    </action>
</package>

Ingreso entity (model)

@Entity
@Table(name = "ingreso")
public class Ingreso extends BaseObject {

// Campos comunes
private Long id;
private TipoIngreso tipo;
private String observaciones;
private BigDecimal importe;
private BigDecimal tipoIVA;
private Date fechaPrevistaCobro;
private Date fechaEfectivaCobro;

// Campos para facturas
private String numeroFactura;
private Cliente cliente;
private Proyecto proyecto;
private TipoServicio servicio;
private Date fechaEmision;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}


@Enumerated(EnumType.STRING)
public TipoIngreso getTipo() {
    return tipo;
}

public void setTipo(TipoIngreso tipo) {
    this.tipo = tipo;
}

public String getObservaciones() {
    return observaciones;
}

public void setObservaciones(String observaciones) {
    this.observaciones = observaciones;
}

public BigDecimal getImporte() {
    return importe;
}

public void setImporte(BigDecimal importe) {
    this.importe = importe;
}

public BigDecimal getTipoIVA() {
    return tipoIVA;
}

public void setTipoIVA(BigDecimal tipoIVA) {
    this.tipoIVA = tipoIVA;
}

@Temporal(javax.persistence.TemporalType.DATE)
@Field
public Date getFechaPrevistaCobro() {
    return fechaPrevistaCobro;
}

public void setFechaPrevistaCobro(Date fechaPrevistaCobro) {
    this.fechaPrevistaCobro = fechaPrevistaCobro;
}

@Temporal(javax.persistence.TemporalType.DATE)
@Field
public Date getFechaEfectivaCobro() {
    return fechaEfectivaCobro;
}

public void setFechaEfectivaCobro(Date fechaEfectivaCobro) {
    this.fechaEfectivaCobro = fechaEfectivaCobro;
}

public String getNumeroFactura() {
    return numeroFactura;
}

public void setNumeroFactura(String numeroFactura) {
    this.numeroFactura = numeroFactura;
}

@ManyToOne
public Cliente getCliente() {
    return cliente;
}

public void setCliente(Cliente cliente) {
    this.cliente = cliente;
}

@ManyToOne
public Proyecto getProyecto() {
    return proyecto;
}

public void setProyecto(Proyecto proyecto) {
    this.proyecto = proyecto;
}

@Enumerated(EnumType.STRING)
public TipoServicio getServicio() {
    return servicio;
}

public void setServicio(TipoServicio servicio) {
    this.servicio = servicio;
}

@Temporal(javax.persistence.TemporalType.DATE)
@Field
public Date getFechaEmision() {
    return fechaEmision;
}

public void setFechaEmision(Date fechaEmision) {
    this.fechaEmision = fechaEmision;
}

@Override
public int hashCode() {
    int hash = 3;
    hash = 43 * hash + (this.numeroFactura != null ? this.numeroFactura.hashCode() : 0);
    hash = 43 * hash + (this.fechaEmision != null ? this.fechaEmision.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Ingreso other = (Ingreso) obj;
    if ((this.numeroFactura == null) ? (other.numeroFactura != null) : !this.numeroFactura.equals(other.numeroFactura)) {
        return false;
    }
    if (this.fechaEmision != other.fechaEmision && (this.fechaEmision == null || !this.fechaEmision.equals(other.fechaEmision))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "Ingreso{" + "id=" + id + ", tipo=" + tipo + ", observaciones=" + observaciones + ", importe=" + importe + ", tipoIVA=" + tipoIVA + ", fechaPrevistaCobro=" + fechaPrevistaCobro + ", fechaEfectivaCobro=" + fechaEfectivaCobro + ", numeroFactura=" + numeroFactura + ", cliente=" + cliente + ", proyecto=" + proyecto + ", servicio=" + servicio + ", fechaEmision=" + fechaEmision + '}';
}

}

Thx in advance

Was it helpful?

Solution

Problem solved. It seems that jquery-plugin work perfectly, as usual the error was between the keyboard and the chair...

I forgot to load the data list for the second select, so jquery was unable to select the aproppiate value.

So, the solution was to ensure that the second list (proyectos) was loaded when the user edits an element.

I add a flag (cliente) as a hidden element on JSP and preloaded it from the main action, so I can check from the Ajax Action if it is necessary to populate the second list.

Ingreso Action (main action for the view)

public class IngresoAction extends BaseAction implements Preparable {

private String cliente;

public String edit() {
    if (id != null) {
        ingreso = ingresoManager.get(id);
        cliente = Long.toString(ingreso.getCliente().getId());

    } else {
        ingreso = new Ingreso();
    }

    return SUCCESS;
}

public String getCliente() {
    return cliente;
}

public void setCliente(String cliente) {
    this.cliente = cliente;
}

<...>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top