未捕获的引用错误:当 javax.faces.PROJECT_STAGE 为 Production 时,未定义 mojarra

问题描述 投票:0回答:2

免责声明

虽然这个问题被问了好几次,但他们都无法解决我的问题。


以下是示例代码:

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>

登录.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

环境:

  • JSF 莫哈拉 - 2.2.17
  • Java - 1.8.0_181
  • TomEE - 7.0.4
jsf jsf-2.2 mojarra
2个回答
1
投票

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

  1. TomEE 附带 MyFaces
  2. 您发布的第一个屏幕截图(jsf.js)充满了 MyFaces 引用

所以我的赌注是与你的(Maven 定义的?)Mojarra(如果你的类路径中的任何地方都有它)和你的应用程序服务器附带的 Myfaces 发生冲突。 您可以强制使用 Mojarra 版本的 jsf.js

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


0
投票

只需在您的 xhtml 代码中添加 即可! Head 标签将 Faces javascript 添加到您的代码中!

© www.soinside.com 2019 - 2024. All rights reserved.