Where to store images, CSS and javascript files on glassfish?

泄露秘密 提交于 2019-12-10 20:17:47

问题


I've looked around a lot (trust me :) before posting this questions and I'm still confused. Here's the scenario:

Hosting a J2EE web-app on glassfish v3 using Restlet 2.0 The freemarker template files are located in the /WEB-INF folder. The templates link to jQuery/javascript files, some static images and stylesheets using relative URLs.

Since it's not a good practice to store them in /WEB-INF (as stated at many places on the web) I moved the files to the WebContent folder


WebContent:
+--Images
   -.jpg, .gif files
+--Styles
   -myStyle.css
+--Scripts
   +--jQLabel
   +--mColorPicker
      +--Images
      +--javascripts
         -.js files

I refer to them in the freemarker template/html as:

<link rel="stylesheet" type="text/css" href="${baseRef}/Styles/myStyle.css"  />

where ${baseRef} is the Root Reference of the site. The site is deployed at /Winbook. So the URL on the localhost for the css looks like this:

http://localhost:8080/Winbook/Styles/winbookwall.css <-- retrieved using a GET

Problem(s):

For each of the above resources I get a 404 :( I'm not sure if this is really how one should store css, images or scripts and whether the WebContent folder is a good place for it be stored.

Question(s):

  1. Why is the above not working? I mean there has to be a mistake and I just don't know about it :)
  2. What is the best place to store the above files on the web server/glassfish?
  3. How do I refer to those resources (in the HTML/freemarker template) if they are deployed in the .war? (They will get deployed in the war file if they are placed in WebContent, right? Is this 'legal' or good practice? Seems to smell :)
  4. Do we have to create alternate doc roots in glassfish for all such resources (or the equivalent Directory class in Restlet?)
  5. What is the 'best' way/place of/for such a deployment for an intranet based application to seamlessly locate the resources? JNDI based lookup? (I don't know how that would work, another question probably on SO ;)

I am totally confused! It's been a while since I had to write a full end to end web app and this was usually taken care of by the 'others' :)

Thanks!


回答1:


I'm probably late on posting an answer to this but here it is. As I mentioned it Restlet intercepts the URLs and you HAVE to use the Directory class to return static content - initialized with the path "war:///" or "war:///images/" etc., for each of the folders.

The reason it wasn't working was with the Routing issues of Restlet - The directory folder was 'last' in the code order of URLs. Directories are created with Template.MODE_STARTS_WITH and the others (i.e., Restlets or other resources) are Template.MODE_EQUALS

You should either change the order of routing manually or push it at the top of the list of URIs when routing.

Hope that helps anyone stumbling on the same problem.

Here is the related question that Jerome answered: Restlet Routing Nightmare




回答2:


Try "resource" JARs - see here : http://alexismp.wordpress.com/2010/04/28/web-inflib-jarmeta-infresources/



来源:https://stackoverflow.com/questions/5639791/where-to-store-images-css-and-javascript-files-on-glassfish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!