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

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

免责声明

虽然这个问题被问了好几次,但其中 none 能够为我的问题提供解决方案。


示例代码如下:

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>

身份验证控制器

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;
    }
}

登录查看

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;
    }
}

我点击登录 h:commandLink 时遇到的错误是:

jsf.js 在 HTML 中呈现 URL:http://localhost:6180/myapp/javax.faces.resource/jsf.js.xhtml?ln=javax.faces 可访问:

当我将 context-param javax.faces.PROJECT_STAGE 的值更改为 Development 时,错误消失了。在这种情况下,jsf.js 的 URL 呈现为:http://localhost:6180/myapp/javax.faces.resource/jsf.js.xhtml?ln=javax.faces&stage=Development

环境:

我认为您实际上并没有使用 Mojarra:

  1. TomEE ships with MyFaces
  2. 您发布的第一个屏幕截图(jsf.js)中充斥着 MyFaces 引用

所以我敢打赌与您的(Maven 定义的?)Mojarra(如果您的类路径中的任何地方都有它)和您的应用程序服务器附带的 Myfaces 有冲突。 You can force Mojarra's version of jsf.js

或者,您可以通过将 lib 文件夹中的必要 jar 替换为 Mojarra 提供的内容来强制在 TomEE 上使用 Mojarra