2

I am new to JSF and its associate.

My development environment as follows Java 6 Tomcat 6 Eclipse Primeface 2.5

I am trying a test Web application described in instant primefaces starter.

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

<display-name>PrimeFaces Web Application</display-name>

<!-- Change to "Production" when you are ready to deploy -->
<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>

<!-- Welcome page -->
<welcome-file-list>
    <welcome-file>faces/index.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>

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">

beans.xml

Is an empty file

index.xhtml

<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head />
<body>
<ui:composition template="/WEB-INF/templates/main.xhtml">
    <ui:define name="title">Welcome to our site</ui:define>
    <ui:define name="content">
        <h:form>
            <h:panelGrid columns="2" border="0">
                <p:panel header="Please Sign In">
                    <p:panelGrid columns="2" styleClass="no-border"
                        columnClasses="no-border,no-border">
                        <p:outputLabel value="Username" for="username" />
                        <p:inputText id="username" label="Username"
                            value="#{credentials.username}" />
                        <p:outputLabel value="Password" for="password" />
                        <p:password id="password" label="Password"
                            value="#{credentials.password}" />
                        <h:outputText value="" />
                        <p:commandLink value="Forgot your password?" />
                        <h:outputText value="" />
                        <p:commandButton value="Login" action="members"
                            actionListener="#{loginController.login}" />
                        <h:outputText value="" />
                    </p:panelGrid>
                </p:panel>
                <ul>
                    <li>Find out what's happening</li>
                    <li>Rate and review shows</li>
                    <li>Invite your friends</li>
                    <li><a href="/faces/register.xhtml">Join today!</a>
                    </li>
                </ul>
            </h:panelGrid>
        </h:form>
    </ui:define>
</ui:composition>
</body>
</html>

loginController.java

package com.example.starterexample.controller;

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

import com.example.starterexample.model.Credentials;

/**
 * Controller class for desktop and mobile login functionality.
 * 
 * @uml.dependency supplier="com.mycompany.controller.AbstractController"
 */
@ManagedBean(name="loginController", eager=true)
//@ViewScoped
@RequestScoped
/*
 * @ViewScoped  means the object is created by JSF 
 *              and its lifecycle is scoped to the current view.
 *          Once the user navigates to another view, the LoginControllerobject     is released and 
 *          will eventually be garbage-collected by the JVM
 */
public class LoginController extends AbstractController {

private static final long serialVersionUID = -6710063228036238737L;

@ManagedProperty(value = "#{credentials}")
private Credentials credentials;

public String login() {
    String outcome = null;
    try {
        String username = credentials.getUsername();
        String password = credentials.getPassword();
        //User user = userService.findUser(username, password);
        //if (user == null) {
        //  addErrorMessage("Invalid login");
        //  userSession.setUser(null);
        //} else {
        //  userSession.setUser(user);
        //  if (!activeUsers.contains(user)) {
        //      activeUsers.add(user);
        //  }
            //outcome = "members";
        //}

            addErrorMessage("Invalid login");
    } catch (Exception e) {
        logger.error("Unable to login:", e);
    }
    return outcome;
}

public void setCredentials(Credentials credentials) {
    this.credentials = credentials;
}


}

Credentials.java

package com.example.starterexample.model;

import javax.enterprise.inject.Model;

@Model
public class Credentials {

private String username;
private String password;

public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}


}

Error:

Sep 21, 2013 5:03:34 PM com.sun.faces.lifecycle.ProcessValidationsPhase execute
WARNING: /index.xhtml @19,67 value="#{credentials.username}": Target Unreachable, identifier 'credentials' resolved to null
javax.el.PropertyNotFoundException: /index.xhtml @19,67 value="#{credentials.username}": Target Unreachable, identifier 'credentials' resolved to null
at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
at             org.primefaces.renderkit.InputRenderer.findImplicitConverter(InputRenderer.java:170)
at org.primefaces.renderkit.InputRenderer.findConverter(InputRenderer.java:190)
at org.primefaces.renderkit.InputRenderer.getConvertedValue(InputRenderer.java:196)
at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
at javax.faces.component.UIInput.validate(UIInput.java:960)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
at javax.faces.component.UIInput.processValidators(UIInput.java:698)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at org.primefaces.component.panel.Panel.processValidators(Panel.java:297)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIForm.processValidators(UIForm.java:253)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)

Please help me to find what am I missing.

I have looked for similar problems, nothing worked for me.

I have checked the created war file and class files are found in WEB-INF/classes

Thanks in advance.

Sasi
  • 37
  • 1
  • 4

3 Answers3

2

I also use this example for self-learning etc. Actually i faced same problems as you. This sample has errors so you should not use it blindly. This article helped me to better understand beans types (CDI,EJB,JSF beans) and to code workable code.

You should not use JSF managed bean with CDI. CDI beans are more powerfull itself. This worked for me.

LoginController starts like

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;


@Named
@javax.faces.view.ViewScoped
public class LoginController extends AbstractController {


    private static final long serialVersionUID = -6710063228036238737L;


        @Inject
    private ActiveUsers activeUsers;

        @Inject
    private Credentials credentials;

        @Inject
    private UserService userService;

....
}

ActiveUsers looks like

import java.util.ArrayList;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

@Named("activeUsers")
@ApplicationScoped
public class ActiveUsers extends ArrayList<User> {

    private static final long serialVersionUID = -3068333669859836249L;

}

Credentials:

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named("credentials")
@RequestScoped
public class Credentials {

    private String password;

    private String username;

    public String getPassword() {
        return password;
    }

    public String getUsername() {
        return username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

}

Hope this will help you

VLAD
  • 21
  • 3
1

You're not allowed to do what you're trying to: access a CDI bean using a JSF annotation. javax.enterprise.inject.model is a CDI based annotation so, depending on your version of JSF, you have the following options:

  1. Convert loginController to a CDI bean using @Named and then use @Inject to get a hold of the model - JSF 2.1

  2. Upgrade to JSF 2.2 that lets you use @Injectand @ManagedProperty with CDI beans

kolossus
  • 20,559
  • 3
  • 52
  • 104
  • Hi, sorry for the late reply, could you please send me a sample working one, or will you be able to have a look at my project code and help, please send me your email id to mail the project. – Sasi Oct 05 '13 at 03:30
0

Only ManagedBeans are accessible in the faces context. Thus you will have to access the credentials via the loginController, e.g. #{loginController.credentials.username}. You also need to add a getCredentials() method to the LoginController.

morja
  • 8,297
  • 2
  • 39
  • 59
  • You possibly also need a getCredentials() method in the LoginController. – morja Sep 21 '13 at 13:44
  • As you suggested I tried modifying the expression and got following error. Sep 21, 2013 7:16:17 PM com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException SEVERE: Error Rendering View[/index.xhtml] javax.el.PropertyNotFoundException: /index.xhtml @19,83 value="#{loginController.credentials.username}": Missing Resource in EL implementation: ???propertyNotReadable??? at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:111) .... – Sasi Sep 21 '13 at 13:46
  • Yes, see my previous comment and edit of the answer. You need a getter for the credentials. – morja Sep 21 '13 at 13:48
  • No luck, I have included the getCredentials method in LoginControlle. Page got loaded, but when press login button, getting my initial error. – Sasi Sep 21 '13 at 13:52
  • This means the credentials are still null. This must have to do with the injection. I have not tried this kind of auto wiring. You have to find out how to inject the Credentials with @ManagedProperty(value = "#{credentials}"). I guess "#{credentials}" does not exist at that point. – morja Sep 21 '13 at 13:57
  • I think your credentials would also have to be a managed bean. – morja Sep 21 '13 at 14:00