Skip to content

Commit 1d29f9b

Browse files
forketyforkEugen
authored and
Eugen
committedJan 18, 2018
BAEL-1414 Learn to Fully Leverage Java Server Faces (eugenp#3446)
1 parent 2055105 commit 1d29f9b

11 files changed

+263
-0
lines changed
 

‎guest/deep-jsf/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Building
2+
3+
To build the module, use Maven's `package` goal:
4+
5+
```
6+
mvn clean package
7+
```
8+
9+
The `war` file will be available at `target/deep-jsf.war`
10+
11+
## Running
12+
13+
The `war` application is deployed to a Java EE 7 compliant application server, for example, to GlassFish 4 or later.
14+
15+
The example then will be accessible at http://localhost:8080/deep-jsf/index.xhtml

‎guest/deep-jsf/pom.xml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.stackify</groupId>
6+
<artifactId>deep-jsf</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>war</packaging>
9+
10+
<properties>
11+
<failOnMissingWebXml>false</failOnMissingWebXml>
12+
</properties>
13+
14+
<dependencies>
15+
16+
<dependency>
17+
<groupId>javax</groupId>
18+
<artifactId>javaee-api</artifactId>
19+
<version>7.0</version>
20+
<scope>provided</scope>
21+
</dependency>
22+
23+
</dependencies>
24+
25+
<build>
26+
<finalName>deep-jsf</finalName>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.apache.maven.plugins</groupId>
30+
<artifactId>maven-compiler-plugin</artifactId>
31+
<version>3.7.0</version>
32+
<configuration>
33+
<source>1.8</source>
34+
<target>1.8</target>
35+
</configuration>
36+
</plugin>
37+
</plugins>
38+
</build>
39+
40+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.stackify.deepjsf;
2+
3+
import javax.faces.bean.ManagedBean;
4+
import javax.faces.bean.RequestScoped;
5+
6+
@ManagedBean
7+
@RequestScoped
8+
public class GreetControllerBean {
9+
10+
public String greet() {
11+
return "greet";
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.stackify.deepjsf;
2+
3+
import javax.faces.bean.ManagedBean;
4+
import javax.faces.bean.RequestScoped;
5+
import javax.faces.component.UIComponent;
6+
import javax.faces.component.UIViewRoot;
7+
import javax.faces.component.visit.VisitContext;
8+
import javax.faces.component.visit.VisitResult;
9+
import javax.faces.event.PhaseEvent;
10+
import javax.faces.event.PhaseId;
11+
import javax.servlet.http.HttpServletRequest;
12+
13+
@ManagedBean
14+
@RequestScoped
15+
public class PhaseListenerBean {
16+
17+
public void beforeListener(PhaseEvent event) {
18+
if (!event.getPhaseId().equals(PhaseId.RENDER_RESPONSE)) {
19+
return;
20+
}
21+
UIViewRoot root = event.getFacesContext().getViewRoot();
22+
23+
boolean showNewFeature = showNewFeatureForIp(event);
24+
25+
processComponentTree(root, event, showNewFeature);
26+
}
27+
28+
private boolean showNewFeatureForIp(PhaseEvent event) {
29+
HttpServletRequest request = (HttpServletRequest) event.getFacesContext()
30+
.getExternalContext().getRequest();
31+
String ip = request.getRemoteAddr();
32+
return !ip.startsWith("127.0");
33+
}
34+
35+
private void processComponentTree(UIComponent component, PhaseEvent event, boolean show) {
36+
component.visitTree(VisitContext.createVisitContext(event.getFacesContext()),
37+
(context, target) -> {
38+
if (target.getId() != null
39+
&& target.getId().startsWith("new-feature-")
40+
&& !show) {
41+
target.setRendered(false);
42+
}
43+
return VisitResult.ACCEPT;
44+
});
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.stackify.deepjsf;
2+
3+
import javax.faces.bean.ManagedBean;
4+
import javax.faces.bean.SessionScoped;
5+
import javax.faces.event.ValueChangeEvent;
6+
7+
@ManagedBean
8+
@SessionScoped
9+
public class UserBean {
10+
11+
private String name = "";
12+
13+
private String lastName = "";
14+
15+
private String proposedLogin = "";
16+
17+
public void nameChanged(ValueChangeEvent event) {
18+
this.proposedLogin = event.getNewValue() + "-" + lastName;
19+
}
20+
21+
public void lastNameChanged(ValueChangeEvent event) {
22+
this.proposedLogin = name + "-" + event.getNewValue();
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
public String getLastName() {
34+
return lastName;
35+
}
36+
37+
public void setLastName(String lastName) {
38+
this.lastName = lastName;
39+
}
40+
41+
public String getProposedLogin() {
42+
return proposedLogin;
43+
}
44+
45+
public void setProposedLogin(String proposedLogin) {
46+
this.proposedLogin = proposedLogin;
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.stackify.deepjsf;
2+
3+
import javax.faces.bean.ManagedBean;
4+
import javax.faces.bean.RequestScoped;
5+
6+
@ManagedBean
7+
@RequestScoped
8+
public class UserControllerBean {
9+
10+
public String register() {
11+
return "register-success";
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<faces-config version="2.2" 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
5+
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
6+
7+
<navigation-rule>
8+
<from-view-id>/register.xhtml</from-view-id>
9+
<navigation-case>
10+
<from-outcome>register-success</from-outcome>
11+
<to-view-id>/hello.xhtml</to-view-id>
12+
</navigation-case>
13+
</navigation-rule>
14+
15+
</faces-config>
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4+
<html xmlns="http://www.w3.org/1999/xhtml"
5+
xmlns:h="http://xmlns.jcp.org/jsf/html"
6+
xmlns:f="http://xmlns.jcp.org/jsf/core">
7+
<f:view beforePhase="#{phaseListenerBean.beforeListener}">
8+
<h:outputLabel value="Hello, #{userBean.name}"/>
9+
<h:outputLabel id="new-feature-last-name" value=" #{userBean.lastName}"/>
10+
</f:view>
11+
</html>
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4+
<html xmlns="http://www.w3.org/1999/xhtml"
5+
xmlns:h="http://xmlns.jcp.org/jsf/html"
6+
xmlns:f="http://xmlns.jcp.org/jsf/core">
7+
<f:view>
8+
<h:outputLabel value="Hello, #{userBean.name} #{userBean.lastName}! Your login is: #{userBean.proposedLogin}"/>
9+
</f:view>
10+
</html>
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4+
<html xmlns="http://www.w3.org/1999/xhtml"
5+
xmlns:h="http://xmlns.jcp.org/jsf/html"
6+
xmlns:f="http://xmlns.jcp.org/jsf/core">
7+
<f:view beforePhase="#{phaseListenerBean.beforeListener}">
8+
<h:form>
9+
<h:panelGrid columns="2">
10+
<h:outputLabel value="First Name:"/>
11+
<h:inputText id="name" value="#{userBean.name}"/>
12+
<h:outputLabel id="new-feature-last-name-label" value="Last Name:"/>
13+
<h:inputText id="new-feature-last-name" value="#{userBean.lastName}"/>
14+
<h:commandButton value="Submit" action="#{greetControllerBean.greet}"/>
15+
</h:panelGrid>
16+
</h:form>
17+
</f:view>
18+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4+
<html xmlns="http://www.w3.org/1999/xhtml"
5+
xmlns:h="http://xmlns.jcp.org/jsf/html"
6+
xmlns:f="http://xmlns.jcp.org/jsf/core">
7+
8+
<h:head>
9+
<h:outputScript library="javax.faces" name="jsf.js"/>
10+
</h:head>
11+
12+
<f:view>
13+
<h:form>
14+
<h:panelGrid columns="2">
15+
<h:outputLabel value="First Name:"/>
16+
<h:inputText id="name" value="#{userBean.name}"
17+
valueChangeListener="#{userBean.nameChanged}">
18+
<f:ajax event="change" execute="@this" render="proposed-login"/>
19+
</h:inputText>
20+
<h:outputLabel id="lastname-label" value="Last Name:"/>
21+
<h:inputText id="lastname" value="#{userBean.lastName}"
22+
valueChangeListener="#{userBean.lastNameChanged}">
23+
<f:ajax event="change" execute="@this" render="proposed-login"/>
24+
</h:inputText>
25+
<h:outputLabel id="login-label" value="Proposed Login:"/>
26+
<h:inputText id="proposed-login" disabled="true" value="#{userBean.proposedLogin}"/>
27+
<h:commandButton value="Submit" action="#{userControllerBean.register}"/>
28+
</h:panelGrid>
29+
</h:form>
30+
</f:view>
31+
</html>

0 commit comments

Comments
 (0)
Please sign in to comment.