h:selectOneMenu – Java Server Faces (JSF)

  • Topic : Java Server Faces (JSF)
  • Environment : J2EE 5.0, MyFaces 1.1.5

selectOneMenu.jsp

also read:

<body>
	<f:view>
		<h:form id='select'>
			<h:panelGrid columns='1'>
				<h:column>
					<h:outputText value='Select a Name : '/>
					<h:selectOneMenu value='#{selectOneBean.name}'>
					<f:selectItem itemValue='SteveJobs' value='SteveJobs'/>
					<f:selectItem itemValue='Sergey Brin' value='Sergey Brin'/>
					<f:selectItem itemValue='Larry Page' value='Larry Page'/>
					<f:selectItem itemValue='Dell' value='Dell'/>
					<f:selectItem itemValue='Mark Anderson' value='Mark Anderson'/>
				</h:selectOneMenu>
			</h:column>
			<h:column>
				<h:commandButton value='Submit' action='#{selectOneBean.submit}'/>
			</h:column>
		</h:panelGrid>
	</h:form>
</f:view>
</body>
</html>

SelectOneMenuBean.java

package net.javabeat.myfaces.menu;

public class SelectOneMenuBean {
	private String name;

	public String getName() {
		return name;
	}

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

	public String submit() {
		return "selectOneMenu";
	}
}

faces-config.xml

<faces-config>
	<managed-bean>
		<managed-bean-name> selectOneBean </managed-bean-name>
		<managed-bean-class> net.javabeat.myfaces.menu.SelectOneMenuBean </managed-bean-class>
		<managed-bean-scope> request </managed-bean-scope>
	</managed-bean>
	<navigation-rule>
		<navigation-case>
			<from-outcome> selectOneMenu </from-outcome>
			<to-view-id> /pages/menu/selectOneMenuResult.jsp </to-view-id>
		</navigation-case>
	</navigation-rule>
</faces-config>

selectOneMenuResult.jsp

<html>
	<body>
		<f:view>
			<h:outputText value=&quot;Selected Name is : &quot;/>
			<h:outputText value=&quot;#{selectOneBean.name}&quot;/>
		</f:view>
	</body>
</html>

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Comments

  1. sucheta says:

    hi in above examople when i try to access name value i am getting null pointer exception

Speak Your Mind

*