Skip to content

Instantly share code, notes, and snippets.

@franzwong
Created October 2, 2012 07:13
Show Gist options
  • Save franzwong/3816986 to your computer and use it in GitHub Desktop.
Save franzwong/3816986 to your computer and use it in GitHub Desktop.
Jsf cheatsheet
  • usage of h:selectOneMenu
<h:selectOneMenu value="#{manageStudent.student.gender}">
    <f:selectItem itemLabel="- Select Gender -" itemValue=""/>
    <f:selectItems value="#{manageStudent.genders}"/>
</h:selectOneMenu>
  • get HttpServletRequest and HttpServletResponse in managed bean
HttpServletRequest request = 
	(HttpServletRequest)FacesContext
		.getCurrentInstance()
		.getExternalContext()
		.getRequest();

HttpServletResponse response = 
	(HttpServletRequest)FacesContext
		.getCurrentInstance()
		.getExternalContext()
		.getResponse();
  • include another xhtml page

main.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:ui="http://java.sun.com/jsf/facelets">
	<h:body>
		<ui:include src="content.xhtml"/>
	</h:body>
</html>

content.xhtml

<ui:composition 
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <h2>Hello</h2>
</ui:composition>
  • create template for xhtml

template.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:ui="http://java.sun.com/jsf/facelets">
	<h:head>
        <title><ui:insert name="title">Title here</ui:insert></title>
    </h:head>
	<h:body>
		<ui:insert name="content">Content here</ui:insert>
	</h:body>
</html>

welcome.xhtml

<ui:composition template="template.xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">

    <ui:define name="title">
        Welcome!!
    </ui:define>

    <ui:define name="content">
        <p>Hello World!!!</p>
    </ui:define>
</ui:composition>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment