Exemple d’intégration Wicket Spring

Exemple d'intégration Wicket + Spring

Ce didacticiel montre comment utiliserintegrate Wicket with Spring framework.

Bibliothèques dans cet article:

  1. Wicket v1.4.17

  2. portillon-ressort v1.4.17

  3. Spring v3.0.5.RELEASE

1. Structure du projet

Structure finale du répertoire de projet de ce tutoriel, rien de spécial, juste un projet Maven standard.

wicket spring example

2. Dépendance du projet

Obtenez les dépendances Wicket et Spring, pour intégrer les deux, vous avez besoin de «wicket-spring.jar».



    

        
        
            org.apache.wicket
            wicket
            1.4.17
        

        
        
            org.apache.wicket
            wicket-spring
            1.4.17
        

        
        
            org.springframework
            spring-core
            3.0.5.RELEASE
        

        
            org.springframework
            spring-context
            3.0.5.RELEASE
        

        
            org.springframework
            spring-web
            3.0.5.RELEASE
        

        
        
            org.slf4j
            slf4j-log4j12
            1.5.6
        

    

3. Haricot de printemps

Créez un bean Spring, annotez-le avec@Service.

package com.example.user;

public interface HelloService {

    String getHelloWorldMsg();

}
package com.example.user;

import org.springframework.stereotype.Service;

@Service
public class HelloServiceImpl implements HelloService {

    public String getHelloWorldMsg() {
        return "Spring : hello world";
    }

}

4. Injecter dans le récipient Spring

Créez un fichier SpringapplicationContext.xml standard, activez la fonction d'analyse automatique des composants.

Fichier: applicationContext.xml



    

5. Intégrer Wicket avec Spring

Remplacez la méthodeinit() de l'application Wicket avec ce «addComponentInstantiationListener(new SpringComponentInjector(this));».

Fichier: classe d'application Wicket

package com.example;

import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import com.example.user.SimplePage;

public class WicketApplication extends WebApplication {

    @Override
    public Class getHomePage() {

        return SimplePage.class; // return default page
    }

    @Override
    protected void init() {

        super.init();
        addComponentInstantiationListener(new SpringComponentInjector(this));

    }

}

Maintenant, vous pouvez injecter Spring bean dans le composant Wicket via@SpringBean.

package com.example.user;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.spring.injection.annot.SpringBean;

public class SimplePage extends WebPage {

    @SpringBean
    private HelloService helloService;

    public SimplePage(final PageParameters parameters) {

        add(new Label("msg", helloService.getHelloWorldMsg()));

    }

}


    

Wicket + Spring integration example

6. web.xml

Dernière étape, faites savoir à votre projet Web ce qu'est Wicket et Spring. Déclare les deux enweb.xml.

Fichier: web.xml



    Wicket Web Application

    
        wicket.wicketTest
        org.apache.wicket.protocol.http.WicketFilter
        
            applicationClassName
            com.example.WicketApplication
        
    

    
        wicket.wicketTest
        /*
    

    
        
                     org.springframework.web.context.ContextLoaderListener
                
    

7. Demo

Démarrer et visiter -http://localhost:8080/WicketExamples/.

Une simple page Wicket, et le message est renvoyé du printemps.

wicket spring demo

Téléchargez-le -Wicket-Spring-Integration-Example.zip (8 Ko)