Run applications in JSF 2.2 compatible mode
For those not ready for new CDI activation, JSF 2.3 provides a compatible mode to make your codes working seamlessly with a JSF 2.2 faces-config.xml.
In contrast with the former JSF 2.3 configuration, we should perform some modification here.
  1. 1.
    Use a 2.2 versioned faces-config.xml instead. ​
    1
    <?xml version='1.0' encoding='UTF-8'?>
    2
    <faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    5
    version="2.2">
    6
    </faces-config>
    Copied!
  2. 2.
    Declare a beans.xml, CDI is enabled by default in Java EE 7, so it is optional.
    1
    <?xml version="1.0" encoding="UTF-8"?>
    2
    <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
    5
    bean-discovery-mode="all"
    6
    version="2.0">
    7
    </beans>
    Copied!
  3. 3.
    Add 3.1 versioned web.xml. It is optional, but the compatible mode only works with 3.1, does not work with a 4.0 versioned web.xml.
    1
    <?xml version="1.0" encoding="UTF-8"?>
    2
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    5
    version="3.1">
    6
    </web-app>
    Copied!
  4. 4.
    Remove the @FacesConfig annotated class, it is only for JSF 2.3.
Done.
Now create a simple sample to try it.
Firstly create a simple backing bean.
1
@Named
2
@RequestScoped
3
public class HelloBean {
4
5
private String message;
6
7
public String getMessage() {
8
return message;
9
}
10
11
public void setMessage(String message) {
12
this.message = message;
13
}
14
15
public void sayHi() {
16
this.message = "Greeting at " + LocalDateTime.now();
17
}
18
19
@Override
20
public int hashCode() {
21
int hash = 7;
22
hash = 13 * hash + Objects.hashCode(this.message);
23
return hash;
24
}
25
26
@Override
27
public boolean equals(Object obj) {
28
if (this == obj) {
29
return true;
30
}
31
if (obj == null) {
32
return false;
33
}
34
if (getClass() != obj.getClass()) {
35
return false;
36
}
37
final HelloBean other = (HelloBean) obj;
38
if (!Objects.equals(this.message, other.message)) {
39
return false;
40
}
41
return true;
42
}
43
44
}
Copied!
And a simple Facelets template.
1
<!DOCTYPE html>
2
<html lang="en"
3
xmlns="http://www.w3.org/1999/xhtml"
4
xmlns:h="http://xmlns.jcp.org/jsf/html"
5
xmlns:jsf="http://xmlns.jcp.org/jsf">
6
<h:head/>
7
8
<h:messages />
9
10
<body>
11
<p>
12
Hello JSF 2.3(JSF 2.2 Compatible Mode)
13
</p>
14
<form jsf:id="form">
15
<p>
16
<strong>Say Hi to JSF 2.3: </strong>
17
<div>#{helloBean.message}</div>
18
</p>
19
<p>
20
<input type="submit" value="Login" jsf:action="#{helloBean.sayHi()}" />
21
</p>
22
</form>
23
</body>
24
25
</html>
Copied!
In NetBeans IDE, run it on Glassfish directly, finally it will open the home page.
Get the source codes from my GitHub account.
Last modified 8mo ago
Copy link