Exemple de contrôleur abstrait MVC de Spring

Exemple de contrôleur abstrait Spring MVC

À titre de référence, cet article vous montre comment créer une classeAbstract pour Spring Controller ou un modèle de conception de méthode de modèle.

1. Contrôleur abstrait

Dans la classe Abstract, l'annotation@Controller est facultative, votre classe implémentée l'appliquera.

AbstractResultController.java

package com.example.web.controller;

import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.example.web.generator.bo.ResultGenerator;

public abstract class AbstractResultController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    abstract ResultGenerator GetResultGenerator();
    abstract String GetViewName();
    abstract Validator GetValidator();

    @RequestMapping(value = "/{q:.+}", method = RequestMethod.GET)
    final ModelAndView getModelAndView(@PathVariable("q") String q) {

        logger.debug("getModelAndView : [q] : {}", q);

        ModelAndView model = new ModelAndView();
        Map results = GetResultGenerator().getResultForController(q);
        model.addObject("data", results);
        model.setViewName(GetViewName());

        logger.debug("getModelAndView : [model] : {}", model);

        return model;

    }

}

2. Les contrôleurs de printemps étendent le résumé

Peu de contrôleurs Spring s'étendent au-dessus de la classe Abstract et de leur chemin URI correspondant.

2.1 Path = /hosting/{q:.+}

ResultControllerHosting.java

package com.example.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.example.web.generator.bo.ResultGenerator;

@Controller
@RequestMapping("/hosting")
public class ResultControllerHosting extends AbstractResultController {

    private static final String VIEW_NAME = "hosting";

    @Autowired
    @Qualifier("resultGeneratorHosting")
    ResultGenerator resultGenerator;

    @Override
    ResultGenerator GetResultGenerator() {
        return resultGenerator;
    }

    @Override
    String GetViewName() {
        return VIEW_NAME;
    }

}

2.2 Path = /site/{q:.+}

ResultControllerSite.java

package com.example.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.example.web.generator.bo.ResultGenerator;

@Controller
@RequestMapping("/site")
public class ResultControllerSite extends AbstractResultController {

    private static final String VIEW_NAME = "site";

    @Autowired
    @Qualifier("resultGeneratorSite")
    ResultGenerator resultGenerator;

    @Override
    ResultGenerator GetResultGenerator() {
        return resultGenerator;
    }

    @Override
    String GetViewName() {
        return VIEW_NAME;
    }

}

Terminé.