跳至主要内容

博文

目前显示的是 八月, 2013的博文

JSF 2.2: Embrace CDI?

JSF 2.2: Embrace CDI? CDI 1.0 is part of Java EE 6, it is the standard Dependency Injection specification for Java EE platform. But unfortunately, JSF 2.0 which is also part of Java EE 6 did not adopt CDI as its Dependency Injection/IOC container, but invented its IOC container.
Dependency injection in JSF 2.0 In the package javax.faces.bean, there are several annotations provided.
You can annotate your JSF backend bean with annotation @ManagedBean and put it in a reasonable scope( @ApplicationScoped, @RequestScoped, @SessionScoped, @ViewScoped, @NoneSopced).
@ManagedBean(name="foo") @RequestScoped public class FooBean{ } The attribute name specified in the @ManagedBean can be accessed via EL in facelets views.
You can inject it in other beans via @ManagedProperty.
@ManagedBean(name="bar") @RequestScoped public class BarBean{ @ManagedPropertiy("foo") FooBean foo; } In JSF 2.0, it also supports to annotate the fields with @Resource and @E…

JSF 2.2: Flow

JSF 2.2: Flow JSF 2.2 introduces a standard flow concept. The flow concept have been existed in several web solutions for several years, such as Spring Web Flow and Seam 2 page flow.
Introduction The flow definition follows the convention over configuration.
For example, in order to define a flow named flow1, you could create a folder named flow1 in the web root to group all views in this flow. By default, you must provides a view which name is same to the flow name as start node. In this example, it is flow1.xhtml. In order to start the flow, you should specify the value of h:commandButton or h:commandLink as the flow name, it will activate the Window Context support(by default it is disabled) and search the matched flow and navigate the start node of the flow.
/flow1 /flow1.xhtml /flow1a.xhtml /flow1b.xhtml Besides the view files, another flow description file is required.
There are two approaches to declare a flow.
Create a CDI @Produces to declare the flow.
@Pr…

JSF 2.2: HTML5 Support

JSF 2.2: HTML 5 Support No doubt HTML 5 is the hottest topic in web development in these years.
Besides a series of new tags are added, the form element support is also improved. The type attribute of input element can be text, email, number, range, url, date etc. Ideally, a browser could provide native validation for these input tags.(but this feature support is very different between browsers).
JSF 2.0 In JSF 2.0, some attributes of HTML 5 form elements can not be rendered correctly in the standard JSF input components. For example, <input type="email" can not be rendered by <h:inputText type="email".
omnifaces fixes the issue, and adds basic HTML5 form elements support by a extened Html5RenderKitFactory.
What you need to do is declare a Html5RenderKitFactory in the faces-config.xml.
<factory> <render-kit-factory>org.omnifaces.renderkit.Html5RenderKitFactory</render-kit-factory> </factory> Now, the <h:inputText type=&…

JSF 2.2: File Upload

JSF 2.2: File Upload Before 2.2, you have to use Richfaces, Primefaces like JSF components to provide file upload feature.
Or you must write a custom file upload component yourself.
Luckily this long waiting feature was finally added in JSF 2.2.
An example In JSF 2.2, the FacesServlet is annotated with @MultipartConfig, which causes JSF can handle multpart form data correctly.
@MultipartConfig public final class FacesServlet implements Servlet {} The following is a simple facelets page, which includes a h:form and h:inputFile components.
<h:form enctype="multipart/form-data"> <h:inputFile id="file" value="#{fileUploadBean.file}" /> <h:commandButton value="Upload" action="#{fileUploadBean.upload()}"/> </h:form> The new inputFile component is provided for file upload purpose. The usage is simple. Like other input components, put it into a h:form, the enctype attribute must be changed to multipart/form…

JSF 2.2: Theme

JSF 2.2: Theme Richfaces and Primefaces have theme support, it is an attractive feature of the skin-able JSF components projects.
JSF 2.2 introduces a new feature named Resource Library Contracts which supports to apply different resources(css and js) and facelets template at runtime.
contracts resources files can be placed in /contracts folder under the web application root or /META-INF/contracts on classpath.
Using contracts in a web application The following is an example of contracts resources structure in a web application.
/contracts /default /css defautl.css cssLayout.css template.xhtml /alternative /css defautl.css cssLayout.css template.xhtml There are two contracts defined in the project, default and alternative.
The following is the content of /contracts/default/template.xhtml.
<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html> <html xmlns="http:/…

JSF 2.2: View Action

JSF 2.2: View Action JSF 2.2 introduced a new view action feature, which had been existed in JBoss Seam 2 and Seam 3 for a long time.
In fact, JSF 2.2 copied the Seam 3 view action exactly.
An example An example is better than thousands of words.
@Model public class ViewActionBean { @Inject Logger log; private String flag="page1"; public String init(){ log.info("call init"); switch(flag){ case "page1": return "page1"; default: return "page2"; } } public String getFlag() { return flag; } public void setFlag(String flag) { this.flag = flag; } } Create a facelets view and use a view action to invoke the init method.
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.o…