【问题标题】:Uncaught ReferenceError: mojarra is not defined when javax.faces.PROJECT_STAGE is Production未捕获的 ReferenceError:当 javax.faces.PROJECT_STAGE 为 Production 时未定义 mojarra
【发布时间】:2018-12-29 23:12:57
【问题描述】:

免责声明

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


以下是示例代码:

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>

模板.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>

包括/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&amp;stage=Development

环境:

  • JSF 莫哈拉 - 2.2.17
  • Java - 1.8.0_181
  • TomEE - 7.0.4

【问题讨论】:

  • 不知道为什么会这样,因为一切看起来都很好。您可以尝试将private LoginView view; 重命名为private LoginView loginView;,以防它是保留名称。您也可以将&lt;h:messages styleClass="errorMessage" /&gt; 放在h:form 中。你清理过你的 TomEE 安装和工作目录吗?
  • @MitchBroadhead,更改 LoginViewh:messages 并没有解决问题。每次运行时我都会清理 TomEE 安装和工作目录。
  • 我将 TomEE 7.0.3 plus 与 MyFaces 一起使用,所以我对 Mojarra 无能为力。你试过 users@tomee.apache.org 邮件列表吗?他们通常反应很快

标签: jsf jsf-2.2 mojarra


【解决方案1】:

我认为您实际上并没有使用 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

【讨论】:

    猜你喜欢
    • 2013-06-06
    • 2023-01-23
    • 2016-11-03
    • 2011-01-05
    • 2016-01-02
    • 2013-10-06
    • 2016-12-17
    相关资源
    最近更新 更多