Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSF

 
Sivarama Raju
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,

Can any one please guide me to create a dropdown using JSF ?

Thanks and regards,
 
Arun Thulaseedharan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think you can make use of <h:selectOneMenu> tag available in jsf for this....

<h:selectOneMenu value="#{form.day}">
<f:selectItem itemValue="Sunday" itemLabel="Sunday"/>
<f:selectItem itemValue="Monday" itemLabel="Monday"/>
<f:selectItem itemValue="Tuesday" itemLabel="Tuesday"/>
<f:selectItem itemValue="Wednesday" itemLabel="Wednesday"/>
<f:selectItem itemValue="Thursday" itemLabel="Thursday"/>
<f:selectItem itemValue="Friday" itemLabel="Friday"/>
<f:selectItem itemValue="Saturday" itemLabel="Saturday"/>
</h:selectOneMenu>

supposing that you want the value selected to go to the backing bean this code snippet is fine i guess.. here the value goes to the backing bean "form" and updates the property "day"....n for making this drop down we can make use of either <f:selectItem> when the list in the drop down is less and <f:selectItems> when the list in the drop down is more and you need to populate that from a backing bean......hope this answer your question...
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18152
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also provide the selectItems as a list from a backing bean.

Note: please try to be more specific with your topic titles - it attracts people who are better able to help you when they know what the subject of the question will be without having to open up the message.
 
Sivarama Raju
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arun,

Thanks a lot for you reply friend. I will try using <h:selectOneMenu>.
I will inform you about my progress. Thanks a lot.

Thanks and regards,
siva
 
Sivarama Raju
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, thanks Tim.
 
Sivarama Raju
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Arun,

I tried a lot with the Dropdown, but couldn't give action to the submit button, for the items in dropdown. This is my code:

JSF:
<h:selectOneMenu value="#{CalcBean.selectValue}">
<f:selectItem itemLabel="Add" itemValue="SubmitAdd" />
<f:selectItem itemLabel="Multiply"itemValue="SubmitMultiply" />
<f:selectItem itemLabel="Subtract" itemValue="SubmitDivide" />
<f:selectItem itemLabel="Divide" itemValue="SubmitDivide"/>
</h:selectOneMenu>

<h:commandButton action="#{CalcBean.selectChoice}" "Submit"></h:commandButton>

Backing bean:

public String selectChoice() {
String returnVal = "";
if (selectValue == "SubmitAdd") {
returnVal = add();
} else if (selectValue == "SubmitMultiply") {
returnVal = multiply();
} else if (selectValue == "SubmitSutract") {
returnVal = subtract();
} else if (selectValue == "SubmitDivide") {
returnVal = divide();
}
return returnVal;
}


public String getSelectChoice() {
return selectChoice;
}

public void setSelectValue(String aSelectValue) {
this.selectValue = aSelectValue;
}

public String getSelectValue() {
return selectValue;
}


Hey i am new yaar, might have dont some silly mistakes.

Thanks and regards,
siva
[ May 22, 2008: Message edited by: Sivarama Raju ]
 
Arun Thulaseedharan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my first page....
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

<f:view>

<head>
<title><hutputText value="ProjectTrack" /></title>
</head>
<body>
<h:form>
<h:selectOneMenu value="#{calcBean.selectValue}">
<f:selectItem itemLabel="Add" itemValue="SubmitAdd" />
<f:selectItem itemLabel="Multiply" itemValue="SubmitMultiply" />
<f:selectItem itemLabel="Subtract" itemValue="SubmitSubtract" />
<f:selectItem itemLabel="Divide" itemValue="SubmitDivide" />
</h:selectOneMenu>

<h:commandButton value="Submit" type="submit" action="#{calcBean.selectChoice}" />
</h:form>
</body>
</f:view>
</html>

i think you dint put the tags inside a h:form...i guess only then you will be able to include that command button.....
 
Sivarama Raju
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<?xml version="1.0" encoding="ISO-8859-1" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core" version="2.0">

<ui:composition>
<h:form id="CalcForm">
<h:panelGrid columns="3">
<hutputLabel value="First Number" for="firstNumber" />
<h:inputText id="firstNumber" value="#{CalcBean.firstNumber}"
required="true" />
<h:message for="firstNumber" />

<hutputLabel value="Second Number" for="secondNumber" />
<h:inputText id="secondNumber" value="#{CalcBean.secondNumber}"
required="true" />
<h:message for="secondNumber" />
</h:panelGrid>
<h:panelGroup>

<h:selectOneMenu value="#{CalcBean.selectChoice}">

<f:selectItem itemLabel="Add" itemValue="SubmitAdd" />
<f:selectItem itemLabel="Multiply" itemValue="SubmitMultiply" />
<f:selectItem itemLabel="Subtract" itemValue="SubmitDivide" />
<f:selectItem itemLabel="Divide" itemValue="SubmitDivide"/>

</h:selectOneMenu>

<h:commandButton action="#{CalcBean.selectValue}" value="Submit"></h:commandButton>


</h:panelGroup>
</h:form>
</ui:composition>
</jsp:root>

Hi Arun
this is my dropdown.jspx page
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18152
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NOTE

Sivarama has reposted the question in this thread: http://www.coderanch.com/t/214027/JSF/java/Dropdown-created-has-list-items

It can be confusing when the same question is being answered by in different threads by people who don't know what other people are saying, so we ask that anyone wishing to say anything further on the topic respond on the new thread listed above.

Paul, could you close this thread, please? Sivarama's just taking my advice to use a more specific topic name, but since the topic was already started, we've ended up with this unfortunate situation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic