Unable to make it working a JSF test project

Viewed 133

I tried to create a test project for JSF (Primefaces), but it does not work. The test view does not call the managed bean.

This is the managed bean:

package it.myPrimefacesTest.controller;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ViewScoped
@ManagedBean(name="helloWorld", eager=true)
public class HelloWorld implements Serializable {
    private static final long serialVersionUID = 7184399511928573344L;
    
   @SuppressWarnings("static-method")
   public String getMessage() {
      return "Hello World!";
   }
}

This is test.xhtml:

<!DOCTYPE html>

<f:view xmlns="http://www.w3.org/1999/xhtml"
   xmlns:ui="http://java.sun.com/jsf/facelets"
   xmlns:f="http://java.sun.com/jsf/core"
   xmlns:h="http://java.sun.com/jsf/html"
   contentType="text/html"
>
   
<html>
<h:head></h:head>
    
<body>
    ciao!
      
    <h:inputText></h:inputText>
    <h:outputText value="#{helloWorld.getMessage()}"></h:outputText>
</body>
</html>
</f:view>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>MyPrimefacesTest</display-name>
  <servlet>
    <servlet-name>MyPrimefacesTest</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>MyPrimefacesTest</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>
  
  <servlet-mapping>
    <servlet-name>MyPrimefacesTest</servlet-name>
    <url-pattern>*.jsf</url-pattern>
  </servlet-mapping>
  
  <context-param> 
    <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
    <param-value>true</param-value>
  </context-param>
  
    <welcome-file-list>
        <welcome-file>test.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

faces-config.xml:

<?xml version="1.0" encoding="UTF-8"?>

<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd"
    version="2.1"
>

</faces-config>

<h:outputText value="#{helloWorld.getMessage()}"></h:outputText> in test.xhtml does not work.

I'm using:

  • Java 7
  • Eclipse
  • JBoss 7.1.1

This is because my project was created in this way, and I want to be 1:1.

This is the project: https://ufile.io/ihd8j37g . You have to click on Free Download and then on Slow Speed.

2 Answers

If you try to examine your deployed ear, you'll see that inside MyPrimefacesTest.war there's only PF jar, faces-config.xml and web.xml, no .class file.
Same thing if you export your project into an ear file.
Since the compiled code isn't deployed it's impossible to run it at runtime.
If you export your MyPrimefacesTest output folder, in your war, you'll see the problem resolved.
To do it you can try replacing

<classpathentry kind="src" path="src"/>

with

<classpathentry kind="src" output="build/classes" path="src"/>

in .classpath file of MyPrimefacesTest.

Depending on your need you can choose between many ways to obtain this goal:

  • Modify your classpath
  • Modify Manifest
  • If you use maven you can modify pom.xml according to your need, same thing for gradle or if you use ant script

Remember to clean your server, before deploying a new ear (with lower jBoss version it's usefull to clean also deployment dir).

Fixed. Thanks to WoAiNii for suggesting me in the right direction. This is what I have done in Project -> Properties:

  1. Java Build path -> Source -> Default output folder: set to MyPrimefacesTest/build/classes
  2. Deployment Assembly -> Add -> Folder -> src, then I set the Deploy path to WEB-INF/classes
  3. reboot the PC (for superstition...)
  4. go to the JBoss standalone folder, deleted the subfolders data, log and tmp and empty the deployments folder
  5. start Eclipse
  6. Clean the project and the EAR project
  7. Go to Servers. If you servers has other EARs than this one, remove them. Make sure to clear the other servers before start this one.
  8. clear the server
  9. start the server
  10. pray :D
Related