How spraying finds resources - e.g. Javascript

It was easy to create my first spray servlet.

But the sources referenced in the header were never found.

<head> ... <script src = "javascript / jquery / jquery-1.9.1.js" / "> ... </head>

In which directory do you need to put these recursions, or how can you spray to look there?

A simple question, but I could not understand.

Many thanks

Girgl

+6
source share
2 answers

Using Spray routing, I use these directives -

  pathPrefix ("css") {get {getFromResourceDirectory ("css")}} ~
 pathPrefix ("js") {get {getFromResourceDirectory ("js")}} ~ 

"css" and "js" must be in the src/main/resources directory

+10
source

If you use spray routing then this should be easy, just specify a route for your static resources. For example, you can do the following:

Say your static resources are in the /css , /js and /img folders:

 def staticPrefixes = List("css", "js", "img") map { pathPrefix(_) } reduce { _ | _ } 

with pathPrefix you make each path the prefix of an unsurpassed path. Then you need a directive to extract the path to the static file from the request, for example, you can do it like this:

 def stripLeadingSlash(path: String) = if (path startsWith "/") path.tail else path val staticPath = staticPrefixes & cache(routeCache()) & extract(ctx β‡’ stripLeadingSlash(ctx.request.uri.path.toString)) 

then create your route that will manage your resources:

 val staticRoutes = get { staticPath { path β‡’ getFromResource(path.toString) } } 
0
source

Source: https://habr.com/ru/post/958170/


All Articles