Вопрос

I am putting in changes to a tool we use, and I'm having trouble getting local javascript files to load in. The jquery library link works perfectly fine:

builder.append("<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>");

But js files stored within the project seem to be returning blank pages.

builder.append("<script src='/SessionInfo/js/session.js'></script>");

The project uses a custom controller that routes requests based on the uri, heres a snippet:

if ( lowerCaseUri.matches("/sessioninfo/v1/userid/?") ) {
     UserIDHandlerV2.handleUserIdPrompt(output);
  } else if ( lowerCaseUri.matches("/sessioninfo/v1/userid/.*") ) {
     UserIDHandlerV2.handleUserIdSessionListDisplay(request, output);
  } else if ( lowerCaseUri.matches("/sessioninfo/js/.*") ) {
      response.setContentType("text/javascript");
  }

Looking at fiddler and chrome tools, it shows 200 responses returning the correct content type, but 0 bytes are transferred and in resources/scripts it shows blank pages.

Это было полезно?

Решение

I would suggest, get rid of the "custom controller that routes requests based on the uri," and use a URL Rewrite filter like Tuckey Urlrewrite Filter

Your problem is obviously that your custom controller doesn't work. For one thing, look at this:

else if ( lowerCaseUri.matches("/sessioninfo/js/.*") ) 
{
   response.setContentType("text/javascript");
}

If there is a request for a javascript file, all you are doing with that request is setting the content type to javascript. You aren't reading the javascript file and serving it.

What you are doing is trying to build your own url-rewriting, and that's not necessary.

You could also just take /sessioninfo/js/.* out of your controller. I'm assuming these are just normal .js files, right? So why are you using a controller to route them? The purpose of a controller is when you need to put backend information on a view, so you send the user to the controller (i.e. a servlet) first and then controller sets request attributes and then forwards to a JSP that simply displays them.

Другие советы

Use HttpServletRequest#getContextPath to append those sources:

builder.append("<script src='" + request.getContextPath() + "/SessionInfo/js/session.js'></script>");
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top