Link to home
Start Free TrialLog in
Avatar of ayeen
ayeenFlag for United States of America

asked on

struts problem: pulldown menu

hi all,
i'm just wondering if someone could help me with my struts pulldown menu problem.... i'm trying to convert my multibox to a pulldown menu... the user can only choose 1 driver...
thanks in advance!

- @


i have this multibox:

<logic:notEmpty name="processFilterFormBean" property="driverlist">
<logic:iterate name="processFilterFormBean" property="driverlist" id="driver">             
<tr>
      <td>
      <html:multibox name="driver" property="selecteddriverid">
            <bean:write name="driver" property="drivername"/>
      </html:multibox>
      <bean:write name="driver" property="drivername" />
      </td>
</tr>
</logic:iterate>
</logic:notEmpty>

that i want to make into a pulldown menu...


i've tried doing  the several <html:select>/<html:optionsCollection> tutorials i found online but i can't make it work and i would really appreciate your help guys...




---------------------------------------
here's what my struts-config.xml looks:
---------------------------------------

<action-mappings>
      <action path="/maintenance" type="com.webtest.resources.MaintenanceAction" name="processFilterFormBean" scope="request">
            <forward name="start" path = "/WEB-INF/pages/Maintenance.jsp"/>            
      </action>
</action-mappings>


<form-beans>
      <form-bean name="processFilterFormBean" type="com.webtest.resources.ProcessFilterFormBean">
      </form-bean>
</form-beans>      





---------------------------------------
here's my Form Bean:
---------------------------------------

public class ProcessFilterFormBean extends ActionForm {
      private Collection driverlist;
      private String[] selecteddriverid;

      private String driverid = "";
      private String drivername = "";



      // for driver list
      public void setDriverlist(Collection driverlist){
            this.driverlist = driverlist;            
      }

      public Collection getDriverlist(){
            return driverlist;
      }
      
      public String[] getSelecteddriverid(){
            return selecteddriverid;
      }
      
      public void setSelecteddriverid(String[] driverid){
            this.selecteddriverid = driverid;
      }      
      
      
      
      public void reset(ActionMapping mapping, HttpServletRequest request) {
            driverlist = new ArrayList();

      }

      public ActionErrors validate(
            ActionMapping mapping,
            HttpServletRequest request) {

            ActionErrors errors = new ActionErrors();
            return errors;

      }

}




---------------------------------------
here's my action class:
---------------------------------------

public class MaintenanceAction extends Action {

      public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {
                        
            ActionErrors errors = new ActionErrors();
            ActionForward forward = new ActionForward();

            ProcessFilterFormBean pfbean = (ProcessFilterFormBean) form;

            //call the DB
            SimulateDB simulateDB = new SimulateDB();
            pfbean.setDriverlist(simulateDB.getDriverlist(request.getSession()));            

            
            return (mapping.findForward("start"));

      }
}





---------------------------------------
here is my SimulateDB:
---------------------------------------
public class SimulateDB {

    private String drivername;
      private Collection dlist;
      private String result;

    private void init(HttpSession session) {
            
            //test data while DB is not  yet ready
            dlist = new ArrayList();
            dlist.add(new Driver("CCMP",  "Suspense",                   false));
            dlist.add(new Driver("EXALL", "Exclusion Indicator",             false));
            dlist.add(new Driver("EXSUB", "Exclusion Subscriber",             false));
            dlist.add(new Driver("MAIL",  "Mail to Option",             false));
            dlist.add(new Driver("PTD",   "Paid to Date",                   false));
            dlist.add(new Driver("SUSP",  "Suspense Days",                   false));
            dlist.add(new Driver("TERM",  "Termination Days",             false));            
            dlist.add(new Driver("OTHER", "Other",                         false));            
            session.setAttribute("driverDB", dlist);
            
            //another set..trying LabelValueBean
            ArrayList dlist2 = new ArrayList();
      
            dlist2.add(new org.apache.struts.util.LabelValueBean("CCMP",  "Suspense"));
            dlist2.add(new org.apache.struts.util.LabelValueBean("EXALL", "Exclusion Indicator"));
            dlist2.add(new org.apache.struts.util.LabelValueBean("EXSUB", "Exclusion Subscriber"));            
            dlist2.add(new org.apache.struts.util.LabelValueBean("MAIL",  "Mail to Option"));            
            dlist2.add(new org.apache.struts.util.LabelValueBean("PTD",   "Paid to Date"));
            dlist2.add(new org.apache.struts.util.LabelValueBean("SUSP",  "Suspense Days"));
            dlist2.add(new org.apache.struts.util.LabelValueBean("TERM",  "Termination Days"));            
            dlist2.add(new org.apache.struts.util.LabelValueBean("OTHER", "Other"));
            session.setAttribute("fulfillment_drivers", dlist2);

    }

   
    private void loadData(HttpSession session) {
           dlist = (Collection) session.getAttribute("driverDB");
        if (dlist == null || elist == null)
            init(session);
    }

    public String getDrivername(HttpSession session) {
        loadData(session);
        return drivername;

    }
          
   public void loadDriverlist(HttpSession session){
      //put them in our session
      dlist = (Collection) session.getAttribute("driverDB");
      if (dlist == null)
            init(session);
   }
   public Collection getDriverlist(HttpSession session) {
      loadDriverlist(session);
      return dlist;
   }      
      
}

---------------------------------------
here's my Driver class:
---------------------------------------

public class Driver {
      
      private String driverid = "";
      private String[] selecteddriverid;
      private String drivername = "";

      
      public Driver(){}
      
      public Driver(String driverid, String drivername, boolean checked){
            this.driverid = driverid;
            this.drivername = drivername;
      }
      
      public void setDriverid(String driverid){
      this.driverid = driverid;
      }
      public String getDriverid(){
            return driverid;
      }      
            
      public String[] getSelecteddriverid(){
            return selecteddriverid;
      }
      
      public void setSelecteddriverid(String[] driverid){
            this.selecteddriverid = driverid;
      }
      
      public void setDrivername(String drivername){
            this.drivername = drivername;
      }      

      public String getDrivername(){
            return drivername;
      }

}
Avatar of Jathrek
Jathrek

Maybe you could simply use a html:select (used to define an HTML select dropdown list), which you would define outside of the logic:iterate, and using html:option inside of the loop to generate the content of the list...

Maybe you could even remove the loop by using a single html:optionsCollection which would refer the collection you have in your actionForm.

Anyway, you'll find informations for those tags in the official taglib reference;
http://struts.apache.org/1.3.8/struts-taglib/tagreference.html#struts-html.tld
Avatar of ayeen

ASKER

thanks jathrek.... i tried doing what you have suggested..but i'm still getting errors...this is my code:

<form-bean name="singleSelectForm"
  type="org.apache.struts.action.DynaActionForm">
  <form-property name="control" type="java.lang.String"/>
</form-bean>

approach 1: error: Jsp Translate: Attribute <html: option has no value
inside my jsp:
<html:select property="control" size="2">
  <logic:iterate id="driver" name="driverlist"
    <html:option value="<%=driver.getDriverid()%>">
      <bean:write name="driver" property="drivername"/>
    </html:option>
  </logic:iterate>
</html:select>


2nd approach: error: Error 500: No getter method available for property driver for bean under name org.apache.struts.taglib.html.BEAN

inside my struts-config.xml
<form-bean name="singleSelectForm"
    type="org.apache.struts.action.DynaActionForm">
   <form-property name="control" type="java.lang.String"/>
</form-bean>


inside my jsp:
<html:select property="singleSelectForm">
  <html:options collection="driverlist" property="selecteddriverid" labelProperty="drivername"/>
</html:select>


thanks for your time..


For the option, you may try;
<html:option name="driver" property="selecteddriverid">...</html:option>

For the second error;
I do not really understand the error message, it seems you've an struts tag generating a HTML field which value is based on a property "driver". Though, the small JSP code you show me is using a "selecteddriverid" , so I think the error message is not related to that select element.

Anyway, try correcting the first case and tell me how it works out?

Sorry for those little disturbance, I'm far from being perfect (at least for now :p)...
Avatar of ayeen

ASKER

hi again Jathrek,

this is how my jsp now ooks:

<html:select property="control">
  <logic:iterate id="" name="driverlist"
    <html:option name="driver" property="selecteddriverid>
      <bean:write name="driver" property="drivername"/>
    </html:option>
  </logic:iterate>
</html:select>

and this is the error --> JspTranslate: Attribute <html:option has no value
Is it a copy and paste?
If yes, it seems that the logic:iterate tag is missing the value for the ID attribute (which probably should be "driver".

Otherwise, I have no idea what causes that error; I've never seen it, and it should work as is :(
Avatar of ayeen

ASKER

hi...sorry about that: here it is:

<html:select property="control" size="2">
  <logic:iterate id="driver" name="driverlist">
    <html:option name="driver" property="selecteddriverid">
      <bean:write name="driver" property="drivername"/>
    </html:option>
  </logic:iterate>
</html:select>

the error is now: JspTranslate: Attribute name invalid according to the specified TLD

thanks again!


ASKER CERTIFIED SOLUTION
Avatar of Jathrek
Jathrek

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ayeen

ASKER

sorry for bugging you again..i'm new in struts and i'm really just getting the hang of it..

i'm getting this error:
Error 500: No getter method available for property control for bean under name org.apache.struts.taglib.html.BEAN

here's my jsp code:
<html:select property="control" size="2">
  <logic:iterate id="driver" name="driverlist">
    <option value="<bean:write name="driver" property="selecteddriverid"/>">
      <bean:write name="driver" property="drivername"/>
    </option>
  </logic:iterate>
</html:select>

here is my struts-config.xml entries:
<form-beans>
     <form-bean name="singleSelectForm"
        type="org.apache.struts.action.DynaActionForm">
                       <form-property name="control" type="java.lang.String"/>
     </form-bean>
     <form-bean name="processFilterFormBean" type="com.webtest.resources.ProcessFilterFormBean">
      </form-bean>
</form-beans>      

all other files and codes are still the same as i posted above....
Well, don't worry, I taught I was getting good at Struts, but it seems I'm still pretty far from it ^^

Anyway, for your other problem, I can only tell you to check a few things;
- Is the html:select correctly surrounded by a html:form?
- Is the action referenced by your html:form corretly linked to the action form "singleSelectForm"

That's about it, and I must admit that I've not been working a lot with dynamic action forms (damn, one more thing I dont know in Struts :( ...)
Avatar of ayeen

ASKER

thanks again for your reply.
yes, the html:select is enclosed in html:form
as to your 2nd question, my current action is linked to displaydriverdetails.do which is calling processFilterFormBean and not singleSelectForm. the processFilterFormBean contains all my getter/setter methods for the other form fields in my jsp. but just the same, i tried changing my action to singleSelectForm but it  complains about driverlist: Error 500: Cannot find bean driverlist in any scope

i'm sorry i'm kinda lost...


----------------------
STRUTS CONFIG ENTRIES
----------------------
<struts-config>
<form-beans>
     <form-bean name="singleSelectForm"
        type="org.apache.struts.action.DynaActionForm">
                       <form-property name="control" type="java.lang.String"/>
     </form-bean>
     <form-bean name="processFilterFormBean" type="com.webtest.resources.ProcessFilterFormBean">
      </form-bean>
</form-beans>    
<action-mappings>
      <action path="/displaydriverdetails" type="com.webtest.resources.ProcessFilterAction" name="processFilterFormBean" scope="session">
     <forward name="filter" path = "/WEB-INF/pages/ProcessFilter.jsp"/>            
</action>
</action-mappings>
</struts-config>

----------------------
JSP code
----------------------
<html:form action="displaydriverdetails.do" method="post">
<html:select property="control" size="2">
  <logic:iterate id="driver" name="driverlist">
    <option value="<bean:write name="driver" property="selecteddriverid"/>">
      <bean:write name="driver" property="drivername"/>
    </option>
  </logic:iterate>
</html:select>
 Enter Entity ID# : <html:text property="entityid" name="processFilterFormBean" disabled="false"/>
...and more form fields here...
</html:form>


---------------------------------------
here's my Form Bean:
---------------------------------------
public class ProcessFilterFormBean extends ActionForm {
      private Collection driverlist;
      private String[] selecteddriverid;

      private String driverid = "";
      private String drivername = "";
      private String entityid = "";

      // for driver list
      public void setDriverlist(Collection driverlist){
            this.driverlist = driverlist;            
      }

      public Collection getDriverlist(){
            return driverlist;
      }
     
      public String[] getSelecteddriverid(){
            return selecteddriverid;
      }
     
      public void setSelecteddriverid(String[] driverid){
            this.selecteddriverid = driverid;
      }      

     public String getEntityid(){            
        return entityid;            
     }
      
     public void setEntityid(String entityid){            
        this.entityid = entityid;
    }      
     
     
      public void reset(ActionMapping mapping, HttpServletRequest request) {
            driverlist = new ArrayList();

      }

      public ActionErrors validate(
            ActionMapping mapping,
            HttpServletRequest request) {

            ActionErrors errors = new ActionErrors();
            return errors;
      }
}




---------------------------------------
here's my action class:
---------------------------------------
public class ProcessFilterAction extends Action {

      public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {
                       
            ActionErrors errors = new ActionErrors();
            ActionForward forward = new ActionForward();

            ProcessFilterFormBean pfbean = (ProcessFilterFormBean) form;
            String form_entityid = request.getParameter("entityid");
            String form_driver = request.getParameter("???"); //how to call the form field for this?
           
            //calls the DB to load drivers
            SimulateDB simulateDB = new SimulateDB();
            pfbean.setDriverlist(simulateDB.getDriverlist(request.getSession()));  
            return (mapping.findForward("start"));
      }
}

...codes for SimulateDB is on my first post...
thank you so much again..

Avatar of ayeen

ASKER

i had it working!

looks like i messed up the <logic:iterate tag

here's the working jsp entry for my pulldown
<html:select property="selecteddriverid" size="1">
  <logic:iterate name="processFilterFormBean" property="driverlist" id="driver">    
    <option value="<bean:write name="driver" property="selecteddriverid"/>">
      <bean:write name="driver" property="drivername"/>
    </option>
  </logic:iterate>
</html:select>

thanks for the patience and effort Jathrek!
Bah, sorry for taking so long to solve this problem :(

I hope you'll be able to continue improve your knowledge in Struts without too many problems ^^