In this post,
we will write a simple login custom tag. The climax will consist in passing a
method expression as attribute into our tag using OmniFaces, <o:methodParam>.
But, first let's see the implementation of our custom tag (login.xhtml):
<?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:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
<h:body>
<ui:composition>
<h:form>
<h:panelGrid columns="2">
<h:outputLabel
for="emailId" value="#{emaillabel}"/>
<h:inputText
id="emailId" value="#{emailfield}"/>
<h:outputLabel
for="passId" value="#{passlabel}"/>
<h:inputSecret
id="passId" value="#{passfield}"/>
<h:commandButton
value="#{btnlabel}"/>
</h:panelGrid>
</h:form>
</ui:composition>
</h:body>
</html>
After we
configure this implementation in a *.taglib.xml file, we can easily use it
as below:
<?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:h="http://xmlns.jcp.org/jsf/html"
xmlns:mt="http://jsf.custom/tags">
<h:head>
<title>
</title>
</h:head>
<h:body>
<mt:login emaillabel="E-mail" passlabel="Password"
btnlabel="Login"
emailfield="#{loginBean.email}" passfield="#{loginBean.password}"/>
emailfield="#{loginBean.email}" passfield="#{loginBean.password}"/>
</h:body>
</html>
Now, when we
press the Login
button we want to submit the form and invoke a bean method named, login().
This method will check the user credentials and return a new page, welcome.xhtml.
But, by default we can submit the form but we cannot invoke the bean method,
because we cannot pass a method expression as attribute into our tag (or any
other Facelets tag). This is the point where OmniFaces comes to "rescue"
the situation, by providing a custom tag handler that facilitate us to
accomplish such tasks. This tag handler is named MethodParam, and it can be
used via <o:methodParam>
tag. First we add <o:methodParam> in login.xhtml, as below:
<?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:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:o="http://omnifaces.org/ui">
<h:body>
<ui:composition>
<h:form>
<h:panelGrid columns="2">
<h:outputLabel for="emailId"
value="#{emaillabel}"/>
<h:inputText id="emailId"
value="#{emailfield}"/>
<h:outputLabel for="passId"
value="#{passlabel}"/>
<h:inputSecret id="passId"
value="#{passfield}"/>
<o:methodParam name="method" value="#{action}"
/>
<h:commandButton value="#{btnlabel}" action="#{method}"/>
</h:panelGrid>
</h:form>
</ui:composition>
</h:body>
</html>
Perfect! Now,
we just instruct JSF about our bean method as below:
<?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:h="http://xmlns.jcp.org/jsf/html"
xmlns:mt="http://jsf.custom/tags">
<h:head>
<title>
</title>
</h:head>
<h:body>
<mt:login emaillabel="E-mail"
passlabel="Password" btnlabel="Login" emailfield="#{loginBean.email}"
passfield="#{loginBean.password}" action="#{loginBean.login()}"/>
passfield="#{loginBean.password}" action="#{loginBean.login()}"/>
</h:body>
</html>
Complete
code on GitHub.
Note MethodParam
can be used to pass a method expression as attribute into our tag and the method
can be:
·
a method with zero arguments
·
a method with zero arguments but explicit
parenthesis
·
a method with 1 view provided argument
·
a method to second bean with 1 view provided
argument
·
a listener method with zero arguments
·
a listener method with zero arguments but
explicit parenthesis
·
a listener method with 1 view provided argument
·
a listener method with declared event parameter,
no provided arguments
·
a direct binding (no method passing) with zero
arguments
So, do not
hesitate to check the MethodParam Showcase to see more useful examples.
Niciun comentariu :
Trimiteți un comentariu