From 7ba792b42485be82bde428f7923df12caf4f13e2 Mon Sep 17 00:00:00 2001 From: Miguel Reboiro-Jato Date: Wed, 9 Nov 2016 13:55:12 +0100 Subject: [PATCH] Replaces JSF annotations by CDI annotations The JSF 2.2 specification recommends using CDI annotations (e.g. @Named, @RequestScoped, etc.) instead of specific JSF annotations (e.g. @ManagedBean, @RequestScoped, etc.). This commit replaces these annotations following the recommendations. This change has some side effects, as now the container does not automatically recognizes the /faces route and JSF must be configured in the web.xml file. The new configuration now processes every XHTML page as a JSF and the /faces paths are no longer required or recognized. --- .../uvigo/esei/xcs/jsf/LoginManagedBean.java | 6 +-- ...anagerdBean.java => OwnerManagedBean.java} | 31 ++++++++------- .../es/uvigo/esei/xcs/jsf/PetManagedBean.java | 39 ++++++++++++------- jsf/src/main/webapp/WEB-INF/web.xml | 25 ++++++++++-- jsf/src/main/webapp/admin/owners.xhtml | 7 ++-- jsf/src/main/webapp/owner/pets.xhtml | 5 ++- .../es/uvigo/esei/xcs/jsf/OwnerJsfTest.java | 10 ++--- .../uvigo/esei/xcs/jsf/pages/LoginPage.java | 4 +- .../uvigo/esei/xcs/jsf/pages/OwnersPage.java | 10 ++--- 9 files changed, 84 insertions(+), 53 deletions(-) rename jsf/src/main/java/es/uvigo/esei/xcs/jsf/{OwnerManagerdBean.java => OwnerManagedBean.java} (79%) diff --git a/jsf/src/main/java/es/uvigo/esei/xcs/jsf/LoginManagedBean.java b/jsf/src/main/java/es/uvigo/esei/xcs/jsf/LoginManagedBean.java index 34a0000..07fd245 100644 --- a/jsf/src/main/java/es/uvigo/esei/xcs/jsf/LoginManagedBean.java +++ b/jsf/src/main/java/es/uvigo/esei/xcs/jsf/LoginManagedBean.java @@ -2,14 +2,14 @@ package es.uvigo.esei.xcs.jsf; import java.security.Principal; -import javax.faces.bean.ManagedBean; -import javax.faces.bean.RequestScoped; +import javax.enterprise.context.RequestScoped; import javax.faces.context.FacesContext; import javax.inject.Inject; +import javax.inject.Named; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; -@ManagedBean(name = "login") +@Named("login") @RequestScoped public class LoginManagedBean { @Inject diff --git a/jsf/src/main/java/es/uvigo/esei/xcs/jsf/OwnerManagerdBean.java b/jsf/src/main/java/es/uvigo/esei/xcs/jsf/OwnerManagedBean.java similarity index 79% rename from jsf/src/main/java/es/uvigo/esei/xcs/jsf/OwnerManagerdBean.java rename to jsf/src/main/java/es/uvigo/esei/xcs/jsf/OwnerManagedBean.java index 0fbe39e..026dfcf 100644 --- a/jsf/src/main/java/es/uvigo/esei/xcs/jsf/OwnerManagerdBean.java +++ b/jsf/src/main/java/es/uvigo/esei/xcs/jsf/OwnerManagedBean.java @@ -4,25 +4,25 @@ import static java.util.stream.Collectors.joining; import java.util.List; -import javax.faces.bean.ManagedBean; -import javax.faces.bean.SessionScoped; +import javax.enterprise.context.RequestScoped; import javax.faces.context.FacesContext; import javax.inject.Inject; +import javax.inject.Named; import es.uvigo.esei.xcs.domain.entities.Owner; import es.uvigo.esei.xcs.domain.entities.Pet; import es.uvigo.esei.xcs.service.OwnerService; -@ManagedBean(name = "owner") -@SessionScoped -public class OwnerManagerdBean { +@Named("owner") +@RequestScoped +public class OwnerManagedBean { @Inject private OwnerService service; private String login; private String password; - private Owner currentOwner; + private boolean editing; private String errorMessage; @@ -51,7 +51,11 @@ public class OwnerManagerdBean { } public boolean isEditing() { - return this.currentOwner != null; + return this.editing; + } + + public void setEditing(boolean editing) { + this.editing = editing; } public List getOwners() { @@ -64,9 +68,9 @@ public class OwnerManagerdBean { .collect(joining(", ")); } - public String edit(Owner owner) { - this.currentOwner = owner; - this.login = this.currentOwner.getLogin(); + public String edit(String login) { + this.editing = true; + this.login = login; return this.getViewId(); } @@ -86,9 +90,10 @@ public class OwnerManagerdBean { public String store() { try { if (this.isEditing()) { - this.currentOwner.changePassword(this.password); + final Owner owner = this.service.get(this.login); + owner.changePassword(this.password); - this.service.update(this.currentOwner); + this.service.update(owner); } else { this.service.create(new Owner(login, password)); } @@ -107,7 +112,7 @@ public class OwnerManagerdBean { this.login = null; this.password = null; this.errorMessage = null; - this.currentOwner = null; + this.editing = false; } private String redirectTo(String url) { diff --git a/jsf/src/main/java/es/uvigo/esei/xcs/jsf/PetManagedBean.java b/jsf/src/main/java/es/uvigo/esei/xcs/jsf/PetManagedBean.java index ae2760d..2af4579 100644 --- a/jsf/src/main/java/es/uvigo/esei/xcs/jsf/PetManagedBean.java +++ b/jsf/src/main/java/es/uvigo/esei/xcs/jsf/PetManagedBean.java @@ -4,17 +4,17 @@ import java.util.Date; import java.util.List; import java.util.Optional; -import javax.faces.bean.ManagedBean; -import javax.faces.bean.SessionScoped; +import javax.enterprise.context.RequestScoped; import javax.faces.context.FacesContext; import javax.inject.Inject; +import javax.inject.Named; import es.uvigo.esei.xcs.domain.entities.AnimalType; import es.uvigo.esei.xcs.domain.entities.Pet; import es.uvigo.esei.xcs.service.PetService; -@ManagedBean(name = "pet") -@SessionScoped +@Named("pet") +@RequestScoped public class PetManagedBean { @Inject private PetService service; @@ -23,7 +23,7 @@ public class PetManagedBean { private Date birth; private AnimalType animal; - private Pet currentPet; + private Integer id; private String errorMessage; @@ -62,7 +62,15 @@ public class PetManagedBean { } public boolean isEditing() { - return this.currentPet != null; + return this.id != null; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; } public List getPets() { @@ -70,10 +78,10 @@ public class PetManagedBean { } public String edit(Pet pet) { - this.currentPet = pet; - this.name = this.currentPet.getName(); - this.birth = this.currentPet.getBirth(); - this.animal = this.currentPet.getAnimal(); + this.id = pet.getId(); + this.name = pet.getName(); + this.birth = pet.getBirth(); + this.animal = pet.getAnimal(); return this.getViewId(); } @@ -93,11 +101,12 @@ public class PetManagedBean { public String store() { try { if (this.isEditing()) { - this.currentPet.setName(this.name); - this.currentPet.setBirth(this.birth); - this.currentPet.setAnimal(this.animal); + final Pet pet = this.service.get(this.id); + pet.setName(this.name); + pet.setBirth(this.birth); + pet.setAnimal(this.animal); - this.service.update(this.currentPet); + this.service.update(pet); } else { this.service.create(new Pet(name, animal, birth)); } @@ -113,10 +122,10 @@ public class PetManagedBean { } private void clear() { - this.currentPet = null; this.name = null; this.birth = null; this.animal = null; + this.id = null; this.errorMessage = null; } diff --git a/jsf/src/main/webapp/WEB-INF/web.xml b/jsf/src/main/webapp/WEB-INF/web.xml index b71dc7f..b79c6b3 100644 --- a/jsf/src/main/webapp/WEB-INF/web.xml +++ b/jsf/src/main/webapp/WEB-INF/web.xml @@ -5,14 +5,33 @@ Pet Store JSF - /faces/index.xhtml + index.xhtml + + + javax.faces.PROJECT_STAGE + Development + + + + + Faces Servlet + javax.faces.webapp.FacesServlet + 1 + + + + + Faces Servlet + *.xhtml + + admin - /faces/admin/* + /admin/* POST GET PUT @@ -26,7 +45,7 @@ owner - /faces/owner/* + /owner/* POST GET PUT diff --git a/jsf/src/main/webapp/admin/owners.xhtml b/jsf/src/main/webapp/admin/owners.xhtml index f5c3f73..55e5e89 100644 --- a/jsf/src/main/webapp/admin/owners.xhtml +++ b/jsf/src/main/webapp/admin/owners.xhtml @@ -18,8 +18,9 @@ Password - - + + +
@@ -50,7 +51,7 @@ - + diff --git a/jsf/src/main/webapp/owner/pets.xhtml b/jsf/src/main/webapp/owner/pets.xhtml index ecdb845..b81f128 100644 --- a/jsf/src/main/webapp/owner/pets.xhtml +++ b/jsf/src/main/webapp/owner/pets.xhtml @@ -1,7 +1,7 @@ @@ -23,8 +23,9 @@ + - +
diff --git a/jsf/src/test/java/es/uvigo/esei/xcs/jsf/OwnerJsfTest.java b/jsf/src/test/java/es/uvigo/esei/xcs/jsf/OwnerJsfTest.java index a3212e9..4c1614f 100644 --- a/jsf/src/test/java/es/uvigo/esei/xcs/jsf/OwnerJsfTest.java +++ b/jsf/src/test/java/es/uvigo/esei/xcs/jsf/OwnerJsfTest.java @@ -64,8 +64,8 @@ public class OwnerJsfTest { .addAsWebResource(WEBAPP.resolve("admin/owners.xhtml").toFile(), "admin/owners.xhtml") .addAsResource("test-persistence.xml", "META-INF/persistence.xml") .addAsWebInfResource(WEBAPP.resolve("WEB-INF/template.xhtml").toFile()) - .addAsWebInfResource("jboss-web.xml") - .addAsWebInfResource("web.xml") + .addAsWebInfResource(WEBAPP.resolve("WEB-INF/web.xml").toFile()) + .addAsWebInfResource(WEBAPP.resolve("WEB-INF/jboss-web.xml").toFile()) .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); } @@ -229,8 +229,6 @@ public class OwnerJsfTest { assertDeleteOwner(ownerWithPets()); } - - @Test @InSequence(36) @ShouldMatchDataSet("owners-remove-with-pets.xml") @CleanupUsingScript({ "cleanup.sql", "cleanup-autoincrement.sql" }) @@ -246,6 +244,8 @@ public class OwnerJsfTest { assertThat(ownersPage.areOwnersInTable(expectedOwners), is(true)); } + + @Test @InSequence(41) @UsingDataSet("owners.xml") @Cleanup(phase = TestExecutionPhase.NONE) @@ -268,7 +268,7 @@ public class OwnerJsfTest { } @Test @InSequence(43) - @ShouldMatchDataSet("owners-update-password.xml") + @ShouldMatchDataSet(value = "owners-update-password.xml") @CleanupUsingScript({ "cleanup.sql", "cleanup-autoincrement.sql" }) public void afterEdit() {} diff --git a/jsf/src/test/java/es/uvigo/esei/xcs/jsf/pages/LoginPage.java b/jsf/src/test/java/es/uvigo/esei/xcs/jsf/pages/LoginPage.java index 06a0211..c49d2e7 100644 --- a/jsf/src/test/java/es/uvigo/esei/xcs/jsf/pages/LoginPage.java +++ b/jsf/src/test/java/es/uvigo/esei/xcs/jsf/pages/LoginPage.java @@ -8,7 +8,7 @@ import org.jboss.arquillian.graphene.page.Location; import org.openqa.selenium.WebDriver; import org.openqa.selenium.support.FindBy; -@Location("faces/index.xhtml") +@Location("index.xhtml") public class LoginPage { @Drone private WebDriver browser; @@ -21,6 +21,6 @@ public class LoginPage { } public void assertOnLoginPage() { - assertThat(browser.getCurrentUrl(), containsString("/faces/index.xhtml")); + assertThat(browser.getCurrentUrl(), containsString("/index.xhtml")); } } diff --git a/jsf/src/test/java/es/uvigo/esei/xcs/jsf/pages/OwnersPage.java b/jsf/src/test/java/es/uvigo/esei/xcs/jsf/pages/OwnersPage.java index a298e33..4407066 100644 --- a/jsf/src/test/java/es/uvigo/esei/xcs/jsf/pages/OwnersPage.java +++ b/jsf/src/test/java/es/uvigo/esei/xcs/jsf/pages/OwnersPage.java @@ -1,5 +1,6 @@ package es.uvigo.esei.xcs.jsf.pages; +import static java.util.Arrays.stream; import static org.hamcrest.core.StringContains.containsString; import static org.junit.Assert.assertThat; @@ -25,16 +26,11 @@ public class OwnersPage { private GrapheneElement storeError; public void assertOnOwnersPage() { - assertThat(browser.getCurrentUrl(), containsString("/faces/admin/owners.xhtml")); + assertThat(browser.getCurrentUrl(), containsString("/owners.xhtml")); } public boolean areOwnersInTable(Owner ... owners) { - for (Owner owner : owners) { - if (!this.isOwnerInTable(owner)) - return false; - } - - return true; + return stream(owners).allMatch(this::isOwnerInTable); } public boolean isOwnerInTable(Owner owner) { -- 2.18.1