





Vytvoření projektu:
mvn archetype:create -DarchetypeArtifactId=softeu-archetype-jsf -DremoteRepositories=http://maven.softeu.cz/ -DgroupId=cz.softeu.test -DartifactId=helloApp


public class HelloBean { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title><ui:insert name="title"/> - helloApp </title>
</head>
<body>
<ui:insert name="content" />
</body>
</html><?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
template="/WEB-INF/template/default.xhtml">
<ui:define name="title">Hello World</ui:define>
<ui:define name="content">
<h:form>
<h2>Please enter your name:
<h:inputText id="name" value="#{helloBean.name}" required="true"/></h2>
<h:commandButton value="press me" action="#{helloAction.send}"/>
<h:message for="name"/>
</h:form>
</ui:define>
</ui:composition><faces-config>
<managed-bean>
<managed-bean-name>helloBean</managed-bean-name>
<managed-bean-class>cz.softeu.test.HelloBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>

package cz.softeu.test; public class HelloAction { /** * Action method */ public String send(){ //do something real return (“success”); } }
Do faces-config.xml přidat:
<faces-config>
<managed-bean>
<managed-bean-name>helloAction</managed-bean-name>
<managed-bean-class>cz.softeu.test.HelloAction</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>Volání akce:
...
<h:commandButton value="press me" action="#{helloAction.send}"/>
... Do faces-config.xml přidat:
<navigation-rule>
<from-view-id>/index.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/hello.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<h:outputLabel for="username">User Name:</h:outputLabel>
<h:inputText id="username" label="User Name" class="input-edit"
value="#{user.userName}" />
<h:selectOneMenu id="dnsDomain" label="DNS Domain" value="#{user.dnsDomain}">
<f:selectItem itemValue="" itemLabel="-- select value --" />
<s:selectItems value="#{dnsList.fullList}" var="detail" label="#{detail.name}" />
</h:selectOneMenu>
<h:inputTextarea ... cols="40" rows="6" ... / >
<h:selectBooleanCheckbox ... />
<h:commandButton value="Reserve" action="#{conceptHost.reserve}"/>
<rich:calendar value="#{data.enableFrom}" locale="#{localeSelector.locale}" popup="true"
datePattern="#{settings.dateFormat}" enableManualInput="true" />
Příklady:
#{promenna.properta}
#{promenna}
#{promenna.pole[pokus]}
#{not empty hodnota}
#{fn:toUpperCase(customer.name)}
#{a == 10 and c == '11'}

@Name("helloBean")
@Scope(ScopeType.EVENT)
class HelloBean {



Do faces-config.xml:
<navigation-rule>
<from-view-id>/tabular.xhtml</from-view-id>
<navigation-case>
<from-outcome>drilldown</from-outcome>
<to-view-id>/edit.xhtml</to-view-id>
</navigation-case>
</navigation-rule>Seam umožňuje do tabular.page.xml:
<navigation>
<rule if-outcome="drilldown">
<redirect view-id="/edit.xhtml"/>
</rule>
</navigation><h:inputText ... > <f:validateLength minimum="2" maximum="25" /> </h:inputText>

Do faces-config.xml:
<validator>
<validator-id>myValidator</validator-id>
<validator-class>cz.softeu.test.MyValidator</validator-class>
</validator>
V šabloně:
<h:inputText ...> <f:validator validatorId="myValidator"/> </h:inputText>
public class MyValidator implements Validator {
public void validate(FacesContext facesContext, UIComponent uiComp, Object value)
throws ValidatorException {
FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Always fail", "This validator always fails.");
throw new ValidatorException(fm);
}
}
V kódu:
FacesContext.getCurrentInstance().addMessage("fieldName",
new FacesMessage("Error description"));
V šabloně:
<h:messages showOnlyGlobal="true" styleClass="errors" > <h:message for="fieldName" styleClass="errors">

<ui:repeat value="#{bean.users}" var="user">
#{user.fullName}
</ui:repeat>
<h:inputText id="field" value="#{hostGroupHome.filter.idTe}" />
<rich:suggestionbox for="field"
suggestionAction="#{teManager.autocomplete}"
var="detail"
selfRendered="true"
fetchValue="#{detail.id}">
<h:column>
<h:outputText value="#{detail.name}" />
</h:column>
</rich:suggestionbox>