I have a problem in html:select option

I need to retrieve the value from the dropdown list of html:select from the formbean .As of now its populated with hardcoded valuesAnyhelp appreciated
[171 byte] By [janava] at [2007-11-15]
# 1

In my Jsp page I have the code like this

<html:select name="county" property="selectedCounty">

<html:option value="Alleghany" />

<html:option value="Ashe" />

<html:option value="Pasquotank" />

<html:option value="Peraquimmons" />

<html:option value="Wake" />

<html:option value="Wayne" /></html:select>

I need to setter and getter methods for this options

janava at 2007-7-12 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 2

Use html:optionsCollection to retrieve the values from the backing bean.

<html:select property="selectedCounty">

<html:option value="" />

<html:optionsCollection property="countiesList" />

</html:select>

Where 'countiesList' is a collection type variable in the backing bean with getters and setters.

SirG

SirGenerala at 2007-7-12 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 3
Thanks for your time.I did that but this time I got different exceptionweblogic.servlet.jsp.JspException: (line 51): Non-matching extension tags
janava at 2007-7-12 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 4

I made the changes to the jsp file ...but this time its throwing the following error.

help me ..

javax.servlet.jsp.JspException: No getter method available for property selectedCounty for bean under name org.apache.struts.taglib.html.BEAN

at org.apache.struts.taglib.html.SelectTag.calculateMatchValues(SelectTag.java:327)

at org.apache.struts.taglib.html.SelectTag.doStartTag(SelectTag.java:244)

at jsp_servlet.__contract._jspService(__contract.java:245)

at weblogic.servlet.jsp.JspBase.service(JspBase.java:33)

at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1006)

at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:419)

at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:463)

at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:315)

at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:322)

at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)

at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:455)

at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:279)

at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)

at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1006)

at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:419)

at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:315)

at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6718)

at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)

at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)

at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3764)

at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2644)

at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:219)

at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:178)

janava at 2007-7-12 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 5

In your form object for the page add a

private List counties;

with a getter and setter.

Then, in your Action class create a Vector full of LabelValueBean objects (you should have them as part of your struts application) and set the Vector into you form.

Then, on you page do

<html:select name="country" property="selectedCounty">

<html:optionsCollection property="counties" />

</html:select>

den2681a at 2007-7-12 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 6

javax.servlet.jsp.JspException: No getter method available for property selectedCounty for bean under name org.apache.struts.taglib.html.BEAN

That clearly implies that you don't have a form bean property for 'selectedCounty'. Define a variable 'selectedCounty' in your backing bean and generate getters and setters for the same and check it out.

private String selectedCounty;

//Getter and Setter

SirG

SirGenerala at 2007-7-12 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 7

Thanks for responding.

Let me show you my code:

In Jsp :<html:select property="selectedCounty">

<html:option value="Alleghany" />

<html:option value="Ashe" />

<html:option value="Pasquotank" />

<html:option value="Peraquimmons" />

<html:option value="Wake" />

<html:option value="Wayne" />

<html:optionsCollection property="countiesList" />

</html:select>

In ActionForm:

public String[] getSelectedCounty() {

return selectedCounty;

}

/**

* @param selectedCounty The selectedCounty to set.

*/

public void setSelectedCounty(String[] selectedCounty) {

this.selectedCounty = selectedCounty;

}

/**

* @return Returns the county.

*/

public List getCountiesList() {

return countiesList;

}

/**

* @param county The county to set.

*/

public void setCountiesList(List countiesList) {

this.countiesList = countiesList;

}

I am new to struts. I dont know how to fix this. I have hardcoded the list in the jsp. and wanted to create a form bean.

janava at 2007-7-12 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 8
Thanks let me try this ..SirGeneral ....
janava at 2007-7-12 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 9

I am getting the following exception.....

javax.servlet.jsp.JspException: Failed to obtain specified collection

at org.apache.struts.taglib.html.OptionsCollectionTag.doStartTag(OptionsCollectionTag.java:222)

at jsp_servlet.__contract._jspService(__contract.java:361)

at weblogic.servlet.jsp.JspBase.service(JspBase.java:33)

at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1006)

at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:419)

at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:463)

at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:315)

at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:322)

at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)

at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:455)

at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:279)

at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)

at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1006)

at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:419)

at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:315)

at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6718)

at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)

at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)

at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3764)

at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2644)

at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:219)

at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:178)

janava at 2007-7-12 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 10

private String selectedCounty;

public String getSelectedCounty() {

return selectedCounty;

}

public void setSelectedCounty(String selectedCounty) {

this.selectedCounty = selectedCounty;

}

Just define selectedCounty as a String. You need this variable, basically to fetch the value selected from the dropdown by the user. This will be populated only when the user selects a value and submits it to the action.

SirG

SirGenerala at 2007-7-12 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 11

I made the changes ..

I have added

/**

* @return Returns the selectedCounty.

*/

public String getSelectedCounty() {

return selectedCounty;

}

/**

* @param selectedCounty The selectedCounty to set.

*/

public void setSelectedCounty(String selectedCounty) {

this.selectedCounty = selectedCounty;

}

but its throwing an exception

javax.servlet.jsp.JspException: Failed to obtain specified collection

at org.apache.struts.taglib.html.OptionsCollectionTag.doStartTag(OptionsCollectionTag.java:222)

janava at 2007-7-12 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 12
show us your updated .jsp code (the part where you define the html:select and options)
den2681a at 2007-7-12 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 13

<html:select property="selectedCounty">

<html:option value="Alleghany" />

<html:option value="Ashe" />

<html:option value="Pasquotank" />

<html:option value="Peraquimmons" />

<html:option value="Wake" />

<html:option value="Wayne" />

<html:optionsCollection property="countiesList" />

</html:select>

janava at 2007-7-12 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 14

public static Collection getCountiesList(){

ArrayList countiesList = new ArrayList();

Map option = new HashMap();

option.put("label", "Alleghany");

option.put("value", "Alleghany");

countiesList.add(option);

option = new HashMap();

option.put("label", "Ashe");

option.put("value", "Ashe");

countiesList.add(option);

option = new HashMap();

option.put("label", "Pasquotank");

option.put("value", "Pasquotank");

countiesList.add(option);

option = new HashMap();

option.put("label", "Peraquimmons");

option.put("value", "Peraquimmons");

countiesList.add(option);

option = new HashMap();

option.put("label", "Wake");

option.put("value", "Wake");

countiesList.add(option);

option = new HashMap();

option.put("label", "Wayne");

option.put("value", "Wayne");

options.add(option);

return options;

}

Replace your getCountiesList getter with the above code and check if you are still getting the same exception.

Also make the following changes to your jsp:

<html:select property="selectedCounty">

<html:optionsCollection property="countiesList" />

</html:select>

SirG

SirGenerala at 2007-7-12 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 15
Do I need to remove the getter and setter for counties list ?and just place the above code.
janava at 2007-7-21 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 16

Code In Action form

/**

* @return Returns the county List.

*/

public static Collection getCountiesList(){

ArrayList countiesList = new ArrayList();

Map option = new HashMap();

option.put("label", "Alleghany");

option.put("value", "Alleghany");

countiesList.add(option);

option = new HashMap();

option.put("label", "Ashe");

option.put("value", "Ashe");

countiesList.add(option);

option = new HashMap();

option.put("label", "Pasquotank");

option.put("value", "Pasquotank");

countiesList.add(option);

option = new HashMap();

option.put("label", "Peraquimmons");

option.put("value", "Peraquimmons");

countiesList.add(option);

option = new HashMap();

option.put("label", "Wake");

option.put("value", "Wake");

countiesList.add(option);

option = new HashMap();

option.put("label", "Wayne");

option.put("value", "Wayne");

countiesList.add(option);

return countiesList;

}

/**

* @param county The county to set.

*/

public void setCountiesList(ArrayList countiesList) {

this.countiesList = countiesList;

}

/**

* @return Returns the selectedCounty.

*/

public String getSelectedCounty() {

return selectedCounty;

}

/**

* @param selectedCounty The selectedCounty to set.

*/

public void setSelectedCounty(String selectedCounty) {

this.selectedCounty = selectedCounty;

}

Code in Jsp :

<html:select property="selectedCounty">

<html:optionsCollection property="countiesList" />

</html:select>

But I am getting the following Exception:

;javax.servlet.jsp.JspException: No getter method for property countiesList of bean org.apache.struts.taglib.html.BEAN

at org.apache.struts.util.RequestUtils.lookup(RequestUtils.java:968)

at org.apache.struts.taglib.html.OptionsCollectionTag.doStartTag(OptionsCollectionTag.java:219)

at jsp_servlet.__contract._jspService(__contract.java:265)

at weblogic.servlet.jsp.JspBase.service(JspBase.java:33)

--

I tried changing the return type of getCountiesList from Collection to ArrayList.

Nothing worked

janava at 2007-7-21 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 17
Do not place this code in action class. Just replace your getCountiesList method in your form bean with this method. Check at least in this case if its able to retrieve the collection object.
SirGenerala at 2007-7-21 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 18
May be I am not clear ..I did placed the code in ActionForm.
janava at 2007-7-21 > top of java,Enterprise & Remote Computing,Web Tier APIs...