Announcement Announcement Module
Collapse
No announcement yet.
Issue with ResourceHelper from spring-faces and requestServletPath != "" Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Issue with ResourceHelper from spring-faces and requestServletPath != ""

    Hello!

    We want to use the URL-pattern "/a/*" for spring's dispatcher servlet.
    Unfortunately the script tags for loading spring webflow's JavaScript
    files have the wrong src attribute and won't be found!

    The implementation of the problematic method is:

    public static void renderScriptLink(FacesContext facesContext, String scriptPath, Map attributes)
    throws IOException {
    if (alreadyRendered(facesContext, scriptPath)) {
    return;
    }
    ResponseWriter writer = facesContext.getResponseWriter();
    writer.startElement(SCRIPT_ELEMENT, null);
    writer.writeAttribute("type", "text/javascript", null);
    Iterator i = attributes.keySet().iterator();
    while (i.hasNext()) {
    String key = (String) i.next();
    writer.writeAttribute(key, attributes.get(key), null);
    }
    String src = facesContext.getExternalContext().getRequestContex tPath() + "/resources" + scriptPath;
    writer.writeAttribute("src", src, null);
    writer.endElement(SCRIPT_ELEMENT);
    markRendered(facesContext, scriptPath);
    }

    IMHO the src attribute isn't composed correctly in case of a non-empty servlet path,
    shouldn't it be like this?

    (...)
    // create path prefix
    final String prefix;
    if (StringUtils.hasText(fc.getExternalContext().getRe questServletPath())) {
    // we have a servlet path, append it to the context path!
    prefix =
    fc.getExternalContext().getRequestContextPath()
    + fc.getExternalContext().getRequestServletPath();
    } else {
    // no servlet path, use context path, only!
    prefix = fc.getExternalContext().getRequestContextPath();
    }
    final String src = prefix + "/resources" + scriptPath;
    (...)

    What do you think?

    Any feedback is appreciated!

    Regards,
    Thomas

  • #2
    Hello

    Use code tags, to make the code more readable

    Unfortunately the script tags for loading spring webflow's JavaScript
    files have the wrong src attribute and won't be found!
    Could you post the complete error stack trace?

    Comment


    • #3
      More details

      Originally posted by dr_pompeii View Post
      Hello

      Use code tags, to make the code more readable
      Could you post the complete error stack trace?
      Thanks for your reply,

      of course I should try to improve the readability , so let's try again :-)!

      You won't get an exception, but the requested resource file won't be found and
      you get a 404 response!

      The problem we have found belongs to the following methods from class org.springframework.faces.ui.resource.ResourceHelp er from spring-faces:

      Code:
        public static void renderScriptLink(FacesContext facesContext, String scriptPath, Map attributes);
        public static void renderStyleLink(FacesContext facesContext, String cssPath);
      In both methods the src atribute of the rendered link element doesn't contain the servlet path!

      Let us assume that the context path for the web application is "/foo" and the dispatcher
      servlet is configured with the url-pattern "/bar/*". The requested resource in our example is
      "/spring/Spring.js".

      Let's take a look at renderScriptLink():

      Code:
      public static void renderScriptLink(FacesContext facesContext, String scriptPath, Map attributes)
      			throws IOException {
      		if (alreadyRendered(facesContext, scriptPath)) {
      			return;
      		}
      		ResponseWriter writer = facesContext.getResponseWriter();
      		writer.startElement(SCRIPT_ELEMENT, null);
      		writer.writeAttribute("type", "text/javascript", null);
      		Iterator i = attributes.keySet().iterator();
      		while (i.hasNext()) {
      			String key = (String) i.next();
      			writer.writeAttribute(key, attributes.get(key), null);
      		}
      		String src = facesContext.getExternalContext().getRequestContextPath() + "/resources" + scriptPath;
      		writer.writeAttribute("src", src, null);
      		writer.endElement(SCRIPT_ELEMENT);
      		markRendered(facesContext, scriptPath);
      	}
      The script element will look like this:
      Code:
      <script type="text/javascript" src="/foo/resources/spring/Spring.js"></script>
      This path doesn't match to the servlet path of the dispatcher servlet, you will get a 404 response!

      If the src attribute is calculated this way:
      Code:
                      final String prefix;
      		if (StringUtils.hasText(facesContext.getExternalContext().getRequestServletPath())) {
      			// we have a servlet path, append it to the context path!
      			prefix = facesContext.getExternalContext().getRequestContextPath()
      					+ facesContext.getExternalContext().getRequestServletPath();
      		} else {
      			// no servlet path, use context path, only!
      			prefix = facesContext.getExternalContext().getRequestContextPath();
      		}
      		final String src = prefix + "/resources" + scriptPath;
      The script element will now look like this:
      Code:
      <script type="text/javascript" src="/foo/bar/resources/spring/Spring.js"></script>
      and the resource will be found!


      Same problem occurs in method
      Code:
        public static void renderStyleLink(FacesContext facesContext, String cssPath);
      and the same solution fixes the problem!

      I hope the description is comprehensible :-)!

      Regards,
      Thomas

      Comment

      Working...
      X