You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Poon, Johnny" <Jo...@ANICO.com> on 2003/06/10 00:30:19 UTC

Best ways to populate an for a JSP?

Hi,

I'm populating a form bean with a vector of label/value option pairs within
an Action and store in the request.  I then forward it to the targeted JSP,
and the <html:select> and <html:optionsCollection> tags within the
<html:form> will be populated with the values (label/value) in the vector.

I'm able to achieve that and populate my <html:optionsCollection>
successfully.  However, I am having to hard code the name of the form bean
in my Action code.  I don't really like it that way, because if I ever
change the name of the form bean in the strut-config.xml, I will have to
change the Action code as well.

Is this a common practice?  Does anyone have a better approach, or know
what's the best practice regarding this?

The following are the simplified code for your reference:


strut-config.xml:
================
<struts-config>

  <form-beans>
    <form-bean name="baseForm" type="com.forms.BaseForm" />
    <form-bean name="stateListForm" type="com.forms.StateListForm" />
  </form-beans>

  <action path="/checkState" type="com.actions.CheckStateAction"
name="baseForm" scope="request">
    <forward name="selectState" path="/selectState.dhtml" />
    <forward name="newBasicQ1" path="/newBasicQ1.dhtml" />
  </action>

  <action path="/enterBasicQ1" type="com.actions.SelectStateAction"
name="stateListForm"  scope="request">
    <forward name="oldBasicQ1" path="/oldBasicQ1.dhtml" />
    <forward name="newBasicQ1" path="/newBasicQ1.dhtml" />
  </action>

</struts-config>


CheckStateAction.java:
======================
public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
      throws IOException, ServletException
{
  ActionForward result = null;

  //To generate the list in the <option>
  Vector optionVec = StateZipUtil.getStateList(true);

  StateListForm slf = new StateListForm();
  slf.setStateList(optionVec);

  //This is where I have to make sure the request 
  //attribute name matches the form-beam name:
  request.setAttribute("stateListForm", slf);

  return (mapping.findForward("selectState"));
}


Please let me know if you would like me to clearify anything in my post.
Thanks in advance.

JP.







---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org