1

In my UI code, i have a link to a css stylesheet with href="url for spring controller".

I want the Spring Controller to return the CSS file which the UI page uses for styling.

First i was wondering, if this was even possible?, and secondly what the Spring Controller needs to return? Do i need to return a byte[] representation of the css file and put it in a ResponseEntity, or use some kind of outputstream on the Servlet response?

Something like?

@RequestMapping(value = "/getCSS/{userId}", method= RequestMethod.GET, produces={"text/css"})
@ResponseStatus(HttpStatus.OK)
public ??? getCSS(){
}

The UI code and Spring app which has the controller are not part of the same project.

Different users have different stylings, and the Spring app, gets the users css file from a database. Therefore, the css file cannot simply be put into the /static folder or /resources folder as there will be different css files for different users.

rurounisuikoden
  • 269
  • 1
  • 4
  • 16
  • Possible duplicate of [How to include js and CSS in JSP with spring MVC](http://stackoverflow.com/questions/26276603/how-to-include-js-and-css-in-jsp-with-spring-mvc) – Aziz Feb 17 '16 at 11:12

3 Answers3

0

You can put static resources (if they don't have to be secured) in your webapp folder. For example webapp/static/style.css

Your can get this file with : localhost:8080/applicationName/static/style.css

bilal bilal
  • 124
  • 7
0

controler

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class WebController {

   @RequestMapping(value = "/staticPage", method = RequestMethod.GET)
   public String redirect() {

      return "redirect:/static/final.css";
   }
}

Webservlet.xml

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/static/" />
    <property name="suffix" value=".css" />
    </bean>

    <mvc:resources mapping="/static/**" location="/WEB-INF/static/" />
    <mvc:annotation-driven/>

Also you can return page like html,jsp

P S M
  • 1,121
  • 12
  • 29
  • Here tag is being used to map static pages. The mapping attribute must be an Ant pattern that specifies the URL pattern of an http requests. The location attribute must specify one or more valid resource directory locations having static pages including images, stylesheets, JavaScript, and other static content. Multiple resource locations may be specified using a comma-seperated list of values. – P S M Feb 17 '16 at 11:25
0

if the resources are in different project you can store the name of that project in applicationContext and refer to it like this from your view ${applicationScope.resourcesProject} like this

<link href="/${applicationScope.resourcesProject}/resources/css/style.css" rel="stylesheet" type="text/css"/>
achabahe
  • 2,445
  • 1
  • 12
  • 21