We help IT Professionals succeed at work.

struts problem: pulldown menu

ayeen
ayeen asked
on
3,370 Views
Last Modified: 2013-11-24
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;
      }

}
Comment
Watch Question

Commented:
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

Author

Commented:
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..


Commented:
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)...

Author

Commented:
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

Commented:
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 :(

Author

Commented:
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!


Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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....

Commented:
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 :( ...)

Author

Commented:
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..

Author

Commented:
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!

Commented:
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 ^^

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.