Introduction
Here is a simple guide to get started with JSF 2.2 development with JBoss EAP 7.
Maven
Lets start with maven war pom for Java EE 7. See also Minimalistic POM for Java EE 7
<?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> <groupId>se.magnuskkarlsson.netbeans.examples</groupId> <artifactId>example-ee7-web</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.outputEncoding>UTF-8</project.build.outputEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <failOnMissingWebXml>false</failOnMissingWebXml> </properties> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <!-- Test Support --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> </build> </project>
Java EE 7 Deployment Descriptors
The deployment descriptors used here are. See also Java EE 7 Deployment Descriptors:
- src/main/webapp/WEB-INF/web.xml - Not required, but will probably later be used. EMPTY
- src/main/webapp/WEB-INF/faces-config.xml - Is required for JSF to work in EAP. EMPTY
- src/main/webapp/WEB-INF/beans.xml - Is required for CDI to work. EMPTY
src/main/webapp/WEB-INF/web.xml
<?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_3_1.xsd" version="3.1"> </web-app>
src/main/webapp/WEB-INF/faces-config.xml
<?xml version="1.0" encoding="UTF-8"?> <faces-config 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" version="2.2"> </faces-config>
src/main/webapp/WEB-INF/beans.xml
<?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" version="1.1" bean-discovery-mode="all"> </beans>
JSF Managed Bean
Do not use JSF @javax.faces.bean.ManagedBean, instead use standard CDI. It's more generic and you only need to learn one technique.
src/main/java/se/magnuskkarlsson/netbeans/example/ee7/web/Person.java
package se.magnuskkarlsson.netbeans.example.ee7.web; import java.util.Date; import javax.enterprise.context.RequestScoped; import javax.inject.Named; import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import javax.validation.constraints.Past; import javax.validation.constraints.Size; @Named @RequestScoped public class Person { @Size(min = 1, max = 256) private String firstName; @Size(min = 1, max = 256) private String lastName; @Min(12) @Max(100) private int age; @NotNull @Past private Date birthdate; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthdate() { return birthdate; } public void setBirthdate(Date birthdate) { this.birthdate = birthdate; } }
JavaServer Faces/JSF Page
src/main/webapp/person.xhtml
<?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> <title>Facelet Title</title> </h:head> <h:body> <h:form> <!-- <h:messages style="color:red;" showDetail="true" showSummary="true" /> --> <h:panelGrid columns="3"> <h:outputLabel for="firstName" value="First Name: " /> <h:inputText id="firstName" value="#{person.firstName}" /> <h:message for="firstName" style="color: red;" /> <h:outputLabel for="lastName" value="Last Name: " /> <h:inputText id="lastName" value="#{person.lastName}" /> <h:message for="lastName" style="color: red;" /> <h:outputLabel for="age" value="Age: "/> <h:inputText id="age" value="#{person.age}" /> <h:message for="age" style="color: red;" /> <h:outputLabel for="birthdate" value="Birthdate: " /> <h:inputText id="birthdate" value="#{person.birthdate}" label="birthdate"> <f:convertDateTime pattern="yyyy-MM-dd" /> </h:inputText> <h:message for="birthdate" style="color: red;" /> </h:panelGrid> <h:commandButton value="Save" action="confirmation" /> </h:form> </h:body> </html>
Test
Now lets test standard $JBOSS_HOME/bin/standalone.sh
You can access the jsf page from two URL: http://localhost:8080/example-ee7-web/person.jsf and http://localhost:8080/example-ee7-web/faces/person.xhtml
No comments:
Post a Comment