BAEL-1414 Learn to Fully Leverage Java Server Faces (#3446)
This commit is contained in:
@@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
Reference in New Issue
Block a user