0

I Have a Spring Boot Server, In my Database, I have some URLs without any special pattern, I want RequestMapping all of these stored URLs into the Controller and return a special view(control by template engine) for this.

I tried to do this by adding the interceptors method to my Project but I don't have any idea about returning a special view.

I read these questions:

but I don't have any special pattern in the URLs of my project and there are other URLs and controllers in my project.

What can I do?

Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35

1 Answers1

0

After some search, I found the solution for defining a URL and template name in the database and dynamic URL mapping without any Controller and methods.

I have the database's table by Name Sample:

Sample:

field Detail
url address url of controller
template_view_name address of template(html)

I overriding the method addViewControllers of WebApiConfigurer to solve my problem:

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    List<Pair<String, String>> pathTemplate = getPathWithTemplate();

    for (Pair<String, String> pt : pathTemplate) {
        registry.addViewController(pt.getKey()).setViewName(pt.getValue());
    }
}

complete code of WebApiConfigurer:

@EnableWebMvc
@Configuration
@ComponentScan
public class WebApiConfigurer implements org.springframework.web.servlet.config.annotation.WebMvcConfigurer {

    @Autowired
    private CCFormCrudModelInitializer ccFormCrudModelInitializer;

    @Autowired
    private SampleRepository sampleRepository;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //if need to add interceptors
        String[] path = getPath();

        registry.addInterceptor(new SampleInterceptor()).addPathPatterns( path );

    }


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        List<Pair<String, String>> pathTemplate = getPathWithTemplate();

        for (Pair<String, String> pt : pathTemplate) {
            registry.addViewController(pt.getKey()).setViewName(pt.getValue());
        }
    }


    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/",
            "classpath:/resources/",
            "classpath:/static/",
            "classpath:/public/"
    };

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS)
                .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS));
    }


    private List<Pair<String, String>> getPathWithTemplate(){
        return sampleRepository.findAll().stream().map(m -> new Pair<>(m.getUrl(), m.getTemplateName())).collect(Collectors.toList());
    }

    private String[] getPath(){
        return sampleRepository.findAll().stream().map(m -> m.getUrl()).toArray(String[]::new);
    }

}
Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35