jsfjsf-2.2mojarra

Uncaught ReferenceError: mojarra is not defined when javax.faces.PROJECT_STAGE is Production


Disclaimer

Although this question was asked several times, none of them was able to provide the solution to my problem.


Followings are the sample code:

web.xml

<?xml version='1.0' encoding='UTF-8'?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    <display-name>MyApp</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Production</param-value>
    </context-param>
    
    <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>
</web-app>

template.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <f:view locale="en_US">
        <ui:insert name="fmetadata" />
        <h:head>
            <ui:include src="/includes/head.xhtml" />
            <title>
                <ui:insert name="title" />
            </title>            
        </h:head>
        <h:body>
            <ui:insert name="content" />
        </h:body>
    </f:view>
</html>

includes/head.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
        
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="Pragma" content="no-cache"/>
    
    <h:outputStylesheet name="style/style.css" />
</ui:composition>

login.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    template="/templates/template.xhtml">
    <ui:define name="fmetadata" />

    <ui:define name="title">
        <h:outputText value="Login" />      
    </ui:define>

    <ui:define name="content">
        <div id="login">
            <h2>
                <h:outputText value="Login" escape="false" />
            </h2>
            <h:messages styleClass="errorMessage" />
            <h:form>
                <label>
                    <h:outputText value="Login"
                        escape="false" />
                </label>
                <br />
                <h:inputText value="#{authenticationController.view.login}"
                    required="true"
                    requiredMessage="Please enter a User Name"
                    styleClass="inputText" />
                <br />
                <label>
                    <h:outputText value="Password"
                        escape="false" />
                </label>
                <br />
                <h:inputSecret value="#{authenticationController.view.password}"
                    required="true"
                    requiredMessage="Please enter a password"
                    styleClass="inputText" />
                <div class="inputButton">
                    <h:commandLink value="Login"
                        action="#{authenticationController.authenticate}" />
                </div>
            </h:form>
        </div>
    </ui:define>
</ui:composition>

AuthenticationController

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;

@SessionScoped
@ManagedBean(name = "authenticationController")
public class AuthenticationController implements Serializable {

    private static final long serialVersionUID = -6685652208738725676L;

    @EJB
    private UserServiceRemote userService;
    
    private LoginView view;

    public AuthenticationController() {
        
    }
    
    @PostConstruct
    public void init() {
        view = new LoginView();     
    }
        
    public String authenticate() {
        String login = view.getLogin();
        String password = view.getPassword();
            
        boolean isAuthenticated = userService.authenticate(login,password); 

        if(isAuthenticated) {
            return "home.xhtml?faces-redirect=true";
        } else {
            FacesContext.getCurrentInstance().addMessage(null,new FacesMessage("Invalid User Name / Password"));
            return "login.xhtml?faces-redirect=true";
        }           
    }   
        
    public LoginView getView() {
        return view;
    }

    public void setView(LoginView view) {
        this.view = view;
    }
}

LoginView

public class LoginView implements Serializable {

    private static final long serialVersionUID = -9139791962440768607L;

    private String login;
    private String password;
    
    public LoginView() {
        
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

The error that I'm getting when I click on the Login h:commandLink is:

error

The jsf.js is rendered in the HTML also the URL: http://localhost:6180/myapp/javax.faces.resource/jsf.js.xhtml?ln=javax.faces is accessible:

jsf.js-Production

The error disappears when I change the value of the context-param javax.faces.PROJECT_STAGE to Development. In this case, the URL of jsf.js is rendered as: http://localhost:6180/myapp/javax.faces.resource/jsf.js.xhtml?ln=javax.faces&stage=Development: jsf.js-Development

Environment:


Solution

  • I don't think you're actually using Mojarra:

    1. TomEE ships with MyFaces
    2. The first screenshot you posted (of jsf.js) is littered with MyFaces references

    So my bet is on a conflict with your (Maven-defined?) Mojarra (if you have it anywhere in your classpath) and the Myfaces that ships with your app server. You can force Mojarra's version of jsf.js

    Alternatively, you can force the use of Mojarra on TomEE by replacing the necessary jars in the lib folder with what's supplied with Mojarra