Skip to content

Commit

Permalink
BAEL-1414 Learn to Fully Leverage Java Server Faces (eugenp#3446)
Browse files Browse the repository at this point in the history
  • Loading branch information
forketyfork authored and Eugen committed Jan 18, 2018
1 parent 2055105 commit 1d29f9b
Show file tree
Hide file tree
Showing 11 changed files with 263 additions and 0 deletions.
15 changes: 15 additions & 0 deletions guest/deep-jsf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Building

To build the module, use Maven's `package` goal:

```
mvn clean package
```

The `war` file will be available at `target/deep-jsf.war`

## Running

The `war` application is deployed to a Java EE 7 compliant application server, for example, to GlassFish 4 or later.

The example then will be accessible at http://localhost:8080/deep-jsf/index.xhtml
40 changes: 40 additions & 0 deletions guest/deep-jsf/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.stackify</groupId>
<artifactId>deep-jsf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>

<dependencies>

<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>

</dependencies>

<build>
<finalName>deep-jsf</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.stackify.deepjsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class GreetControllerBean {

public String greet() {
return "greet";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.stackify.deepjsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.component.visit.VisitContext;
import javax.faces.component.visit.VisitResult;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.servlet.http.HttpServletRequest;

@ManagedBean
@RequestScoped
public class PhaseListenerBean {

public void beforeListener(PhaseEvent event) {
if (!event.getPhaseId().equals(PhaseId.RENDER_RESPONSE)) {
return;
}
UIViewRoot root = event.getFacesContext().getViewRoot();

boolean showNewFeature = showNewFeatureForIp(event);

processComponentTree(root, event, showNewFeature);
}

private boolean showNewFeatureForIp(PhaseEvent event) {
HttpServletRequest request = (HttpServletRequest) event.getFacesContext()
.getExternalContext().getRequest();
String ip = request.getRemoteAddr();
return !ip.startsWith("127.0");
}

private void processComponentTree(UIComponent component, PhaseEvent event, boolean show) {
component.visitTree(VisitContext.createVisitContext(event.getFacesContext()),
(context, target) -> {
if (target.getId() != null
&& target.getId().startsWith("new-feature-")
&& !show) {
target.setRendered(false);
}
return VisitResult.ACCEPT;
});
}

}
48 changes: 48 additions & 0 deletions guest/deep-jsf/src/main/java/com/stackify/deepjsf/UserBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.stackify.deepjsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ValueChangeEvent;

@ManagedBean
@SessionScoped
public class UserBean {

private String name = "";

private String lastName = "";

private String proposedLogin = "";

public void nameChanged(ValueChangeEvent event) {
this.proposedLogin = event.getNewValue() + "-" + lastName;
}

public void lastNameChanged(ValueChangeEvent event) {
this.proposedLogin = name + "-" + event.getNewValue();
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getProposedLogin() {
return proposedLogin;
}

public void setProposedLogin(String proposedLogin) {
this.proposedLogin = proposedLogin;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.stackify.deepjsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class UserControllerBean {

public String register() {
return "register-success";
}

}
15 changes: 15 additions & 0 deletions guest/deep-jsf/src/main/webapp/WEB-INF/faces-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">

<navigation-rule>
<from-view-id>/register.xhtml</from-view-id>
<navigation-case>
<from-outcome>register-success</from-outcome>
<to-view-id>/hello.xhtml</to-view-id>
</navigation-case>
</navigation-rule>

</faces-config>
11 changes: 11 additions & 0 deletions guest/deep-jsf/src/main/webapp/greet.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?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:f="http://xmlns.jcp.org/jsf/core">
<f:view beforePhase="#{phaseListenerBean.beforeListener}">
<h:outputLabel value="Hello, #{userBean.name}"/>
<h:outputLabel id="new-feature-last-name" value=" #{userBean.lastName}"/>
</f:view>
</html>
10 changes: 10 additions & 0 deletions guest/deep-jsf/src/main/webapp/hello.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?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:f="http://xmlns.jcp.org/jsf/core">
<f:view>
<h:outputLabel value="Hello, #{userBean.name} #{userBean.lastName}! Your login is: #{userBean.proposedLogin}"/>
</f:view>
</html>
18 changes: 18 additions & 0 deletions guest/deep-jsf/src/main/webapp/index.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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:f="http://xmlns.jcp.org/jsf/core">
<f:view beforePhase="#{phaseListenerBean.beforeListener}">
<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="First Name:"/>
<h:inputText id="name" value="#{userBean.name}"/>
<h:outputLabel id="new-feature-last-name-label" value="Last Name:"/>
<h:inputText id="new-feature-last-name" value="#{userBean.lastName}"/>
<h:commandButton value="Submit" action="#{greetControllerBean.greet}"/>
</h:panelGrid>
</h:form>
</f:view>
</html>
31 changes: 31 additions & 0 deletions guest/deep-jsf/src/main/webapp/register.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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:f="http://xmlns.jcp.org/jsf/core">

<h:head>
<h:outputScript library="javax.faces" name="jsf.js"/>
</h:head>

<f:view>
<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="First Name:"/>
<h:inputText id="name" value="#{userBean.name}"
valueChangeListener="#{userBean.nameChanged}">
<f:ajax event="change" execute="@this" render="proposed-login"/>
</h:inputText>
<h:outputLabel id="lastname-label" value="Last Name:"/>
<h:inputText id="lastname" value="#{userBean.lastName}"
valueChangeListener="#{userBean.lastNameChanged}">
<f:ajax event="change" execute="@this" render="proposed-login"/>
</h:inputText>
<h:outputLabel id="login-label" value="Proposed Login:"/>
<h:inputText id="proposed-login" disabled="true" value="#{userBean.proposedLogin}"/>
<h:commandButton value="Submit" action="#{userControllerBean.register}"/>
</h:panelGrid>
</h:form>
</f:view>
</html>

0 comments on commit 1d29f9b

Please sign in to comment.