0

I'm having a lot of problems with implementing an Ajax call to my Managed Bean. I prepared a simple test case - I expect the outputText value to change to Beta, but it remains at Alpha. What am I doing wrong?

package com.example.controllers;

import javax.faces.bean.ViewScoped;
import javax.inject.Named;

@Named(value = "tester")
@ViewScoped
public class tester{

    private String testString;

    /**
     * Creates a new instance of tester
     */
    public tester() {
        testString = "Alpha";
    }

    public String changeText(){

        testString = "Beta";
        return null;
    }

    /**
     * @return the testString
     */
    public String getTestString() {
        return testString;
    }

    /**
     * @param testString the testString to set
     */
    public void setTestString(String testString) {
        this.testString = testString;
    }
}


<?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"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
            <h:commandButton value="Add Row" action="#{tester.changeText}">
                <f:ajax event="action" execute="@form" render="out"></f:ajax>
            </h:commandButton>
            <h:outputText id="out" value="#{tester.testString}"/>
        </h:form>
    </h:body>
</html>
tacua
  • 194
  • 1
  • 14

1 Answers1

1

Don't mix CDI with managed beans. Change import javax.faces.bean.ViewScoped to to import javax.faces.view.ViewScoped

UPDATE From your comment it seems that you do not have JSF 2.2 (my mistake). Easiest thing for you to do would be the following:

Replace

import javax.inject.Named 

with

import javax.faces.bean.ManagedBean;

and

@Named(value = "tester")

with

@ManagedBean(name = "tester")

One last thing, you do not have to specify a value for name when you do the changes above. When you use @ManagedBean without the name you can refer to your bean in your xhtml pages by using the class name. Keep in mind though that the first letter will be in lower case.

Andy
  • 5,900
  • 2
  • 20
  • 29
  • Netbeans doesn't seem to recognize that import, are you sure it's right? – tacua Jul 01 '13 at 00:11
  • @tacua Makes sense or no ? I can expand if needed. – Andy Jul 01 '13 at 00:17
  • I got it working with changing to Managed Beans, thanks! My teacher says I should work with CDI though, so I need to read up on the differences between the two. – tacua Jul 01 '13 at 00:25
  • @tacua It's simple really. CDI will ignore all annotations from the package `javax.faces.bean.*` It has its own package when it comes to scopes `import javax.enterprise.context.*` There's more to CDI (they are more powerful) but that should suffice for now ;) – Andy Jul 01 '13 at 00:27