• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

html optionsCollection Faild to obtain Collection

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have written following code in struts 1.2

test.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib uri="/tlds/c.tld" prefix='c'%>
<%@ taglib uri="/tlds/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/tlds/struts-html.tld" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>
<%@ taglib prefix="s" uri="/tlds/struts-tags.tld" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<html:form action="/testAction">

<html:select property="selectedItem">
<html:optionsCollection value="id" name="testForm" label="name" property="budgetHeadList"/>
</html:select>

</html:form>
</body>
</html>


testForm.java

package com.gramlekha.budget.ActionForm;

import java.util.ArrayList;

import org.apache.struts.action.ActionForm;

import com.gramlekha.bean.BudgetheadBean;

public class testForm extends ActionForm{

private String selectedItem;

private ArrayList budgetHeadList;


public String getSelectedItem() {
return selectedItem;
}

public void setSelectedItem(String selectedItem) {
this.selectedItem = selectedItem;
}


public ArrayList getBudgetHeadList() {
return budgetHeadList;
}


public void setBudgetHeadList(ArrayList budgetHeadList) {
this.budgetHeadList=budgetHeadList;
}


}


testAction.java

package com.gramlekha.budget.Action;

import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


import com.gramlekha.bean.BudgetheadBean;
import com.gramlekha.budget.ActionForm.testForm;

public class testAction extends Action{

public ActionForward execute(ActionMapping mapping,
ActionForm form,HttpServletRequest request,HttpServletResponse response){

testForm test=(testForm) form;

ArrayList<BudgetheadBean> budgetheads=new ArrayList<BudgetheadBean>();

budgetheads.add(new BudgetheadBean(1,"head 1"));
budgetheads.add(new BudgetheadBean(2,"head 2"));
budgetheads.add(new BudgetheadBean(3,"head 3"));
budgetheads.add(new BudgetheadBean(4,"head 4"));
budgetheads.add(new BudgetheadBean(5,"head 5"));

System.out.println("List Created: "+budgetheads);

test.setBudgetHeadList(budgetheads);

return mapping.findForward("success");


}

}


struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources />
<form-beans>
<form-bean name="testForm" type="com.gramlekha.budget.ActionForm.testForm"></form-bean>
</form-beans>

<global-exceptions>
</global-exceptions>
<global-forwards>
</global-forwards>

<action-mappings>

<action
path="/testAction"
type="com.gramlekha.budget.Action.testAction"
name="testForm"
scope="session"
validate="true"
input="/jsp/Budget/test.jsp">
<forward name="success" path="/jsp/Budget/test.jsp"></forward>
</action>


</action-mappings>

<message-resources parameter="resource.ApplicationResources" />
<plug-in className="org.apache.struts.tiles.TilesPlugin">
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
<set-property property="definitions-parser-validate" value="true" />

</plug-in>

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/Validation/validator-rules.xml,/WEB-INF/Validation/validation.xml"/>
</plug-in>


<plug-in className="net.sf.navigator.menu.MenuPlugIn">
<set-property property="menuConfig" value="/WEB-INF/menu-config.xml" />
</plug-in>
</struts-config>


BudgetheadBean.java

package com.gramlekha.bean;

public class BudgetheadBean {

private Integer id;
private String name;


public BudgetheadBean(){

}

public BudgetheadBean(Integer id,String name){
this.id=id;
this.name=name;

}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
};

public String getLabel(){
return name;
}

public Integer getValue(){
return id;
}
}



Showing Error
javax.servlet.ServletException: Failed to obtain specified collection
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.jsp.Budget.test_jsp._jspService(test_jsp.java:92)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:416)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:75)

root cause

javax.servlet.jsp.JspException: Failed to obtain specified collection
org.apache.struts.taglib.html.OptionsCollectionTag.doStartTag(OptionsCollectionTag.java:178)
org.apache.jsp.jsp.Budget.test_jsp._jspx_meth_html_optionsCollection_0(test_jsp.java:178)
org.apache.jsp.jsp.Budget.test_jsp._jspx_meth_html_select_0(test_jsp.java:149)
org.apache.jsp.jsp.Budget.test_jsp._jspx_meth_html_form_0(test_jsp.java:114)
org.apache.jsp.jsp.Budget.test_jsp._jspService(test_jsp.java:82)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:416)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:75)

note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.




Whats the problem.??can anyone help


 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vrushali chaskar wrote:
Showing Error
javax.servlet.ServletException: Failed to obtain specified collection
root cause
javax.servlet.jsp.JspException: Failed to obtain specified collection
org.apache.struts.taglib.html.OptionsCollectionTag.doStartTag(OptionsCollectionTag.java:178)


First of all,
UseCodeTags
IsolateTheProblem

Now exception, says you have no attribute defined for html:optionsCollection tag

You will need bean named "testForm" which has collection property "budgetHeadList" in any scope, also read this for details

http://struts.apache.org/1.3.10/struts-taglib/tagreference.html#html:optionsCollection
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Make sure this action forwards to the JSP, and then call the URL for the action (/myaction.do) rather than calling the JSP directly (/mypage.jsp).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic