tags:

views:

26

answers:

1

I was externalizing in a file all my Javascript code inside and application, and to resolve all the EL variables inside the code I tried to use BalusC solution 2 proposed as an answer to this SO question.

I am trying to use the Faces Servlet to handle JS files when requested. The problem comes that when I set up so, the JS file is not found, and I can't guess where the problem is. This is my web.xml

<mime-mapping>
<extension>js</extension>
<mime-type>application/x-javascript</mime-type>
</mime-mapping>
 <servlet>
  <servlet-name>Faces Servlet</servlet-name>
  <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>*.xhtml</url-pattern>
 </servlet-mapping>
  <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>/js/myapp-functions.js</url-pattern>
 </servlet-mapping>

If I try to get the file through http://localhost:8080/myappname/js/myapp-functions.js I get a 404 error. The file is there (double checked). If I remove the mapping for the JS file, I can get the file normally (but as stated, the EL variables are not resolved).

Any suggestions? Thanks in advance.

+1  A: 

The actual file extension should match javax.faces.DEFAULT_SUFFIX which defaults to *.xhtml. Note that you need to wrap the entire page inside a <script></script>, else Facelets' SAX parser will complain about a non-wellformed document.

BalusC
Then I guess that I should include the page with `<ui:include>` instead of `<script>` tag. Besides, the fact that is an .xhtml file makes that it includes the headers for the libraries (richfaces, etc..).Isn't this solution a bit hacky? (Just for the record, the global variables solution is worse IMHO).
pakore
I see what you mean. This is indeed hacky. I didn't realize that when writing the answer on the linked question. The answer was based on my experience with "plain" JSP, but Facelets is a bit more restrictive and the coding ends up to be more clumsy.
BalusC
I guess I could write a sevlet that parses the JS replacing all the EL expressions looking them up in `FacesContext` and returns a JS `mime-type` file. With some caching mechanism should work fine. What do you think?
pakore
Sounds a nice challenge. I by the way improved the answer in the linked question. It was after all unnecessary to map it on `FacesServlet`. Sorry about that flaw, I usually try to post technically correct answers :)
BalusC
I know, don't worry. I posted this question knowing that you would answer it :). I appreciate your effort when posting solutions.
pakore