• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error while working with JSF SelectOneMenu and JSTL forEach Tag

 
Saravanan Vijayappan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am new to JSF. I have set up few things in ArrayList in my contextListener as like below

ServletContext context=event.getServletContext();
List list=new ArrayList();
list1.add("Honda");
list1.add("Mazda");
list1.add("eClipse");
context.setAttribute("carList", list)

I just wanted to display the list in my JSF page, I wrote the below statements in my JSF page


<h:selectOneMenu value="{com.carSelection}">
<c:forEach var="car" items="${applicationScope.carList}" >
<f:selectItem itemLabel="${car}"/>
</c:forEach>
</h:selectOneMenu>

I am getting the below exception

org.apache.jasper.JasperException: /pages/protected/cmt/index.jsp(23,2) According to TLD or attribute directive in tag file, attribute itemLabel does not accept any expressions
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:148)
org.apache.jasper.compiler.Validator$ValidateVisitor.checkXmlAttributes(Validator.java:1124)
org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:819)
org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1512)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2343)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2393)
org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:838)
org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1512)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2343)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2393)
org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator


Could some one help to solve this error (Or) How should I display from my applicationScope list into MenuItems ?
 
A. Dusi
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are mixing up JSTL and JSF tags. Not sure if this will work.
One way to do this is to use f:selectItems instead of c:foreach and f:selectItem, but you should create a SelectItem List from your carsList.
Another way if you are using Tomahawk is to use t:selectItems in which case you can directly use carsList. Tomahawk will convert the list to SelectItem list for you. Check out the MyFaces documentation and the wiki page.

 
Saravanan Vijayappan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply.

Unfortunately, In JSF 1.2 RI, the UI <f:selectItems> does not have the attribute "var", so, the below code does not work. I think, this can't be done JSF1.2 UI. As you suggested, I might need to add Tomahawk into my application. Please let me know if there are any other solutions. I am using richfaces with JSF1.2 RI

===============================================================
<h:selectOneMenu value="{com.carSelection}">
<f:selectItems var="#{applicationScope.carsSelectItemList}" />
</h:selectOneMenu>

or

<h:selectOneMenu value="{com.carSelection}">
<t:selectItems var="#{applicationScope.carList}" />
</h:selectOneMenu>
==================================================================
 
A. Dusi
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant to say 'value' not 'var'. Try it again now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!