Question

I am new to JSF, Java EE and am having abit of a problem with a small test case I am trying out with Faces.xml, JSF and ManagedBeans..

I have a managedBean called beanManager.java and in it, I have two fields(name and testname) and a method called testcase1() that just returns a string. Now, in the frontpage.xhtml, I have an input text box that gets a name from a user and once the user clicks the submit button, the testcase1() method is called and all it does is just set the testname field of the BeanManager.java class with the user's input but for some reasons-obviously why I am sending this message- when the user enter's the name and hits the search button the page navigates to the displaypage.xhtml and the page displays nothing. It is supposed to show the name entered by the user but it is blank. I was advised to use @SessionScoped instead of @RequestScoped but the problem now is that when I deploy my Java EE application on glassfish, i get the following error:

Exception Occurred :Error occurred during deployment: Exception while loading the app : 
java.lang.IllegalStateException: ContainerBase.addChild: start: 
org.apache.catalina.LifecycleException: java.lang.IllegalArgumentException: 
javax.servlet.ServletException: com.sun.enterprise.container.common.spi.util.InjectionException: 
Error creating managed object for class: class org.jboss.weld.servlet.WeldListener

Below are my files..

BeanManager.java

    import javax.enterprise.context.SessionScoped;
    import javax.faces.bean.ManagedBean;


    @ManagedBean(name = "user")
    @SessionScoped

public class BeanManager {
    private String name = "";
    private String testname = "";


    public String getName() {
        return name;
    }

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


    public String testcase1(){
        setTestname(this.name);
        return "test1";
    }


    public String getTestname() {
        return testname;
    }

    public void setTestname(String testname) {
        this.testname = testname;
    }

}

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

<ui:composition template="WEB-INF/templates/origin.xhtml">
    <ui:define name="content">
        <f:view>
            <h:form>
                <h:panelGrid>
                    <h:inputText value="#{user.name}" required = "true"/>
                </h:panelGrid>
                <h:commandButton

                    action="#{user.testcase1()}"
                    value="Search"></h:commandButton>
            </h:form>
            </f:view>


    </ui:define>
</ui:composition>
</html>

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

<ui:composition template="/WEB-INF/templates/origin.xhtml">
    <ui:define name="content">
       <h:panelGrid>
        <h:outputText value="#{user.testname}"/>
        <h:commandButton id="back" value="GoBack" action="frontpage"/>
       </h:panelGrid>
    </ui:define>
</ui:composition>
</html>

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

    <navigation-rule>
        <from-view-id>/frontpage.xhtml</from-view-id>
        <navigation-case>
            <from-action>#{user.testcase1()}</from-action>
            <from-outcome>test1</from-outcome>
            <to-view-id>/displaypage.xhtml</to-view-id>
            <redirect/>
        </navigation-case>
         </navigation-rule>
    <navigation-rule>
    <from-view-id>/displaypage.xhtml</from-view-id>
            <navigation-case>
                <from-outcome>GoBack</from-outcome>
                <to-view-id>/frontpage.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>
</faces-config>
Was it helpful?

Solution

You can't use the CDI annotation javax.enterprise.context.SessionScoped with the JSF javax.faces.bean.ManagedBean. You either go pure JSF with

  • javax.faces.bean.ManagedBean(naming) and javax.faces.bean.SessionScoped (scoping)

    OR Pure CDI

  • javax.inject.Named (naming) and javax.enterprise.context.SessionScoped(scoping)

Related:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top