Struts 2: How to add dynamically inputs in jsp from the action?

645 views Asked by At

I'm trying to create a select that for each option of that select I will have specified inputs.

For example select has A,B,C options; if I select A, one textfield and one file upload field will appear; if I select B instead, two textfields will appear.

It'ss a JEE application and I'm working with Struts2, struts2-json-plugin and struts2-jquery-plugin.

I've tried to follow the documentation but it's not working.

Action:

    @Actions({
        @Action(
           value="jsonsample",
           results={ @Result(name="success",type="json")}
        )
    })
    public String execute() throws Exception{
        List<SignatureKeyType> signatureKeyTypes = signatureKeyTypeService.findAll();
        signatureKeyTypesSubscriber = new ArrayList<SignatureKeyType>();
        signatureKeyTypesServer = new ArrayList<SignatureKeyType>();
        for (int i=0; i<signatureKeyTypes.size(); i++) {
            if(signatureKeyTypes.get(i).getFlag()==1){
                signatureKeyTypesSubscriber.add(signatureKeyTypes.get(i));
            }else{
                signatureKeyTypesServer.add(signatureKeyTypes.get(i));
            }
        }

        listParamSubscriber = new ArrayList<SignatureKeyTypeParameter>();
        for (SignatureKeyType signatureKeyTypeSub : signatureKeyTypesSubscriber) {
            listParamSubscriber = signatureKeyTypeSub.getSignatureKeyTypeParameter();
        }

        listParamServer = new ArrayList<SignatureKeyTypeParameter>();
        for (SignatureKeyType signatureKeyTypeServer : signatureKeyTypesServer) {
            listParamServer = signatureKeyTypeServer.getSignatureKeyTypeParameter();
        }

        return SUCCESS;
    }

    public String getJSON() throws Exception{
        return execute();
    }

JSP:

<s:url var="remoteurl" action="jsonsample"/>
<sj:select href="%{remoteurl}" 
             id="echo" 
           name="echo"
           list="signatureKeyTypesSubscriber"
        listKey="idSignatureKeyType"
      listValue="label" 
    emptyOption="true" 
      headerKey="-1"
    headerValue="Please Select" 
/>

SignatureKeyType:

@Entity
@Table(name="signaturekeytype")
public class SignatureKeyType extends Tokenizable{

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long idSignatureKeyType;

    @Column(name = "label", nullable = false)
    private String label;

    @Column(name = "flag")
    private int flag;

     @OneToMany(mappedBy = "signatureKeyType")
   private List<ServerSignature> serverSignatures;

   @OneToMany(mappedBy = "signatureKeyType")
   private List<SubscriberSignature> subscriberSignatures;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "signatureKeyType", cascade = CascadeType.ALL)
   private List<SignatureKeyTypeParameter> signatureKeyTypeParameter;

   public Long getIdSignatureKeyType() {
      return idSignatureKeyType;
   }

   /** @param newIdSignatureKeyType */
   public void setIdSignatureKeyType(Long newIdSignatureKeyType) {
      idSignatureKeyType = newIdSignatureKeyType;
   }

   public String getLabel() {
      return label;
   }

   /** @param newLabel */
   public void setLabel(String newLabel) {
      label = newLabel;
   }



public List<ServerSignature> getServerSignatures() {
    return serverSignatures;
}

public void setServerSignatures(List<ServerSignature> serverSignatures) {
    this.serverSignatures = serverSignatures;
}

public List<SubscriberSignature> getSubscriberSignatures() {
    return subscriberSignatures;
}

public void setSubscriberSignatures(List<SubscriberSignature> subscriberSignatures) {
    this.subscriberSignatures = subscriberSignatures;
}

public int getFlag() {
    return flag;
}

public void setFlag(int flag) {
    this.flag = flag;
}

public List<SignatureKeyTypeParameter> getSignatureKeyTypeParameter() {
    return signatureKeyTypeParameter;
}

public void setSignatureKeyTypeParameter(List<SignatureKeyTypeParameter> signatureKeyTypeParameter) {
    this.signatureKeyTypeParameter = signatureKeyTypeParameter;
}


}

The first problem is that I can't get my list in the select, when I use a simple Struts select <s:select> it works, I can see my list but when I use the <sj:select> I can't, I presume that it's because of JSON but I can't figure out what it is it exactly.

0

There are 0 answers