Aggiunto l'esempio del capitolo su Java Server Faces

This commit is contained in:
Fabio Scotto di Santolo
2019-04-05 23:14:28 +02:00
parent 3d0faaa090
commit 9de150ccdc
21 changed files with 430 additions and 1 deletions

View File

@@ -0,0 +1,9 @@
<component name="ArtifactManager">
<artifact type="exploded-ejb" name="jsfdemo:ejb exploded">
<output-path>$PROJECT_DIR$/out/artifacts/jsfdemo_ejb_exploded</output-path>
<root id="root">
<element id="javaee-facet-resources" facet="jsfdemo/ejb/EJB" />
<element id="module-output" name="jsfdemo" />
</root>
</artifact>
</component>

23
.idea/artifacts/jsfdemo_war_exploded.xml generated Normal file
View File

@@ -0,0 +1,23 @@
<component name="ArtifactManager">
<artifact type="exploded-war" name="jsfdemo:war exploded">
<output-path>$PROJECT_DIR$/jsfdemo/target/jsfdemo-1.0-SNAPSHOT</output-path>
<properties id="maven-jee-properties">
<options>
<exploded>true</exploded>
<module>jsfdemo</module>
<packaging>war</packaging>
</options>
</properties>
<root id="root">
<element id="directory" name="WEB-INF">
<element id="directory" name="classes">
<element id="module-output" name="jsfdemo" />
</element>
</element>
<element id="directory" name="META-INF">
<element id="file-copy" path="$PROJECT_DIR$/jsfdemo/target/jsfdemo-1.0-SNAPSHOT/META-INF/MANIFEST.MF" />
</element>
<element id="javaee-facet-resources" facet="jsfdemo/web/Web" />
</root>
</artifact>
</component>

1
.idea/compiler.xml generated
View File

@@ -9,6 +9,7 @@
<outputRelativeToContentRoot value="true" />
<module name="cdidemo" />
<module name="jpademo" />
<module name="jsfdemo" />
<module name="beanvalidationdemo" />
<module name="jmsdemo" />
<module name="jaxwsdemo-client" />

1
.idea/encodings.xml generated
View File

@@ -9,6 +9,7 @@
<file url="file://$PROJECT_DIR$/jaxwsdemo-service" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/jmsdemo" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/jpademo" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/jsfdemo" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/xmlandjsondemo" charset="UTF-8" />
</component>
</project>

1
.idea/misc.xml generated
View File

@@ -4,6 +4,7 @@
<component name="FrameworkDetectionExcludesConfiguration">
<file type="web" url="file://$PROJECT_DIR$/jaxrsdemo" />
<file type="web" url="file://$PROJECT_DIR$/jaxwsdemo-service" />
<file type="web" url="file://$PROJECT_DIR$/jsfdemo" />
</component>
<component name="GOROOT" path="/usr/lib/go" />
<component name="Kotlin2JsCompilerArguments">

View File

@@ -4,7 +4,9 @@
<facet type="jpa" name="JPA">
<configuration>
<setting name="validation-enabled" value="true" />
<datasource-mapping />
<datasource-mapping>
<factory-entry name="jaxrsoPU" />
</datasource-mapping>
<naming-strategy-map />
<deploymentDescriptor name="persistence.xml" url="file://$MODULE_DIR$/src/main/resources/META-INF/persistence.xml" />
</configuration>

35
jsfdemo/jsfdemo.iml Normal file
View File

@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="FacetManager">
<facet type="jpa" name="JPA">
<configuration>
<setting name="validation-enabled" value="true" />
<datasource-mapping />
<naming-strategy-map />
<deploymentDescriptor name="persistence.xml" url="file://$MODULE_DIR$/src/main/resources/META-INF/persistence.xml" />
</configuration>
</facet>
<facet type="ejb" name="EJB">
<configuration>
<ejbRoots>
<root url="file://$MODULE_DIR$/src/main/java" />
</ejbRoots>
</configuration>
</facet>
<facet type="web" name="Web">
<configuration>
<descriptors>
<deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/src/main/webapp/WEB-INF/web.xml" />
</descriptors>
<webroots>
<root url="file://$MODULE_DIR$/src/main/webapp" relative="/" />
</webroots>
<sourceRoots>
<root url="file://$MODULE_DIR$/src/main/java" />
<root url="file://$MODULE_DIR$/src/main/resources" />
<root url="file://$MODULE_DIR$/target/generated-sources/annotations" />
</sourceRoots>
</configuration>
</facet>
</component>
</module>

61
jsfdemo/pom.xml Normal file
View File

@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<artifactId>jsfdemo</artifactId>
<packaging>war</packaging>
<name>jsfdemo</name>
<parent>
<artifactId>jeedemo</artifactId>
<groupId>it.plague.jeedemo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,47 @@
package it.plague.jeedemo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@XmlRootElement
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@NamedQuery(name = Book.FIND_ALL, query = "select b from Book b")
public class Book {
public static final String FIND_ALL = "FIND_ALL";
@Id
@GeneratedValue
private Long id;
@NotNull
@Size(min = 4, max = 50)
@Column(nullable = false)
private String title;
private Float price;
@Column(length = 2000)
private String description;
private String isbn;
private Integer nbOfPage;
private Boolean illustrations;
}

View File

@@ -0,0 +1,35 @@
package it.plague.jeedemo;
import java.util.List;
import javax.annotation.sql.DataSourceDefinition;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
@Named
@Stateless
@DataSourceDefinition(
name = "java:global/jdbc/lab11DS",
className = "org.apache.derby.jdbc.EmbeddedDriver",
url = "jdbc:derby:memory:lab11DB;create=true;user=app;password=app"
)
public class BookEJB {
@Inject
private EntityManager em;
public Book createBook(Book book) {
em.persist(book);
return book;
}
public List<Book> findAllBooks() {
return em.createNamedQuery(Book.FIND_ALL, Book.class).getResultList();
}
public Book findBookById(Long id) {
return em.find(Book.class, id);
}
}

View File

@@ -0,0 +1,36 @@
package it.plague.jeedemo;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Named
@RequestScoped
@NoArgsConstructor
public class BookView {
@Inject
private BookEJB bookEJB;
@Getter
@Setter
private Book book = new Book();
public void doFindBookById() {
book = bookEJB.findBookById(book.getId());
}
public String doCreateBook() {
bookEJB.createBook(book);
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_INFO, "Book created",
"The book " + book.getTitle() + " has been created with id = " + book.getId()));
return "newBook.xhtml";
}
}

View File

@@ -0,0 +1,12 @@
package it.plague.jeedemo;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
public class DatabaseProducer {
@Produces
@PersistenceContext(unitName = "jsfdemoPU")
private EntityManager em;
}

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="jsfdemoPU" transaction-type="JTA">
<jta-data-source>java:global/jdbc/lab11DS</jta-data-source>
<properties>
<property name="eclipselink.target-database" value="DERBY"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.logging.level" value="ALL"/>
</properties>
</persistence-unit>
</persistence>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans
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/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>

View File

@@ -0,0 +1,7 @@
<?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">
</faces-config>

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>

View File

@@ -0,0 +1,22 @@
<?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:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title><ui:insert name="title">Defautl title</ui:insert></title>
</h:head>
<h:body>
<h:link value="Create a book" outcome="newBook.xhtml"/>
<h1><ui:insert name="title">Default title</ui:insert></h1>
<hr/>
<h:messages id="errors" infoStyle="color: blue" warnStyle="color: orange"
errorStyle="color: red"/>
<ui:insert name="content">Default content</ui:insert>
<hr/>
<h:outputText value="APress - Beginning Java EE 7" style="font-style: italic"/>
</h:body>
</html>

View File

@@ -0,0 +1,47 @@
<?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:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<ui:composition template="layout.xhtml">
<ui:define name="title">Create new book</ui:define>
<ui:define name="content">
<h:form id="bookForm">
<h:panelGrid columns="2">
<h:outputLabel value="Title: "/>
<h:inputText value="#{bookView.book.title}"/>
<h:outputLabel value="Price: "/>
<h:inputText value="#{bookView.book.price}"/>
<h:outputLabel value="Description: "/>
<h:inputTextarea value="#{bookView.book.description}" cols="16" rows="3"/>
<h:outputLabel value="Number of pages: "/>
<h:inputText value="#{bookView.book.nbOfPage}"/>
<h:outputLabel value="Illustrations: "/>
<h:selectBooleanCheckbox value="#{bookView.book.illustrations}"/>
</h:panelGrid>
<h:commandButton value="Create a book" action="#{bookView.doCreateBook}">
<f:ajax execute="@form" render=":booklist :errors"/>
</h:commandButton>
</h:form>
<hr/>
<h1>List of books</h1>
<h:dataTable id="booklist" value="#{bookEJB.findAllBooks()}" var="bk" border="1">
<h:column>
<f:facet name="header">
<h:outputText value="Title"/>
</f:facet>
<h:link outcome="viewBook.xhtml?id=#{bk.id}" value="#{bk.title}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Price"/>
</f:facet>
<h:outputText value="#{bk.price}"/>
</h:column>
</h:dataTable>
</ui:define>
</ui:composition>
</html>

View File

@@ -0,0 +1,30 @@
<?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:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<ui:composition template="layout.xhtml">
<f:metadata>
<f:viewParam name="id" value="#{bookView.book.id}"/>
<f:viewAction action="#{bookView.doFindBookById}"/>
</f:metadata>
<ui:define name="title">View a book</ui:define>
<ui:define name="content">
<h:panelGrid columns="2">
<h:outputLabel value="Title: "/>
<h:outputText value="#{bookView.book.title}"/>
<h:outputLabel value="Price: "/>
<h:outputText value="#{bookView.book.price}"/>
<h:outputLabel value="Description: "/>
<h:outputText value="#{bookView.book.description}"/>
<h:outputLabel value="Number of pages: "/>
<h:outputText value="#{bookView.book.nbOfPage}"/>
<h:outputLabel value="Illustrations: "/>
<h:selectBooleanCheckbox value="#{bookView.book.illustrations}"/>
</h:panelGrid>
</ui:define>
</ui:composition>
</html>

View File

@@ -0,0 +1,20 @@
package it.plague.jeedemo;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
}

View File

@@ -17,6 +17,7 @@
<module>jmsdemo</module>
<module>jaxwsdemo-service</module>
<module>jaxrsdemo</module>
<module>jsfdemo</module>
</modules>
<dependencyManagement>