Kunal Batra
Kunal Batra

Reputation: 991

How to set variable name of controller class so that all modelview can access that variable value?

I am using Spring-Mvc in my wab-site where user can change language my project is working fine i just need improvment in my coding i dont have too much knowledge of spring -mvc

Source code of my controller class is given below

@Controller
public class ContentController {

    @RequestMapping("/index")
    public ModelAndView sriWap(){

        Locale locale2 = LocaleContextHolder.getLocale();
        String lang_name = locale2.getDisplayLanguage();

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("language",lang_name);
        modelAndView.setViewName("sriWap");
        return modelAndView;
    }

    @RequestMapping("/contentCat")
    public ModelAndView content_cat_desc(){
    Locale locale2 = LocaleContextHolder.getLocale();
        String lang_name = locale2.getDisplayLanguage();

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("language",lang_name);
        modelAndView.setViewName("content_cat_desc");
        return modelAndView;
    }
    @RequestMapping("/content")
    public ModelAndView content(){
    Locale locale2 = LocaleContextHolder.getLocale();
        String lang_name = locale2.getDisplayLanguage();

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("language",lang_name);
        modelAndView.setViewName("content");
        return modelAndView;
    }

}

As you can see i am writing the same code in all Methods to get the selected language

Locale locale2 = LocaleContextHolder.getLocale();
String lang_name = locale2.getDisplayLanguage();

I dont want to write above code in all methods i want to write above code only for one time so that all methods can use the value of 'lang_name'

Upvotes: 0

Views: 722

Answers (3)

Ken Bekov
Ken Bekov

Reputation: 14025

You can use @ModelAttribute annotation.

@Controller
public class ContentController {

    @ModelAttribute
    public void addCommon(Model model){
        Locale locale2 = LocaleContextHolder.getLocale();
        String lang_name = locale2.getDisplayLanguage(); 
        model.addAttribute("language",lang_name);       

        //... add another common attributes
    }

    @RequestMapping("/index")
    public ModelAndView sriWap(){
        return new ModelAndView("sriWap");
    }

    ....
}

Spring will add "language" attribute to the model for every mapped method automatically.

Upvotes: 1

ChrisGW
ChrisGW

Reputation: 21

Your right, where is a easy way in spring, which prevents this code duplication across your controller request methods. Your looking for @ModelAttribute about a method: http://docs.spring.io/autorepo/docs/spring/current/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-methods

So you have to extraxt this code duplication into a single method, which is annontated with @ModelAttribute like this:

@ModelAttribute("language")
public String getLocaleLanguage() {
    Locale locale = LocaleContextHolder.getLocale();
    return locale.getDisplayLanguage();
}

When this "language" @ModelAttribute also is needed across other @Controllers you want to extract it into a @ControllerAdvice

Upvotes: 2

java_maniac
java_maniac

Reputation: 101

You can have the properties as instance variables and make use of init method to initialise its values.

 @PostConstruct
 public void init() {
 locale2 = LocaleContextHolder.getLocale();
 lang_name = locale2.getDisplayLanguage();     
 }

Upvotes: 0

Related Questions