3 Replies Latest reply: Aug 6, 2012 6:18 AM by newbieRyan RSS

    javax.inject.named causes the no injection

    newbieRyan
      I made a small app that only has a jsf controller with @javax.inject.Named("qb") annotation.
      At the run time, all EL starting with qb referenced to null.
      I have to fall back to use @javax.faces.bean.ManagedBean(name="qb") annotation.
      is it a bug? or did I made a logical error
      <?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:f="http://java.sun.com/jsf/core"
             xmlns:h="http://java.sun.com/jsf/html">
           <h:head>
                <title>#{msgs.title}</title>
           </h:head>
           <h:body>
                <h:form>
                     <h3>#{msgs.heading}</h3>
                     <p>
                          <h:outputFormat value="#{msgs.currentScore}">
                               <f:param value="#{qb.score}"/>
                          </h:outputFormat>
                     </p>
                     <p>#{msgs.guessNext}</p>
                     <p>#{qb.current.sequence}</p>
                     <p>
                          #{msgs.answer}
                          <h:inputText value="#{qb.answer}"/>                    
                     </p>
                     <p><h:commandButton value="#{msgs.next}"/></p>
                </h:form>
           </h:body>
      </html>
      
      ===================================================================
      
      package com.corejsf;
      
      import java.io.Serializable;
      import java.util.ArrayList;
      import javax.faces.bean.ManagedBean;
      import javax.enterprise.context.SessionScoped;
      
      @ManagedBean(name="qb")
      @SessionScoped
      public class QuizBean implements Serializable{
      
           private static final long serialVersionUID = 1L;
           private ArrayList<ProblemBean> problems = new ArrayList<ProblemBean>();
           private int currentIndex;
           private int score;
           
           public QuizBean() {
                problems.add(new ProblemBean(new int[] {3, 1, 4, 1, 5}, 9)); //pi
                problems.add(new ProblemBean(new int[] {1, 1, 2, 3, 5}, 8)); //fibonacci
                problems.add(new ProblemBean(new int[] {1, 4, 9, 16, 25}, 36)); //squares
                problems.add(new ProblemBean(new int[] {2, 3, 5, 7, 11}, 13)); //primes
                problems.add(new ProblemBean(new int[] {1, 2, 4, 8, 16}, 32)); //powers of 2
           }
           
           public void setProblems(ArrayList<ProblemBean> newValue) {
                problems = newValue;
                currentIndex = 0;
                score = 0;
           }
           
           public int getScore() {
                return score;
           }
           
           public ProblemBean getCurrent() {
                return problems.get(currentIndex);
           }
           
           public String getAnswer() { return ""; }
           
           public void setAnswer(String newValue) {
                
                try {
                     int anwser =  Integer.parseInt(newValue.trim());
                     if ( getCurrent().getSolution() == anwser ) score++;
                     currentIndex = (currentIndex+1) % problems.size();
                }
                catch (NumberFormatException e) {}
           }
      }