2

I am migrating JSF1.2 application to JSF 2.1. It has a Login page, which uses facelets template. And the template page has h:head tag. Login page has a tag h:selectBooleanCheckbox inside ui:composition/ui:define/h:form/a4j:outputPanel/h:panelGrid/h:panelGroup tags.

<h:selectBooleanCheckbox value="#{bean.alogin}" >
  <a4j:ajax event="click" execute="@form" render="loginPanel" />    
</h:selectBooleanCheckbox>

On click of check box, I am getting 'ReferenceError: RichFaces is not defined' in Browser Error Console.

The issue is same even with using f:ajax tag, and having

<h:outputScript name="jsf.js" library="javax.faces" target="head"/>

in template page/login page.

This issue (of not finding/loading js libs) is coming only on the First load of Login page. That means if I login to my application and logout and then use the above check box, there is no issue.

Please direct with any pointers and that will be very helpful. Thanks very much in advance.

And following is the html generated in head tag:

<script src="/myapp/javax.faces.resource/jsf.js.faces?ln=javax.faces" type="text/javascript">&lt;!--
//--&gt;</script>

I have the two *.faces mappings in web.xml. One is CustomFilter (implementing javax.servlet.Filter) and the other is CustomServlet (extending org.apache.myfaces.webapp.MyFacesServlet).

Kumar
  • 27
  • 1
  • 8
  • show your `xmlns` include of the page... – Daniel Aug 26 '12 at 13:48
  • @Kumar when people ask you to show your code (Java, HTML, SQL or whatever), you must edit your question and post it there. It's very difficult to read the code when is posted in comments. – Luiggi Mendoza Aug 26 '12 at 15:22
  • does adding `xmlns:rich="http://richfaces.org/rich" xmlns:a4j="http://richfaces.org/a4j"` to your template page helps? – Daniel Aug 26 '12 at 18:35
  • mmmm... what is your servlet mapping within the web.xml ? take a look at this https://www.google.com/search?q=ReferenceError%3A+RichFaces+%2F+jsf+is+not+defined%E2%80%9D+error&rlz=1C1CHEU_enIL465IL465&sugexp=chrome,mod=19&sourceid=chrome&ie=UTF-8 – Daniel Aug 27 '12 at 12:15
  • change -1 into 1 in `-1` , also how do you access your page ? hostname/faces/page.xhtml? or how? – Daniel Aug 27 '12 at 12:31
  • 1
    last attempt : instead of your forwarding try adding to your web.xml ` ui/login.faces ` + create empty file with the name `login.faces` inside the `ui` folder – Daniel Aug 27 '12 at 13:40
  • Daniel: Thanks v much again n again for each word of your reply. I really appreciate your keen and kind help. I have done as suggested by you. The application is working perfectly as before, but the 'jsf is not defined' issue remained. One issue I have observed while doing the above is that I am getting another error in browser console. I have pasted it in my question. Please review and advice if that is causing issue and any possible solutions. Thanks very much. – Kumar Aug 27 '12 at 14:06
  • please take a look at this thread , make sure you did everything too http://stackoverflow.com/a/4532870/617373 – Daniel Aug 27 '12 at 15:09
  • You are welcome , b.t.w about the redirecting way you are using with the jsp , the hack i told you in in one of the comments is described here... – Daniel Aug 27 '12 at 16:05
  • @Kumar oops forgot to paste http://stackoverflow.com/a/7015676/617373 – Daniel Aug 27 '12 at 18:21

1 Answers1

2

ReferenceError: RichFaces / jsf is not defined

This is a JavaScript error. This means that neither var RichFaces = ...; nor var jsf = ...; is anywhere been defined in JavaScript context. This in turn means that auto-including the JSF and RichFaces-provided JavaScript files jsf.js and richfaces.js has failed. This can in turn be caused by not having used the <h:head> component to declare the HTML head which is a mandatory hook for JSF to auto-include JavaScript files.

Apparently you forgot to replace the

<head>
    ...
</head>

in your templates by

<h:head>
    ...
</h:head>

Fix it accordingly.

Using <h:outputScript name="jsf.js" library="javax.faces" target="head"/> only fixes the JSF part of the error, not the RichFaces part and is actually a workaround, not a solution.


Update: as per your question update, you confirmed that you've mapped sort of a login Filter on the exact URL pattern of those resources (at least, the filter name "session filter" in combination with the problem symptoms described so far indicates that you're blocking requests whereby the user is not logged-in). JSF resources are served by *.faces URL pattern through the FacesServlet as well and thus they are checked by that filter as well (if you have studied the generated <script src> URL closely, you'd have realized that).

You likely need to alter your Filter to skip the logged-in check on those resources so that those resources won't be redirected to the login page as well. You could do that by checking if the request URI does not concern the ResourceHandeler.RESOURCE_IDENTIFIER URLs.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;

    if (req.getRequestURI().startsWith(req.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) {
        chain.doFilter(request, response); // Skip JSF resources (CSS/JS/Images/etc)
        return;
    }

    // ... Continue your login check here.
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • ThanksVeryMuch, its privilege to hear from you. I am sorry if I could not present my Q correctly, please allow me to try again. I have two pages, login and home. After login, user is taken to home page. Both share one single common template. And the template has h:head tag with inside h:outputScript tag (with name="jsf.js", library="javax.faces" and target="head"). **The issue is jsf.js is not loading correctly (pls refer my Q above) in login page** but it is loading correctly in home page. After logout, user is directed back to login page. Now it is loading jsf.js correctly. Please advice. – Kumar Aug 27 '12 at 19:15
  • Are you using JSP or Facelets? JSP has been deprecated since JSF 2.0 and all new JSF 2.0 tags/features won't work at all in JSP. – BalusC Aug 27 '12 at 19:23
  • 1
    Do you have mapped any filters on `/*` or `*.faces` or whatever matches your JS files as well? – BalusC Aug 27 '12 at 19:49
  • OMG, I just do not believe this. Your answer works perfectly. Thanks very very much. And my Salute/Namaskar to GeniusGuru. – Kumar Aug 27 '12 at 20:54
  • You're welcome :) Note that this way you don't need that `` anymore. Don't forget to remove that for cleanup, otherwise you keep asking yourself "What the heck is that line doing there?" when you (or someone else) review the code a long time later :) – BalusC Aug 27 '12 at 20:54
  • I have one query that, my application used to work in JSf1.2 without change in filter. Is this change specific to JSF2.1 migration, please help. Thanks v much. – Kumar Aug 27 '12 at 21:31
  • 1
    Yes, that's correct. Since JSF 2.0, it's possible to auto-include CSS/JS resources by `@ResourceDependency` and to declare resources for inclusion in `` by `` and `` anywhere else in the JSF file. This all is part of new resource management mechanism in JSF 2.0. All resource requests are hereby processed by the `FacesServlet` (and should thus have an URL matching the `FacesServlet`'s URL-pattern). Before in JSF 1.x, this all doesn't exist. All resources were the usual way processed by the servletcontainer's default servlet. – BalusC Aug 27 '12 at 21:34
  • Cant stop thanking. Thanks v much again for clarifying my doubt very clearly and for pointers. I will be explore more about resource management in jsf2. – Kumar Aug 27 '12 at 21:38