8

I am aware of this post and I double checked all the possibilities there.

I'm using JSF 2.0 with Mojarra implementation on Glassfish 3.

I'm trying to use two simple <h:commandLink> tags to change the application language. This is the .xhtml page:

<!DOCTYPE html>
<html lang="en"  xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html">
<h:head>
    <title>
        <h:outputText value = "#{appMessage['page.welcome']}" />
    </title>

    <f:metadata>
        <f:event type = "preRenderView" listener = "#{sessionController.changeLanguage}" />
    </f:metadata>
</h:head>

<h:body>
    <h1><h:outputText value = "#{appMessage['text.slide.welcome']}" /></h1>

    <h:form id = "fm-language">
        <h:commandLink action = "#{sessionController.changeLanguage('en')}" value = "#{appMessage['link.english']}" />
        <h:commandLink action = "#{sessionController.changeLanguage('de')}" value = "#{appMessage['link.german']}" />
    </h:form>

</h:body>

This is the HTML code:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>The Maze Project</title>
    </head>
    <body>
        <h1>Welcome</h1>
        <form id="fm-language" name="fm-language" method="post" action="/maze/welcome.xhtml" enctype="application/x-www-form-urlencoded">
            <input type="hidden" name="fm-language" value="fm-language" />
            <script type="text/javascript" src="/maze/javax.faces.resource/jsf.js.xhtml?ln=javax.faces">
            </script>
            <a href="#" onclick="mojarra.jsfcljs(document.getElementById('fm-language'),{'fm-language:j_idt13':'fm-language:j_idt13'},'');return false">English</a>
            <a href="#" onclick="mojarra.jsfcljs(document.getElementById('fm-language'),{'fm-language:j_idt15':'fm-language:j_idt15'},'');return false">Deutsch</a>            
            <input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="8038443616162706480:-1387069664590476821" autocomplete="off" />
        </form>
</body>

When pressing a commandLink nothing at all happens. There is no request sended to the server and this following Java Script error is thrown:

mojarra is not defined

The bean methods are correctly called and work fine in the rest of the appliction.

Community
  • 1
  • 1
Ionut
  • 2,788
  • 6
  • 29
  • 46
  • May I assume that you've tested this in the most minimal view template? I.e. there's absolutely nothing else outside the `` inside the very same ``? If not, please post a fullworthy SSCCE. – BalusC Feb 12 '12 at 13:13
  • Looks okay at first glance. How is the `#{languageController}` bean annotated/registered? – BalusC Feb 12 '12 at 14:07
  • @BalusC languageController is a ManagedBean in the Session Scoped. Also updated the question to proper how this detail. I find it very weird that there is no action at all is happening. – Ionut Feb 12 '12 at 14:14
  • @BalusC Furthermore, I should get something like `action="#{nonexistent}": Identity 'nonexistent' was null and was unable to invoke` if the method specified in the EL doesn't exist. Weirdly, this two links doesn't throw any kind of error if tested with a non existent action. – Ionut Feb 12 '12 at 14:23
  • What package did you import the annotations from? – BalusC Feb 12 '12 at 17:53
  • @BalusC `import javax.faces.bean.ManagedBean;` `import javax.faces.bean.SessionScoped;` `import javax.faces.context.FacesContext;` – Ionut Feb 12 '12 at 18:04
  • @BalusC Is there any other way of debugging besides Phase Listeners and Firebug? The link doesn't send any request and the action attribute is completely ignored (it doesn't matter if the specified method is correct or not). In other cases, for the same application, the `commandLink` works just fine – Ionut Feb 12 '12 at 18:08
  • Oh, the link doesn't send any request at all? I understood that it *did* send a request of which you've shown the phaselistener debug results... Then the problem is in the HTML side already. What's the generated HTML output of the link? There has got to be a JS `onclick` function which must be fully functional and the JS debugger/console should not have shown any errors. Try if necessary in different browsers. – BalusC Feb 12 '12 at 19:40
  • @BalusC May the action method returning void be the reason? Or the String parameter in the action method? The Mojarra doc says: _The expression must evaluate to a public method that takes no parameters, and returns an Object (the toString() of which is called to derive the logical outcome)_ – Matt Handy Feb 12 '12 at 19:57
  • @Matt: This is a leftover from JSF 1.0/1.1. Since JSF 1.2, any valid EL expression which can be represented by a [`MethodExpression`](http://docs.oracle.com/javaee/6/api/javax/el/MethodExpression.html) is a valid action. Even more, since EL 2.2 you can pass parameters (which will work for JSF 1.2 as well). Returning `void` is treated as returning `null`. – BalusC Feb 12 '12 at 20:11
  • @BalusC I updated the question. It seems that no request is made at all. I managed to catch the following exception in the Web Developer error console: `mojarra is not defined`. I'm using `` tags – Ionut Feb 13 '12 at 14:17
  • Please cut off all the irrelevant tags/components/attributes from the Facelets source such as all those `
    `s so that you get the **smallest possible** Facelets source which still reproduces the problem and then edit your question to show it, along with the generated HTML output which you can see by rightclick and *View Source* after opening it in webbrowser.
    – BalusC Feb 13 '12 at 14:23
  • @BalusC I updated as you requested. – Ionut Feb 13 '12 at 14:49
  • I was stuck on this after switching to google app engine and ended up switching over to myfaces. – s_t_e_v_e May 23 '12 at 02:13

2 Answers2

7

The source and generated HTML output looks fine, you have there a <h:head> in the JSF source (otherwise JSF wasn't able to auto-include any CSS/JS files), and the javax.faces:jsf.js script is present in the HTML output.

You said, you got a JS error that mojarra is not definied. That can only mean that the following auto-generated script

<script type="text/javascript" src="/maze/javax.faces.resource/jsf.js.xhtml?ln=javax.faces">
</script>

did not result in a valid response. That can in turn only mean that you've a Filter which is mapped on /* or *.xhtml which is restricting the jsf.js resource request in some way. Perhaps some homegrown authentication filter which is not doing its job entirely right. Try opening

http://localhost:8080/maze/javax.faces.resource/jsf.js.xhtml?ln=javax.faces

in your browser to see what it actually retrieved (or use the web developer tools to check the response). If it's indeed not the proper response and the problem is indeed in the Filter, then you probably need to rewrite it as such that it should continue the chain when the request URI starts with ResourceHandler.RESOURCE_IDENTIFIER.

E.g.

HttpServletRequest req = (HttpServletRequest) request;

if (req.getRequestURI().startsWith(req.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) {
    chain.doFilter(request, response); // Let it continue.
    return;
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • That's quite awesome! You are completely right. That filter was blocking the access. Thank you! – Ionut Feb 13 '12 at 16:05
1

Try to watch what happens in Firebug or something similiar, to see if there is actually a server communication. And since it is a commandLink, look if there are any javascript errors on the page.

You say, you don't get any INFO logs, so I think the request doesn't even get to your application.

(I don't see a closing html tag in your xhtml file, maybe you just didn't pasted it.)

burna
  • 2,932
  • 18
  • 27
  • 2
    what happens, if you switch to h:commandButton? looks like a client side issue to me. (And JS is on? different browsers) I did always use action methods with return values in my jsf apps, you could try to add a String return value to the action methods. – burna Feb 12 '12 at 18:15
  • I had a similar issue when using action method with void. Switching to return a String helped. – Matt Handy Feb 12 '12 at 18:36