0

I'm having problem accessing my managed beans in JSF pages. This is my jsf:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
            xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
            xmlns:h="http://xmlns.jcp.org/jsf/html"
            xmlns:p="http://xmlns.jcp.org/jsf/passthrough"
            template="./template.xhtml">

<ui:define name="left">
    <h:form>
        <h:inputText value="#{UserManager.user.username}" p:placeholder="Username" /><br /><br />
        <h:inputSecret value="#{UserManager.user.password}" p:placeholder="Password" /><br /><br />
        <h:inputText value="#{UserManager.user.firstname}" p:placeholder="Förnamn" /><br /><br />
        <h:inputText value="#{UserManager.user.lastname}" p:placeholder="Efternamn" /><br /><br />
        <h:inputText value="#{UserManager.user.phonenumber}" p:placeholder="Telefonnummer" /><br /><br />
        <h:commandButton value="Register" action="#{UserManager.save}" />
    </h:form>
    <br /><br />

    <h:form>
        <h:commandLink action="#{UserManager.testDispatch}" value="Test Dispatch" />
    </h:form>
</ui:define>

<ui:define name="right">
    right
</ui:define>

This is the template:

<?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:ui="http://xmlns.jcp.org/jsf/facelets"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:f="http://xmlns.jcp.org/jsf/html"
  xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">

<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <h:outputStylesheet library="css" name="bootstrap.min.css"/>
    <h:outputStylesheet library="css" name="bootstrap-theme.min.css"/>
    <h:outputStylesheet library="css" name="jumbotron-narrow.css" />
    <title>Java EE Övning 4 Sam Gholizadeh</title>
</h:head>

<h:body>

    <div class="container">

        <div class="header">
            <ul class="nav nav-pills pull-right">
                <li class="active"><h:link value="Tweet" outcome="index" /></li>
                <li>
                    <c:choose>
                        <c:when test="#{UserManager.user.userid > 0}">
                            <!-- Split button -->
                            <div class="btn-group">
                              <h:link class="btn" value="#{UserManager.user.username}" outcome="settings" />
                              <button type="button" class="btn btn-danger">Action</button>
                              <button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown">
                                <span class="caret"></span>
                                <span class="sr-only">Toggle Dropdown</span>
                              </button>
                              <ul class="dropdown-menu" role="menu">
                                <li><a href="#">Action</a></li>
                                <li><a href="#">Another action</a></li>
                                <li><a href="#">Something else here</a></li>
                                <li class="divider"></li>
                                <li><a href="#">Separated link</a></li>
                              </ul>
                            </div>
                        </c:when>
                        <c:otherwise>
                            <h:link value="Registrera" outcome="register" />
                        </c:otherwise>
                    </c:choose>
                </li>
            </ul>
            <h3>Uppgift 4</h3>
        </div>

        <div class="row marketing">
            <div class="col-lg-6">
                <ui:insert name="left" />
            </div>
            <div class="col-lg-6">
                <ui:insert name="right" />
            </div>
        </div>

        <div class="footer">
            <p>Sam Gholizadeh</p>
        </div>

    </div>

    <h:outputScript name="resources/js/bootstrap.min.js" />

</h:body>

This is the managed bean:

@ManagedBean
@SessionScoped
public class UserManager implements Serializable {
private User user;
private FacesContext context;

@EJB
private UserService UserService;

@PostConstruct
public void init(){
    user = new User();
}

public void Save(){
    UserService.create(user);
}

public void testDispatch() throws IOException{
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.invalidateSession();
    ec.redirect(ec.getRequestContextPath() + "/index.xhtml");
}

public void logout() throws IOException{
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.invalidateSession();
    ec.redirect(ec.getRequestContextPath() + "/index.xhtml");
}
}

The error message I get is:

javax.el.PropertyNotFoundException: /register.xhtml @21,89 action="#{UserManager.testDispatch}": Target Unreachable, identifier 'UserManager' resolved to null

There is no faces-config.xml so the Managed bean name should default to classname?

Elrond_EGLDer
  • 47,430
  • 25
  • 189
  • 180
user2455558
  • 117
  • 1
  • 9

0 Answers0