Search code examples
javajspstruts2comboboxognl

Trouble in populating the combobox using Struts 2


I'm having trouble setting the fields of a combobox tag in Struts2 when I'm using the following code in the file

SUCCESS.JSP:

<s:form>
  <s:combobox label="Select and extract a field:" name="fieldname" 
            headerValue="--- Please Select ---" headerKey="1"
            list="fields" listValue="myField" />
</s:form>

On the other hand I can populate the combobox using this more direct way, but this is useful for me only for reassuring that the structure of the project is correct:

<s:form>
   <s:combobox label="Select and extract a field: " name="fieldsNames"
        headerValue="--- Please Select ---"
        headerKey="1" list="{'1','2','3','4','5','6'}"  />
</s:form>

I'm open to any suggestions, since I've tried so many things and it is obvious that I'm stuck.

The rest of the classes and XML files are:

COMBOBOX.JAVA:

    public class comboboxTag extends ActionSupport{

    private List<String> fields;
    private String myField;
    private String fieldName;

    public String getFieldName() 
    {
    return fieldName;
    }

    public void setFieldName(String fieldName) 
   {
      this.fieldName = fieldName;
   }

   public String getField() 
  {
  return myField;
  }

  public void setField(String field) 
  {
        this.myField = field;
  }

 public String execute()throws Exception
 {

  fields = new ArrayList<String>();

  fields.add("1");
  fields.add("2");
  fields.add("3");
  fields.add("4");
  return SUCCESS;

  }

  public List<String> getFields()
  {
  return fields;      
  }

 }

STRUTS.XML:

<struts>

<constant name="struts.multipart.maxSize" value="19000000" />

<package name="fileUploadPackage" extends="struts-default">

    <interceptors>
        <interceptor-stack name="fileUploadStack">
            <interceptor-ref name="exception" />
            <interceptor-ref name="alias" />
            <interceptor-ref name="servletConfig" />
            <interceptor-ref name="prepare" />
            <interceptor-ref name="i18n" />
            <interceptor-ref name="chain" />
            <interceptor-ref name="debugging" />
            <interceptor-ref name="profiling" />
            <interceptor-ref name="scopedModelDriven" />
            <interceptor-ref name="modelDriven" />
            <interceptor-ref name="fileUpload">
                <param name="maximumSize"> 18900000 </param>
                <param name="allowedTypes"> application/pdf,text/plain</param>
            </interceptor-ref>
            <interceptor-ref name="checkbox" />
            <interceptor-ref name="staticParams" />
            <interceptor-ref name="actionMappingParams" />
            <interceptor-ref name="params">
                <param name="excludeParams"> dojo\..*,^struts\..*</param>
            </interceptor-ref>
            <interceptor-ref name="conversionError" />
            <interceptor-ref name="validation">
                <param name="excludeMethods"> input,back,cancel,browse</param>
            </interceptor-ref>
            <interceptor-ref name="workflow">
                <param name="excludeMethods"> input,back,cancel,browse</param>
            </interceptor-ref>
        </interceptor-stack>
    </interceptors>

    <!-- FILE UPLOAD ACTION -->
    <action name="fileUpload" class="org.apache.fileUploadStruts.FileUploadAction">
        <interceptor-ref name="fileUploadStack" />
        <result name="input">/index.jsp</result>
        <result name="success">/success.jsp</result>
    </action>

            <action name="comboboxTag" class="org.apache.fileUploadStruts.comboboxTag">
         <result>/success.jsp</result>
    </action>  

</package>

INDEX.JSP:

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>Upload a File!!</title>
  <s:head />
</head>
<body>
  <s:form action="fileUpload" method="post" enctype="multipart/form-data" >
      <s:file name="file" label="User File: " />
      <s:submit />
     </s:form>   
</body>
</html>

Solution

  • You don't need separate action for combobox. Just create appropriate getter which returns list in your FileUploadAction

    public List<String> getFields() {
      List<String> fields = new ArrayList<String>();
      fields.add("1");
      fields.add("2");
      fields.add("3");
      fields.add("4");
      return fields;
    }
    

    And don't use listValue, because there is simple strings in your list.

    <s:form>
      <s:combobox label="Select and extract a field:" name="fieldname" 
            headerValue="--- Please Select ---" headerKey="1"
            list="fields" />
    </s:form>