0

Target unreachable error is coming, somehow my bean is not initializing and the value of the bean is coming null. My helloBean reference is coming null.

POM.xml

<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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.rout.sagar</groupId>
 <artifactId>DemoMavenJAVAEE</artifactId>
 <packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>DemoMavenJAVAEE Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-api</artifactId>
    <version>2.1.7</version>
</dependency>
<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-impl</artifactId>
    <version>2.1.7</version>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>


    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
    </dependency>
            <!-- Tomcat 6 need this -->
    <dependency>
        <groupId>com.sun.el</groupId>
        <artifactId>el-ri</artifactId>
        <version>1.0</version>
    </dependency>

 </dependencies>
 <build>
<finalName>DemoMavenJAVAEE</finalName>

    </build>
</project>

HelloBean.java

package com.rout.sagar;

   import javax.faces.bean.ManagedBean;
   import javax.faces.bean.SessionScoped;
   import java.io.Serializable;

   @ManagedBean
   @SessionScoped
   public class HelloBean implements Serializable{
private static final long serialVersionUID = 1L ; 
private HelloBean hello ;

private String name ; 
public String getName() {
    return name ;
}

public void setName(String name) {
    this.name = name ;
}}

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" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<display-name>DemoMavenJAVAEE</display-name>

<!-- Change to "Production" when you are ready to deploy -->



<context-param>
<param-name>org.apache.myfaces.annotation.SCAN_PACKAGES</param-name>
 <param-value>com.rout.sagar.HelloBean</param-value>
</context-param>
<!-- Welcome page -->
<welcome-file-list>
    <welcome-file>faces/hello.xhtml</welcome-file>
</welcome-file-list>

<!-- 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>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>

faces-config.xml

<?xml version="1.0"?>
    <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_0.xsd"
    version="2.0">

    <navigation-rule>
<from-view-id>/hello.xhtml</from-view-id>
<navigation-case>
    <from-outcome>success</from-outcome>
    <to-view-id>/welcome.xhtml</to-view-id>
</navigation-case>
</navigation-rule>

</faces-config>

hello.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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">

 <h:head>
<title>JSF 2.0 Hello World</title>
</h:head>
<h:body>
<h3>JSF 2.0 Hello World Example - hello.xhtml</h3>
<h:form>
    <h:inputText value="#{helloBean.name}"></h:inputText>
    <h:commandButton value="Welcome Me" action="welcome"></h:commandButton>
</h:form>
</h:body>
 </html>

Welcome.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://java.sun.com/jsf/html">


<h:head>
  <title>JSF2.0 hello World    </title>
</h:head>     
  <h:body bgcolor = "red">
  <h2>JSF 2.0 Hello World Example welcome.xhtml</h2>
  <h2>Welcome #{helloBean.name}</h2>
  </h:body>
  </html>

Folder Structure in Eclipse

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Sagar Rout
  • 536
  • 8
  • 22

1 Answers1

1

You placed the Java source file of the backing bean in src/main/resources instead of in src/main/java. So the Java source file won't be compiled into a .class file in /WEB-INF/lib during the build of the WAR file.

Fix your wrong Maven project structure. I think the red cross on the project icon is also hinting to the problem in the project structure.

See also:


Unrelated to the concrete problem: your pom shouldn't let the webapp provide libraries which are already provided by the target server itself, such as JSP, Servlet and EL in case of Tomcat. And, that JSF impl version is ancient.

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • Yeah, it did work. I tried every thing which you mention in the first link but i have no idea how maven's structure work. – Sagar Rout Jun 28 '15 at 16:30