Just read the article - Status of wicket-cdi Module at
https://www.42lines.net/2012/09/11/status-of-wicket-cdi-module/.

Wonder if wicket-cdi is part of Wicket 6.1 release and upwards. I tried a
simple Wicket 6.3 application running in TomEE, but could not get the CDI
injection to work. These are the steps I used:

1. Create a Wicket project with Maven (according to
http://wicket.apache.org/start/quickstart.html)

mvn archetype:generate -DarchetypeGroupId=org.apache.wicket
-DarchetypeArtifactId=wicket-archetype-quickstart -DarchetypeVersion=6.3.0
-DgroupId=com.mycompany -DartifactId=myproject -DarchetypeRepository=
https://repository.apache.org/ -DinteractiveMode=false

2. Generate the eclipse configuration files
mvn eclipse:eclipse -Dwtpversion=2.0

(Note: Following steps are based on the tutorial on Youtube:
http://www.youtube.com/watch?v=Lr8pxEACVRI)
3. In folder src/main/webapp/WEB-INF, create file beans.xml
(Also in folder src/main/webapp/META-INF, create file beans.xml, according
to the explanation for this example:
http://tomee.apache.org/examples-trunk/cdi-basic/README.html, and also
src/main/resources/META-INF/beans.xml just in case)

4. Create a POJO
public class Faculty {
    private List<String> facultyMembers;
    private String facultyName;

    @PostConstruct
    public void initialize() {
        this.facultyMembers = new ArrayList<String>();
        facultyMembers.add("Ian Schultz");
        facultyMembers.add("Diane Reyes");
        facultyName = "Computer Science";
    }

    public List<String> getFacultyMembers() {
        return facultyMembers;
    }

    public String getFacultyName() {
        return facultyName;
    }
}

5. Inject the POJO in HomePage.java and display it.
public class HomePage extends WebPage {
        private static final long serialVersionUID = 1L;

        @Inject
        private Faculty faculty;

        public HomePage(final PageParameters parameters) {
                super(parameters);

                if (faculty != null) {
                        add(new Label("cdi", faculty.getFacultyName()));
                } else {
                        add(new Label("cdi", "Not working!"));
                }
    }
}

Note, class HomePage is the class for the Wicket homepage, as specified
below:
public class WicketApplication extends WebApplication
{
        /**
         * @see org.apache.wicket.Application#getHomePage()
         */
        @Override
        public Class<? extends WebPage> getHomePage()
        {
                return HomePage.class;
        }
}

These are the steps I tried, but when running, "Not working!" is displayed
on the home page. I debugged it and confirmed that faculty is null, which
means not injected.

Many thanks for your kind help!

Yuci

Reply via email to