Search code examples
javacssspring-mvcwhite-labelling

Serving different CSS based on user ID - spring mvc


I have a web app developed with spring-mvc. Every company will have an admin who will be able to edit backgrounds, fonts and colors. The app should store the color settings( in the database or in a css file ?) and it should use the preferred CSS for all users belonging to the same company. Different companies will see different CSS.

What is the best approach to achieve that: serving different CSS based on user ID.


Solution

  • You can try to code a CssController with a mapping to a method serving css

    @RequestMapping(value = "/mycustom.css")
        public @ResponseBody
        byte[] getMessages(final HttpSession session,  final Principal principal) {
            StringBuffer sb = new StringBuffer("");
            sb.append("/* some css*/");
            return sb.toString().getBytes(Charset.forName("ISO-8859-1"));
    
    }
    

    and with optionnaly a @Cacheable if you need to access DB to build the css.To not have to fetch the data on every page.

    and include it :

    <link href="/mycustom.css" rel="stylesheet">