Notes by Fang
Displaying keyword search results 1 - 10
Created by Fang on April 16, 2012 13:18:40
Last update: April 16, 2012 13:18:40
Simply call pageContext.setAttribute() to export a variable from within a JSP custom tag:
public class MyCustomVarTag extends TagSupport {
...
The availability of the exported variable can be limited in the TLD:
<tag>
<name>setVar</name>
<tag-class...
The availability scopes are:
Value Availability
NESTED Between the start tag and the end tag.
AT_BEGIN From the start tag until the scope of any enclosing tag. If there’s no enclosing tag, then to the end of the page.
AT_END After the end tag until the scope of any enclosing tag. If there’s no enclosing tag, then to the end of the page.
Created by Fang on January 28, 2012 13:24:09
Last update: January 28, 2012 13:31:22
This is a simple JSP custom tags library with tag body. Just like the JSF counterpart , it splits a string and repeats the body for each word, i.e., with this markup:
<%@ taglib uri="http://custom.tag.com/demo" prefix...
output:
<html>
<body>
<p>Hello Tigger!</p>
<p>H...
With Maven, this is the directory structure:
./src
./src/main
./src/main/resources
./s...
There are three files to write:
pom.xml :
<project xmlns="http://maven.apache.org/POM/4.0.0"...
src/main/java/tagdemo/IterateTag.java :
package tagdemo;
import java.io.IOException...
src/main/resources/META-INF/demotag.tld :
<?xml version="1.0" encoding="UTF-8"?>
<!DO...
Build with:
mvn clean install
To use it as a dependency in other Maven projects:
<dependency>
<groupId>tag-demo</groupId>
...
Created by Fang on November 21, 2011 16:30:56
Last update: December 07, 2011 08:54:32
This is a series of notes on building custom JSF 2.0 facelet taglibs, ordered from the simplest to the less simple. Hopefully it can help you to get started on how to build custom taglibs for JSF. A simple JSF facelets taglib example The simplest taglib I can think of. Using EL expression with a custom tag Make tag attributes dynamic. Mixing custom tag with facelet ui taglibs Discover things you might run into when you get into more details. Which EL context to use? Using the wrong EL context can lead to subtle bugs. JSF facelet taglib backed by a UI component A UIComponent can be a tag handler, without being TagHandler . Using tag handler, UI component and renderer with a JSF facelet...
Created by Fang on November 10, 2011 13:19:13
Last update: December 01, 2011 19:10:43
You can add custom implicit variables to JSF pages by using a custom EL resolver, in two simple steps: Write an ELResolver class to resolve the variable Add the ELResolver to faces-config.xml Starting from the Maven Hello World example: Add faces API and EL dependencies to pom.xml :
<dependencies> <dependency> <groupId>o... Add a simple greeter class ( src/main/java/com/example/Greeter.java ): package com.example; public class Greeter {... Add our custom EL resolver ( src/main/java/com/example/ELResolver.java ): package com.example; import java.util.Itera... Add the custom EL resolver to src/main/resources/META-INF/faces-config.xml <?xml version="1.0" encoding="UTF-8"?> <faces-c... Build JAR with mvn package Drop the JAR into WEB-INF/lib of a webapp and test the new EL with: <h:outputText value="#{Greeter.sayHi('Mike')}"/> Fixed: the setValue method used to throw an exception, which is wrong. @Override public void setValue(ELContext ctx, O......
Created by Fang on December 01, 2011 18:54:54
Last update: December 01, 2011 18:54:54
Use the varStatus attribute with the ui:repeat tag to get the iteration index:
<ui:repeat varStatus="status" var="item" value="#{...
The varStatus attribute specifies the name of the exported request scoped variable for the status of the iteration.The object is a POJO with the following read-only JavaBeans properties. This scoped variable has nested visibility.
begin of type Integer
end of type Integer
index of type int
step of type Integer
even of type boolean
odd of type boolean
first of type boolean
last of type boolean
Created by Fang on November 22, 2011 10:40:16
Last update: November 22, 2011 10:40:16
This is an example that uses tag handler, UI component and renderer together to support a custom taglib. The main purpose is to show how these components play together.
The tag renders
<ui:param name="extra" value="el interpreted"/>
...
as
<h3>my:foreach</h3>
<ul class="css class" extra...
These are the files:
The tag handler ( src/main/java/com/example/ForeachTagHandler.java ):
package com.example;
import java.util.Map;
...
The UI component ( src/main/java/com/example/UIForeach.java ):
package com.example;
import java.io.IOExcep...
The renderer ( src/main/java/com/example/ForeachRenderer.java ):
package com.example;
import java.io.IOExcep...
Faces config ( src/main/resources/META-INF/faces-config.xml ):
<?xml version="1.0" encoding="UTF-8"?>
<faces-c...
Taglib config ( src/main/resources/META-INF/foreach.taglib.xml ):
<?xml version="1.0" encoding="UTF-8"?>
<facelet...
Created by Fang on November 21, 2011 15:57:49
Last update: November 22, 2011 09:51:26
The improved custom taglib works with existing facelet ui taglibs. For example:
<ui:param name="theName" value="John"/> <my:hel... produces the expected output. However, a problem exists with the ui:repeat tag: <h3>With ui:repeat</h3> <ui:repeat var="theName... When tested with a URL like: http://localhost:8080/facelet-demo/?name=Zack&name... the raw EL prints out the correct names, but my custom tag substitutes empty string for theName2 ! In theory, the response is rendered in the Render Response phase, way after the Apply Request Values phase, actual values should be available to EL. The answer to this anomaly turned out to be very deep ! Yes, right there in the code! I would consider this a bug in facelets implementation, but the JSF spec did not tell what the expected behavior should be. In my custom...
Created by Fang on November 14, 2011 20:50:51
Last update: November 22, 2011 09:06:10
This facelet fragment will never print anything:
<ui:repeat var="person" value="#{myBean.theJTeam}"... because the test condition always returns false, even though the person var is not null. The same happens even when I define another variable with ui:param : <ui:repeat var="person" value="#{myBean.theJTeam}"... What's happening? The c:if test condition is evaluated before the ui:repeat tag had a chance to set the value! Both facelet ui tags and JSTL c tags are evaluated at the Render Response phase of the JSF lifecycle. But within the Render Response cycle, there are two sub-phases (so to speak): the first builds the UI element tree, the second renders the UI tree. The JSTL c:if tag is evaluated when the tree is built, but the facelet ui:repeat tag is evaluated when the UI...
Created by Fang on November 08, 2011 20:55:00
Last update: November 21, 2011 18:19:44
In the simple taglib example , I used a tag handler class to implement a taglib. This is an example to implement a taglib with a UI component. The purpose is to use a custom tag to split a string and print each part in a separate paragraph, i.e., print
<p>john</p> <p>steve</p> <p>mike</p> with custom tag <my:foreach> : <my:foreach var="who" value="john steve mike"> ... These are the files: pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0"... src/main/java/com/example/UIForeash.java : package com.example; import java.io.IOExcep... src/main/resources/META-INF/faces-config.xml : <?xml version="1.0" encoding="UTF-8"?> <faces-c... src/main/resources/META-INF/foreach.taglib.xml : <?xml version="1.0" encoding="UTF-8"?> <facelet... How to use: Put the JAR file generated by the above project in the WEB-INF/lib folder of the web app. If the web app is a Maven project, just add the taglib project as a dependency:...
Created by Fang on November 12, 2011 21:03:03
Last update: November 12, 2011 21:03:03
Experts may disagree but I found it absolutely stunning that JSF EL does not provide an operator for string concatenation. The Java "+" operator is there for the take. Java, which is a strongly typed compiled language, overloads the "+" operator in such a way that any object can be concatenated with a string. But JSF EL, which definitely isn't as strongly typed as Java, restricts the "+" operator to numerical values only! Of course, experts may argue that the "+" operator overloading is a huge design flaw of the Java language. But even so, JSF EL is not the right place to fix it! In some cases, a concatenation operator isn't needed, for example:
<ui:repeat var="tab" value="#{tabs}"> <img ... But in case the concatenated...