I'm also a newbie, but here is an example of what I found by getting
some help from the people on the list plus experimentation. Here is
a helper class I wrote:

package fi.els.form;

import java.util.*;
import org.apache.struts.util.LabelValueBean;

public class CreditCardOptions
{
public static Collection getCardOptions()
{
Vector creditCardOptions = new Vector();
creditCardOptions.add(new LabelValueBean("MasterCard", "mc"));
creditCardOptions.add(new LabelValueBean("Visa", "visa"));
creditCardOptions.add(new LabelValueBean("American Express", "amex"));
creditCardOptions.add(new LabelValueBean("Discover", "discover"));
return creditCardOptions;
}

public static Collection getMonthOptions()
{
Vector monthOptions = new Vector();
monthOptions.add(new LabelValueBean("01", "01"));
monthOptions.add(new LabelValueBean("02", "02"));
monthOptions.add(new LabelValueBean("03", "03"));
monthOptions.add(new LabelValueBean("04", "04"));
monthOptions.add(new LabelValueBean("05", "05"));
monthOptions.add(new LabelValueBean("06", "06"));
monthOptions.add(new LabelValueBean("07", "07"));
monthOptions.add(new LabelValueBean("08", "08"));
monthOptions.add(new LabelValueBean("09", "09"));
monthOptions.add(new LabelValueBean("10", "10"));
monthOptions.add(new LabelValueBean("11", "11"));
monthOptions.add(new LabelValueBean("12", "12"));
return monthOptions;
}

public static Collection getYearOptions()
{
Vector yearOptions = new Vector();
Calendar calendar = Calendar.getInstance();
int y = calendar.get(Calendar.YEAR);

for (int i = 0; i < 12; ++i)
{
Integer year = new Integer(y + i);
String ys = year.toString();
yearOptions.add(new LabelValueBean(ys, ys));
}

return yearOptions;
}
}

Here, I am using it. Since the choices are static, I put it in the
session. I suppose I could have put it in application context.
Anyway, here is a JSP snippet that uses it:

<%
// Create the credit card options (if not already in the session) for
// the credit card select widgets.

if (request.getSession().getAttribute("creditCardOptions") == null)
request.getSession().setAttribute("creditCardOptions",
fi.els.form.CreditCardOptions.getCardOptions());

if (request.getSession().getAttribute("creditCardExpirationMonthOptions") == null)
request.getSession().setAttribute("creditCardExpirationMonthOptions", fi.els.form.CreditCardOptions.getMonthOptions());


if (request.getSession().getAttribute("creditCardExpirationYearOptions") == null)
request.getSession().setAttribute("creditCardExpirationYearOptions", fi.els.form.CreditCardOptions.getYearOptions());
%>
<tr>
<td class="required-formlabel"><bean:message key="billingPrefWidgetsSubForm.label.cardAndExpiration"/>:</td>
<td align="left">
<html:select property="billingPrefWidgetsSubFormType">
<html:options collection="creditCardOptions"
property="value" labelProperty="label"/>
</html:select>


<html:select property="billingPrefWidgetsSubFormExpireMonth">
<html:options collection="creditCardExpirationMonthOptions"
property="value" labelProperty="label"/>
</html:select>

<html:select property="billingPrefWidgetsSubFormExpireYear">
<html:options collection="creditCardExpirationYearOptions"
property="value" labelProperty="label"/>
</html:select>
</td>
</tr>

Hope this helps. I know how frustrating this stuff can be...
Dean Hoover

ddd ddd wrote:

Hi Timo

I am new bie and learning to populate the drop box by all different ways . no body has replied for alomst a 4-5 days pl. help me...

population of data by different ways which are as below 1. By Collections of strings 2. By collections beans 3. Hard coding I am unable to achieve even first way tried a lot but failed can any body suggest me where I am wrong. Also pl. suggest me how the “collection of beans” will be coded . any body has simple java nd jsp code please Post me


Manay many thanks



Regrds StrutsGuy
[EMAIL PROTECTED]



Below is Jps ,stutsconfig and java files and errors of browser .


<%@ page language="java" import="StudentForm" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<jsp:useBean id="sf" class="StudentForm" scope="session" > </jsp:useBean>
<html:html>
<head>
<title><bean:message key="hello"/> </title>
</head>
<body>
<B>LOGIN PAGE <B>
<html:errors />
<html:form action="/LoginForm" focus="studentid" onsubmit="return validateStudentForm(this);"> <table>
<html:select property="passwd" >
<html:options name="sf" labelName="sf" />
</html:select>
<td>LOGIN ID</td>
<td><html:text property="student"/></td>
<td>PASSWORD</td>
<td><html:text property="passwd"/> </td>
</table>
<html:submit><bean:message key="button"/></html:submit>
<html:reset><bean:message key="reset"/></html:reset>
<html:cancel><bean:message key="cancel" /></html:cancel>
</html:form>
<html:javascript formName="studentForm" dynamicJavascript="true" staticJavascript="true"/>
<script language="Javascript1.1" src="staticJavascript.jsp"></script>
</body>
</html:html>



import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm; import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import javax.servlet.jsp.PageContext;


// use The seSSion stuf
public class StudentAction extends Action
{
public ActionForward execute( ActionMapping map,ActionForm form, HttpServletRequest req , HttpServletResponse res) throws Exception
{
String action = req.getParameter("action");
if (action == null)
{ StudentDatabase stBase = new StudentDatabase ();
StudentForm sf= new StudentForm();
String[] str = stBase.getAllNames();
String strng ="blah" ;
sf.setPasswd(strng);
req.setAttribute("sf", sf);
ActionErrors er= new ActionErrors();
er.add(ActionErrors.GLOBAL_ERROR , new ActionError("errorRakesh"));
if(!er.empty()) {
saveErrors(req,er);
return(map.findForward("RakyError"));
}
else
return(map.findForward("RakyCancel")); }
else
{ return(map.findForward("RakyCancel")); }
}// end of class
}


import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.*;
import org.apache.struts.validator.ValidatorForm;

public class StudentForm extends ValidatorForm
{
private int student =0;
private String passwd = null;
public StudentForm()
{
student=0;
passwd = new String();
}
public StudentForm( int student ,String passwd)
{
student=student;
passwd= passwd; }
public int getStudent()
{
return this.student;
}
public void setStudent(int student)
{
this.student=student; }


public String getPasswd( )
{
return this.passwd;
}
public void setPasswd(String passwd)
{
this.passwd=passwd; }
public void reset(ActionMapping mapping ,HttpServletRequest request)
{
this.student=0; this.passwd=null;
}
public ActionErrors validate( ActionMapping map,HttpServletRequest req)
{
ActionErrors errors=super.validate(map,req);
if(student == 0)
{
System.out.println("HELLO blah "+student);
errors.add("student", new ActionError("IDProblem"));
}
return errors;
} // end of action errors
}// end of Student from class



<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd";> <!-- This is the Struts configuration file for the rakesh application --> <struts-config> <!-- Form bean smust be maped here first --> <form-beans> <form-bean name="StudentForm" type="StudentForm"/> </form-beans> <!-- All Action msut be mapped here --> <action-mappings> <!-- Calling a jsp page through link --> <action path="/LoginJsp" forward="/Login.jsp"/> <action path="/LoginForm" type="StudentAction" name="StudentForm" scope="request" validate="true"> <forward name="RakySuccess" path="/Start.jsp"/> <forward name="RakyCancel" path="/Start.jsp"/> <forward name="RakyError" path="/Start.jsp"/> </action> </action-mappings> <message-resources parameter="propertyInfo" /> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml"/> </plug-in> </struts-config>

propertyInfo.properties
hello=This is test page
start=This is first page of the application
info=This page stores contains text files and drop downs error=There is problem please have a look submit=ADD TO DATABASE clear=Clear the Data cancel=Cancel Button
click=Please Click to start the application
button=Login
cancel=CancelButton
reset=ResetButton
IDProblem=There is problem in id has greater value than 4 pl. recheck it prompt.username= Please Enter the User Name by blah
prompt.password=Please enter the password by bla errors.required={0} is required.
errors.minlength={0} cannot be less than {1} characters.
errors.maxlength={0} cannot be greater than {2} characters.
errors.invalid={0} is invalid.
errors.byte={0} must be an byte.
errors.short={0} must be an short.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.float={0} must be an float.
errors.double={0} must be an double.
errors.date={0} is not a date.
errors.range={0} is not in the range {1} through {2}.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
rakeshErROR=This is first ssuccessfyl errro by blah errorRakesh=This is type of error in jsp page



eError is


HTTP Status 500 - ________________________________________
type Exception report
message description The server encountered an internal error () that prevented it from fulfilling this request.
exception org.apache.jasper.JasperException: Cannot create iterator for [EMAIL PROTECTED]
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:432)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:356)
at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1014)
at org.apache.struts.action.RequestProcessor.internalModuleRelativeForward(RequestProcessor.java:961)
at org.apache.struts.action.RequestProcessor.processForward(RequestProcessor.java:544)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:252)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1292)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:492)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2416)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline




On Sun, 14 Mar 2004 Timo Tjäder wrote :


Hi,

Thanks Hubert, the problem was on the html page.

1. I forgot to add second form tag to handle page linking system on footer.
2. Also as you mentioned all the parameters must be match with the form beans I am 
posting.

After making those changes the combox box was redered nicely on same lone
with navigation numbers.

1 2 3 4 5 next COMBOBOX v

I have one rule when writing jsp pages and it is "no scriplets anymore".
The Struts handles everything nicely if some time is offered in designing
the beans and linking system.

In this case mine is

WEB->FORM->ACTION->SERVICE->BEAN
WEB<-FORM<-ACTION<-SERVICE<-BEAN

Service sets the values and they are read from the page using
struts tags (equal/iterate/present). The totalNumOfPagesCollection
comes actually from BEAN.

Thanks,

Timo




The <html:select> needs to be provided with the property it's associated to,
the one on your form bean that will hold the actual value selected.

<html:select property="mySelectValue" style="font-size: 10px;"
property="goTo" size="1">
  <html:optionsCollection
      name="PageDetailView"
      property="totalNumOfPagesCollection"
      />
</html:select>

"mySelectValue" should be a property on your form bean.

hth,

Hubert



--- Timo_Tjäder <[EMAIL PROTECTED]> wrote:


Hi,

I have problems with optionsCollection, when used with <html:select>
custom tag.

1. I set up in a bean LabelValueBean object in a Vector
2. pass it back to form and try to create a <html:selec>

This part should display a combo box with options, but
it just does not work. It does not display anything.

Html code is as follows:

<html:select style="font-size: 10px;" property="goTo" size="1">
<html:optionsCollection name="PageDetailView"
property="totalNumOfPagesCollection"/>
</html:select>

I have checked using struts "bean:write" the contents of
the object. Bean write claims that totalNumOfPages really
contains LabelValueBean-objects (14 peaces).
LabelValueBeanÄ1, 1Å, LabelValueBeanÄ2, 2Å, LabelValueBeanÄ3, 3Å,
...

So, does anyone know how to reference to this object correctly.
Current solution does not diaplay anything to the screen.

Thanks,

Timo

So what I'd like to do is display a selection list of
all availabele pages from which user choose one and press
a goTo page button. This is quite common web control in
any web pages.


.... <logic:equal name="PageDetailView" property="linkType" value="add-total-num-of-pages-collection"> <bean:write name="PageDetailView.totalNumOfPagesCollection" property="LabelValueBean"/>

<html:select style="font-size: 10px;" property="goTo" size="1">
<html:optionsCollection name="PageDetailView"
property="totalNumOfPagesCollection"/>
</html:select>
</logic:equal>



Ä1Å  2 3 4 5 next +  Result 14 pages.
....
ÄLabelValueBeanÄ1, 1Å, LabelValueBeanÄ2, 2Å, LabelValueBeanÄ3, 3Å,
LabelValueBeanÄ4, 4Å, LabelValueBeanÄ5, 5Å, LabelValueBeanÄ6, 6Å,
LabelValueBeanÄ7, 7Å, LabelValueBeanÄ8, 8Å, LabelValueBeanÄ9, 9Å,
LabelValueBeanÄ10, 10Å, LabelValueBeanÄ11, 11Å, LabelValueBeanÄ12,
12Å, LabelValueBeanÄ13, 13Å, LabelValueBeanÄ14, 14ÅÅ


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



__________________________________
Do you Yahoo!?
Yahoo! Mail - More reliable, more storage, less spam
http://mail.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]









--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to