3

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.

Sicine
  • 33
  • 7
  • Remove your very suspicious and useless `getJSON()` method, then check out if you have a namespace for that action, in that case you need to specify it in the – Andrea Ligios Jun 09 '15 at 12:28
  • Also post your SignatureKeyType code and define "it's not working" – Andrea Ligios Jun 09 '15 at 12:32
  • Do you have a getter for that list, do you ? – Andrea Ligios Jun 09 '15 at 12:33
  • first thank you so much for the answer; i have checked all the getters and setters, it has been my first instinct. and i edited my post to post the SignatureKeyType. what is not working is that i can not see the elements in my .. – Sicine Jun 09 '15 at 12:50
  • You're welcome; I meant the getter in the action (the getters in the entity are ok). Also are you sure the flag is 1 ? Maybe the list is just empty due to a wrong check; I suggest you tu put some logging in your first `for` and at the end of the action methot to print what the lists are. **Also notice that in the last 2 `for`s, instead of adding you are assigning**. LOL :) – Andrea Ligios Jun 09 '15 at 12:57
  • yes i checked all the getters and setters in the action also, can u explain more to me about the name space for the , the name space of the action that returns the form is in namespace="/admin" should i put the same namespace for that action too ?? – Sicine Jun 09 '15 at 13:25
  • It is better to always use a namespace, at least for clarity. If your action runs under a namespace that is not the default one (that is "", not "/"), then you need to specify it when calling the action, eg: `` – Andrea Ligios Jun 09 '15 at 13:28
  • what if i want to map the action not by annotations but by xml; i did the following : .. but the type="json" is not defined. :( how can i do it, i belive that if i mapped it like that it may solve the problem since all my actions are mapped that way.. – Sicine Jun 09 '15 at 13:45
  • the type json is defined if your package extends `json-default`, not `struts-default` – Andrea Ligios Jun 09 '15 at 13:50
  • @Sicine Annotations use different configuration provider. See [this](http://stackoverflow.com/a/25708453/573032) answer. – Roman C Jun 09 '15 at 13:53
  • thank you all, i did a sysout in my action and it seems that the lists are not empty, but still i can't see the values in my select, everything seems to be correct :( !!! – Sicine Jun 09 '15 at 15:51
  • You need to use firebug, or type the url of "%{remoteurl}" into the address bar of firefox. Either way you *should* see a string of json, If not the action is not returning json. The easiest way to correct for that is to use the ParentPackage annotation. – Quaternion Jun 11 '15 at 16:39

0 Answers0