Spring MVC Abstract Controller example
By:Roy.LiuLast updated:2019-08-17
For self-reference, this article shows you how to create a Abstract class for Spring Controller, or a template method design pattern.
1. Abstract Controller
In Abstract class, the @Controller annotation is optional, your implemented class will apply it.
AbstractResultController.java
package com.mkyong.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.mkyong.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<String, Object> results = GetResultGenerator().getResultForController(q); model.addObject("data", results); model.setViewName(GetViewName()); logger.debug("getModelAndView : [model] : {}", model); return model;
2. Spring Controllers Extend Abstract
Few Spring controllers extend above Abstract class and their matched URI path.
2.1 Path = /hosting/{q:.+}
ResultControllerHosting.java
package com.mkyong.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.mkyong.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.mkyong.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.mkyong.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;
Done.
References
From:一号门
Previous:How to change Eclipse theme
Next:logback.xml Example
COMMENTS