14 septiembre, 2020

Configurar un proyecto Java Web en Eclipse usando JSF 2.3 (Maven)

Herramietas

Eclipse IDE

WildFly Server 20.0

Pasos

1. Crear un Maven Project

File / New / Other / Maven Project

Creamos un proyecto simple y lo configuramos




2. Modificamos el archivo pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jsf</groupId>
  <artifactId>proyectoJSF</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
 
  <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.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>8.0</version>
          <scope>provided</scope>
      </dependency>
  </dependencies>
 
  <build>
      <finalName>proyectoJSF</finalName>
      <pluginManagement><!-- lock down plugins versions to avoid using Maven
              defaults (may be moved to parent pom) -->
          <plugins>
              <plugin>
                  <artifactId>maven-clean-plugin</artifactId>
                  <version>3.1.0</version>
              </plugin>
              <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
              <plugin>
                  <artifactId>maven-resources-plugin</artifactId>
                  <version>3.0.2</version>
              </plugin>
              <plugin>
                  <artifactId>maven-compiler-plugin</artifactId>
                  <version>3.8.0</version>
              </plugin>
              <plugin>
                  <artifactId>maven-surefire-plugin</artifactId>
                  <version>2.22.1</version>
              </plugin>
              <plugin>
                  <artifactId>maven-war-plugin</artifactId>
                  <version>3.2.2</version>
              </plugin>
              <plugin>
                  <artifactId>maven-install-plugin</artifactId>
                  <version>2.5.2</version>
              </plugin>
              <plugin>
                  <artifactId>maven-deploy-plugin</artifactId>
                  <version>2.8.2</version>
              </plugin>
          </plugins>
      </pluginManagement>
  </build>
</project>

Guardamos los cambios y esperamos a que termine de bajar las dependencias y se haya actualizado el proyecto


3. Configuramos la facetas del proyecto

Clic derecho al proyecto / Build Path / Configure Build Path / Project Facets


Clic en Further configuration required...




Aplicamos y cerramos


Si el Eclipse les sigue marcando algún error, clic derecho al proyecto / Maven / Update Project

Ahora movemos la carpeta META-INF dentro de la carpeta resources



4. Creamos el siguiente Managed Bean

SaludoView.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package proyectoJSF.view;
 
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
 
@Named
@RequestScoped
public class SaludoView {
 
    private String input;
    private String output;
 
    public void submit() {
        output = "Hola estimado " + input;
    }
 
    public String getInput() {
        return input;
    }
 
    public void setInput(String input) {
        this.input = input;
    }
 
    public String getOutput() {
        return output;
    }
}

5. Creamos las siguientes páginas

index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Proyecto Demo de JSF</title>
</head>
<body>
    <h1>Probando JSF</h1>
    <hr />
    <a href="prueba1.xhtml">Prueba #1</a>
</body>
</html>

prueba1.xhtml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
<h:head>
    <title>Prueba 01</title>
</h:head>
<h:body>
    <h1>Saludando</h1>
    <h:form>
        <h:outputLabel for="input" value="Input" />
        <h:inputText id="input" value="#{saludoView.input}" />
        <h:commandButton value="Submit" action="#{saludoView.submit}">
            <f:ajax execute="@form" render=":output" />
        </h:commandButton>
    </h:form>
    <h:outputText id="output" value="#{saludoView.output}" />
</h:body>
</html>


Probar


Descargar proyecto

No hay comentarios, ¡cuéntame algo!

Me gustaría saber tu opinión. ¡Saludos!