Commit 7ba792b4 authored by Administrator's avatar Administrator

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.
parent c4d17fa9
...@@ -2,14 +2,14 @@ package es.uvigo.esei.xcs.jsf; ...@@ -2,14 +2,14 @@ package es.uvigo.esei.xcs.jsf;
import java.security.Principal; import java.security.Principal;
import javax.faces.bean.ManagedBean; import javax.enterprise.context.RequestScoped;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext; import javax.faces.context.FacesContext;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ManagedBean(name = "login") @Named("login")
@RequestScoped @RequestScoped
public class LoginManagedBean { public class LoginManagedBean {
@Inject @Inject
......
...@@ -4,25 +4,25 @@ import static java.util.stream.Collectors.joining; ...@@ -4,25 +4,25 @@ import static java.util.stream.Collectors.joining;
import java.util.List; import java.util.List;
import javax.faces.bean.ManagedBean; import javax.enterprise.context.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext; import javax.faces.context.FacesContext;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named;
import es.uvigo.esei.xcs.domain.entities.Owner; import es.uvigo.esei.xcs.domain.entities.Owner;
import es.uvigo.esei.xcs.domain.entities.Pet; import es.uvigo.esei.xcs.domain.entities.Pet;
import es.uvigo.esei.xcs.service.OwnerService; import es.uvigo.esei.xcs.service.OwnerService;
@ManagedBean(name = "owner") @Named("owner")
@SessionScoped @RequestScoped
public class OwnerManagerdBean { public class OwnerManagedBean {
@Inject @Inject
private OwnerService service; private OwnerService service;
private String login; private String login;
private String password; private String password;
private Owner currentOwner; private boolean editing;
private String errorMessage; private String errorMessage;
...@@ -51,7 +51,11 @@ public class OwnerManagerdBean { ...@@ -51,7 +51,11 @@ public class OwnerManagerdBean {
} }
public boolean isEditing() { public boolean isEditing() {
return this.currentOwner != null; return this.editing;
}
public void setEditing(boolean editing) {
this.editing = editing;
} }
public List<Owner> getOwners() { public List<Owner> getOwners() {
...@@ -64,9 +68,9 @@ public class OwnerManagerdBean { ...@@ -64,9 +68,9 @@ public class OwnerManagerdBean {
.collect(joining(", ")); .collect(joining(", "));
} }
public String edit(Owner owner) { public String edit(String login) {
this.currentOwner = owner; this.editing = true;
this.login = this.currentOwner.getLogin(); this.login = login;
return this.getViewId(); return this.getViewId();
} }
...@@ -86,9 +90,10 @@ public class OwnerManagerdBean { ...@@ -86,9 +90,10 @@ public class OwnerManagerdBean {
public String store() { public String store() {
try { try {
if (this.isEditing()) { 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 { } else {
this.service.create(new Owner(login, password)); this.service.create(new Owner(login, password));
} }
...@@ -107,7 +112,7 @@ public class OwnerManagerdBean { ...@@ -107,7 +112,7 @@ public class OwnerManagerdBean {
this.login = null; this.login = null;
this.password = null; this.password = null;
this.errorMessage = null; this.errorMessage = null;
this.currentOwner = null; this.editing = false;
} }
private String redirectTo(String url) { private String redirectTo(String url) {
......
...@@ -4,17 +4,17 @@ import java.util.Date; ...@@ -4,17 +4,17 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.faces.bean.ManagedBean; import javax.enterprise.context.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext; import javax.faces.context.FacesContext;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named;
import es.uvigo.esei.xcs.domain.entities.AnimalType; import es.uvigo.esei.xcs.domain.entities.AnimalType;
import es.uvigo.esei.xcs.domain.entities.Pet; import es.uvigo.esei.xcs.domain.entities.Pet;
import es.uvigo.esei.xcs.service.PetService; import es.uvigo.esei.xcs.service.PetService;
@ManagedBean(name = "pet") @Named("pet")
@SessionScoped @RequestScoped
public class PetManagedBean { public class PetManagedBean {
@Inject @Inject
private PetService service; private PetService service;
...@@ -23,7 +23,7 @@ public class PetManagedBean { ...@@ -23,7 +23,7 @@ public class PetManagedBean {
private Date birth; private Date birth;
private AnimalType animal; private AnimalType animal;
private Pet currentPet; private Integer id;
private String errorMessage; private String errorMessage;
...@@ -62,7 +62,15 @@ public class PetManagedBean { ...@@ -62,7 +62,15 @@ public class PetManagedBean {
} }
public boolean isEditing() { 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<Pet> getPets() { public List<Pet> getPets() {
...@@ -70,10 +78,10 @@ public class PetManagedBean { ...@@ -70,10 +78,10 @@ public class PetManagedBean {
} }
public String edit(Pet pet) { public String edit(Pet pet) {
this.currentPet = pet; this.id = pet.getId();
this.name = this.currentPet.getName(); this.name = pet.getName();
this.birth = this.currentPet.getBirth(); this.birth = pet.getBirth();
this.animal = this.currentPet.getAnimal(); this.animal = pet.getAnimal();
return this.getViewId(); return this.getViewId();
} }
...@@ -93,11 +101,12 @@ public class PetManagedBean { ...@@ -93,11 +101,12 @@ public class PetManagedBean {
public String store() { public String store() {
try { try {
if (this.isEditing()) { if (this.isEditing()) {
this.currentPet.setName(this.name); final Pet pet = this.service.get(this.id);
this.currentPet.setBirth(this.birth); pet.setName(this.name);
this.currentPet.setAnimal(this.animal); pet.setBirth(this.birth);
pet.setAnimal(this.animal);
this.service.update(this.currentPet); this.service.update(pet);
} else { } else {
this.service.create(new Pet(name, animal, birth)); this.service.create(new Pet(name, animal, birth));
} }
...@@ -113,10 +122,10 @@ public class PetManagedBean { ...@@ -113,10 +122,10 @@ public class PetManagedBean {
} }
private void clear() { private void clear() {
this.currentPet = null;
this.name = null; this.name = null;
this.birth = null; this.birth = null;
this.animal = null; this.animal = null;
this.id = null;
this.errorMessage = null; this.errorMessage = null;
} }
......
...@@ -5,14 +5,33 @@ ...@@ -5,14 +5,33 @@
<display-name>Pet Store JSF</display-name> <display-name>Pet Store JSF</display-name>
<welcome-file-list> <welcome-file-list>
<welcome-file>/faces/index.xhtml</welcome-file> <welcome-file>index.xhtml</welcome-file>
</welcome-file-list> </welcome-file-list>
<!-- Change to "Production" when you are ready to deploy -->
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<!--Defining security constraint for type of roles available --> <!--Defining security constraint for type of roles available -->
<security-constraint> <security-constraint>
<web-resource-collection> <web-resource-collection>
<web-resource-name>admin</web-resource-name> <web-resource-name>admin</web-resource-name>
<url-pattern>/faces/admin/*</url-pattern> <url-pattern>/admin/*</url-pattern>
<http-method>POST</http-method> <http-method>POST</http-method>
<http-method>GET</http-method> <http-method>GET</http-method>
<http-method>PUT</http-method> <http-method>PUT</http-method>
...@@ -26,7 +45,7 @@ ...@@ -26,7 +45,7 @@
<security-constraint> <security-constraint>
<web-resource-collection> <web-resource-collection>
<web-resource-name>owner</web-resource-name> <web-resource-name>owner</web-resource-name>
<url-pattern>/faces/owner/*</url-pattern> <url-pattern>/owner/*</url-pattern>
<http-method>POST</http-method> <http-method>POST</http-method>
<http-method>GET</http-method> <http-method>GET</http-method>
<http-method>PUT</http-method> <http-method>PUT</http-method>
......
...@@ -18,8 +18,9 @@ ...@@ -18,8 +18,9 @@
<h:inputText id="name-field" class="form-control" a:placeholder="Owner name" value="#{owner.login}" readonly="#{owner.editing}"></h:inputText> <h:inputText id="name-field" class="form-control" a:placeholder="Owner name" value="#{owner.login}" readonly="#{owner.editing}"></h:inputText>
<h:outputLabel for="password-field">Password</h:outputLabel> <h:outputLabel for="password-field">Password</h:outputLabel>
<h:inputSecret id="password-field" class="form-control" a:placeholder="Owner password" value="#{owner.password}"></h:inputSecret> <h:inputSecret id="password-field" class="form-control" a:placeholder="Owner password" value="#{owner.password}"></h:inputSecret>
<h:commandButton id="submit-button" class="btn btn-default" value="Store" action="#{owner.store}" /> <h:inputHidden id="editing" value="#{owner.editing}"></h:inputHidden>
<h:commandButton id="cancel-button" class="btn btn-default" value="Cancel" action="#{owner.cancelEditing()}" rendered="#{owner.editing}"/> <h:commandButton id="submit-button" class="btn btn-default" value="Store" action="#{owner.store()}" />
<h:commandButton id="cancel-button" class="btn btn-default" value="Cancel" action="#{owner.cancelEditing()}" />
</h:form> </h:form>
</h:panelGroup> </h:panelGroup>
<div class="row"> <div class="row">
...@@ -50,7 +51,7 @@ ...@@ -50,7 +51,7 @@
<h:column> <h:column>
<h:form> <h:form>
<h:commandButton class="owners-table-remove" value="Remove" action="#{owner.remove(c.login)}"/> <h:commandButton class="owners-table-remove" value="Remove" action="#{owner.remove(c.login)}"/>
<h:commandButton class="owners-table-edit" value="Edit" action="#{owner.edit(c)}"/> <h:commandButton class="owners-table-edit" value="Edit" action="#{owner.edit(c.login)}"/>
</h:form> </h:form>
</h:column> </h:column>
</h:dataTable> </h:dataTable>
......
<?xml version='1.0' encoding='UTF-8' ?> <?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"> <!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" <html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:a="http://xmlns.jcp.org/jsf/passthrough"> xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
...@@ -23,8 +23,9 @@ ...@@ -23,8 +23,9 @@
<f:selectItem itemValue="CAT" itemLabel="Cat" /> <f:selectItem itemValue="CAT" itemLabel="Cat" />
<f:selectItem itemValue="DOG" itemLabel="Dog" /> <f:selectItem itemValue="DOG" itemLabel="Dog" />
</h:selectOneMenu> </h:selectOneMenu>
<h:inputHidden value="#{pet.id}"></h:inputHidden>
<h:commandButton id="submit" class="btn btn-default" value="Store" action="#{pet.store()}" /> <h:commandButton id="submit" class="btn btn-default" value="Store" action="#{pet.store()}" />
<h:commandButton id="cancel" class="btn btn-default" value="Cancel" action="#{pet.cancelEditing()}" rendered="#{pet.editing}"/> <h:commandButton id="cancel" class="btn btn-default" value="Cancel" action="#{pet.cancelEditing()}" />
</h:form> </h:form>
</h:panelGroup> </h:panelGroup>
<div class="row"> <div class="row">
......
...@@ -64,8 +64,8 @@ public class OwnerJsfTest { ...@@ -64,8 +64,8 @@ public class OwnerJsfTest {
.addAsWebResource(WEBAPP.resolve("admin/owners.xhtml").toFile(), "admin/owners.xhtml") .addAsWebResource(WEBAPP.resolve("admin/owners.xhtml").toFile(), "admin/owners.xhtml")
.addAsResource("test-persistence.xml", "META-INF/persistence.xml") .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
.addAsWebInfResource(WEBAPP.resolve("WEB-INF/template.xhtml").toFile()) .addAsWebInfResource(WEBAPP.resolve("WEB-INF/template.xhtml").toFile())
.addAsWebInfResource("jboss-web.xml") .addAsWebInfResource(WEBAPP.resolve("WEB-INF/web.xml").toFile())
.addAsWebInfResource("web.xml") .addAsWebInfResource(WEBAPP.resolve("WEB-INF/jboss-web.xml").toFile())
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
} }
...@@ -229,8 +229,6 @@ public class OwnerJsfTest { ...@@ -229,8 +229,6 @@ public class OwnerJsfTest {
assertDeleteOwner(ownerWithPets()); assertDeleteOwner(ownerWithPets());
} }
@Test @InSequence(36) @Test @InSequence(36)
@ShouldMatchDataSet("owners-remove-with-pets.xml") @ShouldMatchDataSet("owners-remove-with-pets.xml")
@CleanupUsingScript({ "cleanup.sql", "cleanup-autoincrement.sql" }) @CleanupUsingScript({ "cleanup.sql", "cleanup-autoincrement.sql" })
...@@ -246,6 +244,8 @@ public class OwnerJsfTest { ...@@ -246,6 +244,8 @@ public class OwnerJsfTest {
assertThat(ownersPage.areOwnersInTable(expectedOwners), is(true)); assertThat(ownersPage.areOwnersInTable(expectedOwners), is(true));
} }
@Test @InSequence(41) @Test @InSequence(41)
@UsingDataSet("owners.xml") @UsingDataSet("owners.xml")
@Cleanup(phase = TestExecutionPhase.NONE) @Cleanup(phase = TestExecutionPhase.NONE)
...@@ -268,7 +268,7 @@ public class OwnerJsfTest { ...@@ -268,7 +268,7 @@ public class OwnerJsfTest {
} }
@Test @InSequence(43) @Test @InSequence(43)
@ShouldMatchDataSet("owners-update-password.xml") @ShouldMatchDataSet(value = "owners-update-password.xml")
@CleanupUsingScript({ "cleanup.sql", "cleanup-autoincrement.sql" }) @CleanupUsingScript({ "cleanup.sql", "cleanup-autoincrement.sql" })
public void afterEdit() {} public void afterEdit() {}
......
...@@ -8,7 +8,7 @@ import org.jboss.arquillian.graphene.page.Location; ...@@ -8,7 +8,7 @@ import org.jboss.arquillian.graphene.page.Location;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.FindBy;
@Location("faces/index.xhtml") @Location("index.xhtml")
public class LoginPage { public class LoginPage {
@Drone @Drone
private WebDriver browser; private WebDriver browser;
...@@ -21,6 +21,6 @@ public class LoginPage { ...@@ -21,6 +21,6 @@ public class LoginPage {
} }
public void assertOnLoginPage() { public void assertOnLoginPage() {
assertThat(browser.getCurrentUrl(), containsString("/faces/index.xhtml")); assertThat(browser.getCurrentUrl(), containsString("/index.xhtml"));
} }
} }
package es.uvigo.esei.xcs.jsf.pages; package es.uvigo.esei.xcs.jsf.pages;
import static java.util.Arrays.stream;
import static org.hamcrest.core.StringContains.containsString; import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
...@@ -25,16 +26,11 @@ public class OwnersPage { ...@@ -25,16 +26,11 @@ public class OwnersPage {
private GrapheneElement storeError; private GrapheneElement storeError;
public void assertOnOwnersPage() { public void assertOnOwnersPage() {
assertThat(browser.getCurrentUrl(), containsString("/faces/admin/owners.xhtml")); assertThat(browser.getCurrentUrl(), containsString("/owners.xhtml"));
} }
public boolean areOwnersInTable(Owner ... owners) { public boolean areOwnersInTable(Owner ... owners) {
for (Owner owner : owners) { return stream(owners).allMatch(this::isOwnerInTable);
if (!this.isOwnerInTable(owner))
return false;
}
return true;
} }
public boolean isOwnerInTable(Owner owner) { public boolean isOwnerInTable(Owner owner) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment