Abstract classes and Spring MVC @ModelAttribute/@RequestParam

07-08-2016

You need to provide your own Binding methods and then create the correct subtypes. Spring wouldn't know otherwise which of the subtypes should be instantiated for which element.

Example: Url address can be like this: /proje-basvuru/ilk-uzun-metrajli-film or /proje-basvuru/kisa-film
@ModelAttribute("project")
public Project getProject(final HttpServletRequest request){
    return createProject(getProjeBasvuruUrl(request.getRequestURI()));
}

private Project createProject(String projectType) {
    if (projectType.equals("ilk-uzun-metrajli-film"))
    return new FirstFeatureFilm();
    if (projectType.equals("kisa-film"))
    return new ShortFilm();
    return new Project();
}

private String getProjeBasvuruUrl(String url){
    return RegularExpressionUtil
    .createNewInstance(url)
    .getSingleWord("proje-basvuru/([\\w-]{0,})",1);
}

@RequestMapping(value = {"/kullanici/home/proje-basvuru/{projectType}/save"}, method = RequestMethod.POST)
public ModelAndView saveProject(@Valid @ModelAttribute("project") Project project,
BindingResult bindingResult, @PathVariable("projectType") String projectType) {
    ModelAndView modelAndView = new ModelAndView("kullanici/home/proje-basvuru");
    modelAndView.addObject("projectCategory", projectCategoryService.get("url",projectType));
    initForm(modelAndView, project);
    if (!bindingResult.hasErrors()) {
        projectService.saveOrUpdate(project);
    }
    return modelAndView;
}

When you access the form page or post the form, Spring will run getProject() method and init subtype. After post request Project model attribute will point to related sub type.

RegularExpressionUtil Class

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegularExpressionUtil {
    private String searchText;
    public static RegularExpressionUtil instance;

    private RegularExpressionUtil(String searchText) {
        this.searchText = searchText;
    }

    private RegularExpressionUtil() {
    }

    public static RegularExpressionUtil createInstance() {
        if(instance == null) {
            instance = new RegularExpressionUtil();
        }

        return instance;
    }

    public static RegularExpressionUtil createNewInstance(String searchText) {
        return new RegularExpressionUtil(searchText);
    }

    public boolean isMatch(String regex) {
        Pattern pattern = Pattern.compile(regex);
        this.searchText = this.searchText.replace("\\Q", "").replace("\\E", "");
        Matcher matcher = pattern.matcher(this.searchText);
        return matcher.find();
    }

    public boolean isMatch(String str, String regex) {
        this.searchText = str;
        return this.isMatch(regex);
    }

    public boolean isEscapeMatch(String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(this.searchText);
        return matcher.find();
    }

    public boolean equals(String regex) {
        return this.isMatch("^(" + regex + ")$");
    }

    public List<String> getSubStrings(String regex, int groupId) {
        ArrayList result = new ArrayList();
        Pattern pattern = Pattern.compile(regex);
        this.searchText = this.searchText.replace("\\Q", "").replace("\\E", "");
        Matcher matcher = pattern.matcher(this.searchText);

        while(matcher.find()) {
            result.add(matcher.group(groupId));
        }

        return result;
    }

    public String getSingleWord(String regex, int groupId) {
        List result = this.getSubStrings(regex, groupId);
        return result != null && result.size() > 0?(String)result.get(0):null;
    }

    public void setSearchText(String searchText) {
        this.searchText = searchText;
    }
}

© 2019 All rights reserved. Codesenior.COM