samedi 27 juin 2015

Bean : Target unreachable , identifier bean resolved to null - JSF

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://ift.tt/IH78KX"           xmlns:xsi="http://ift.tt/ra1lAU"
  xsi:schemaLocation="http://ift.tt/IH78KX    http://ift.tt/HBk9RF">
 <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://ift.tt/19pvvEY;
<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://ift.tt/ra1lAU"
xmlns="http://ift.tt/nSRXKP" 
    xmlns:web="http://ift.tt/LU8AHS"
xsi:schemaLocation="http://ift.tt/nSRXKP 
http://ift.tt/LU8AHS"
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://ift.tt/nSRXKP"
    xmlns:xsi="http://ift.tt/ra1lAU"
    xsi:schemaLocation="http://ift.tt/nSRXKP
    http://ift.tt/1cpkFEe"
    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://ift.tt/kkyg93">
    <html xmlns="http://ift.tt/lH0Osb"
xmlns:f="http://ift.tt/HcrI1S"
xmlns:h="http://ift.tt/HjFrZb">

 <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://ift.tt/kkyg93">
  <html xmlns="http://ift.tt/lH0Osb"    
  xmlns:h="http://ift.tt/HjFrZb">


<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

Aucun commentaire:

Enregistrer un commentaire