Daniel Morgan
Daniel Morgan

Reputation: 772

The form does not submit the selected value of the <s:select>

I have the following dropdown list which correctly shows the options but when I select an item and submit the form it runs into the following error :

'select', field 'list', name 'name': The requested list key 'listnames' could not be 
 resolved as a collection/array/map/enumeration/iterator type. Example: people or 
 people.{name} - [unknown location]

My JSP form

      <s:form method="POST" action="addNames">
            <s:select name="name"
                      label="Names"
                      list="listnames"
                      />
     </s:form>

My Action

 @Action
 public class Myaction implements ModelDriven{
    private MyClass myclass = new MyClass();
    private List listnames = new ArrayList();

   @Override
   public MyClass getModel() {
      return this.myclass;
   }

  public List getListnames() {
     return listnames;
  }

  public void setListnames(List listnames) {
     this.listnames = listnames;
 } 


   public MyClass getMyClass() {
       return myclass;
 }

   public void setMyClass(MyClass myclass) {
       this.myClass = myclass;
   }
 }

My Class

 public class MyClass {
   private String name;

   ..... getter and setters go here ....
 }

struts

<package name="MyUsers" extends="default" namespace="/MyUsers">
        <action name="*" method="{1}" class="com.myproject.controller.Myaction">
            <result name="uAdd" type="tiles" >uAdd</result>
            <result name="uView" type="tiles" >uView</result>
        </action>
    </package>

Upvotes: 2

Views: 2416

Answers (3)

Nidhish Krishnan
Nidhish Krishnan

Reputation: 20741

STEP 1 :make sure that getter and setter for listnames are done properly

STEP 2 :make sure that you have done the declaration and initialization for listnames List properly

UPDATE 2 Sample Example

struts.xml

<action name="getText" class="commonpackage.ReportsCommonClass" method="getText">
<result name="success">index.jsp</result>
</action>

<action name="myaction" class="commonpackage.ReportsCommonClass" method="myaction">
<result name="success">index2.jsp</result>
</action>

index.jsp

 <s:form id="conform" action="myaction" method="post">
 <label>NAME</label>
     <s:select id="name1" name="name1" list="mylist" headerKey="0" headerValue="--SELECT--"/>              

<s:submit value="Click" />  
 </s:form>

In commonpackage.ReportsCommonClass class

ArrayList mylist=new ArrayList();

    public ArrayList getMylist() {
        return mylist;
    }

    public void setMylist(ArrayList mylist) {
        this.mylist = mylist;
    }

  public String getText()
  {
      mylist.add("NAME 1");
       mylist.add("NAME 2");
        mylist.add("NAME 3");
         mylist.add("NAME 4");
          mylist.add("NAME 5");
       return SUCCESS;
  }
  String name1;

    public String getName1() {
        return name1;
    }

    public void setName1(String name1) {
        this.name1 = name1;
    }


  public String myaction()
  {
       System.out.println("NAMEEEEEEEEEEEEEEEEEEEEE:"+name1);
       return SUCCESS;
  }

Upvotes: 2

Rais Alam
Rais Alam

Reputation: 7016

change your select tag like this

 <s:form method="POST" action="addNames">
            <s:select name="myclass.name"
                      label="Names"
                      list="listnames"
                      />
     </s:form>

Edit:

Problem: I guess you are hitting jsp directly hence there is no any action execution. If there is no any action execution then there is no any list in request.

Solution. Hit URL in way that action class get executed and list should initialize or populated before rendering the jsp or view.

Create a method like populateView in action class and execute this method rather than directly execution JSP.

Hope you understand what I want to say.

Upvotes: 1

MayurB
MayurB

Reputation: 3649

Provide getter setter for name in your action class. As your select tag name is name <s:select name="name"> when you submit your form it will search for the property name in your action class. This may be the problem in your case

Upvotes: 1

Related Questions