JSF-2.3 找不到我的 @Named CDI-1.2 托管 bean [英] JSF-2.3 not finding my @Named CDI-1.2 managed bean

查看:30
本文介绍了JSF-2.3 找不到我的 @Named CDI-1.2 托管 bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近从 2.2 升级到 JSF 2.3,我注意到 @ManagedBean 已被弃用,经过一些研究发现我应该使用 CDI-1.2 托管 bean 和 @Named 注释.

但是在切换到@Named 之后,JSF 页面找不到托管 bean:

<块引用>

javax.servlet.ServletException:/index.xhtml @38,38 value="#{controller.telstraPass}": Target Unreachable, identifier 'controller' 解析为 null

我正在使用 Maven、eclipse 和 WebSphere Application Server Liberty v16.0.0.4不知道我做错了什么,以下是相关文件:

Controller.java:

package ManagedBeans;导入 java.io.PrintWriter;导入 java.io.Serializable;导入 java.io.StringWriter;导入 javax.enterprise.context.SessionScoped;导入 javax.inject.Named;导入 javax.servlet.http.Part;导入 Main.FileHandler;导入 Main.IBMEmployee;@Named("控制器")@SessionScoped公共类控制器实现可序列化{私人部分 telstraCustomersFile;私有部分终止文件;私人字符串 telstraPass;私人字符串 termineesPass;私有字符串异常;私有字符串异常跟踪;private FileHandler fileHandler = new FileHandler();私有 IBMEmployee[] 潜在匹配;公共字符串执行(){尝试 {fileHandler.process(telstraCustomersFile, terminateesFile, telstraPass, termineesPass);potentialMatches = fileHandler.potentialMatches;}捕获(异常前){StringWriter 错误 = new StringWriter();ex.printStackTrace(new PrintWriter(errors));异常 = ex.toString();exceptionTrace = errors.toString();return ("errorPage.xhtml");}return ("searchExcel.xhtml");}public void setTelstraPass(String value) { telstraPass = value;}公共字符串 getTelstraPass() { 返回 telstraPass;}public void setTermineesPass(String value) { termineesPass = value;}公共字符串 getTermineesPass() { 返回 termineesPass;}public void setTelstraCustomersFile(Part file) { telstraCustomersFile = file;}公共部分 getTelstraCustomersFile() { return telstraCustomersFile;}public void setTerminateesFile(Part file) { terminateesFile = file;}公共部分 getTerminateesFile() { return terminateesFile;}public String getException() { 返回异常;}公共字符串 getExceptionTrace() { 返回异常跟踪;}公共 IBMEmployee[] getExactMatches() { return fileHandler.exactMatches;}公共 IBMEmployee[] getPotentialMatches() { return potentialMatches;}}

index.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"xmlns:h="http://java.sun.com/jsf/html"xmlns:f="http://java.sun.com/jsf/core"><h:头><title>终止清单</title><h:outputScript name="js/scripts.js"/><h:outputStylesheet name="css/PageFormat.css"/>由 Jerry Boyaji 创建<h:graphicImage id="IBMLogo" name="images/IBM-logo.jpg" width="101" height="48"/></h:head><h:body onload="enableDisableSubmitBtn()"><div id="ContentFrame"><h1>公司账户终止申请 (CATA)</h1><h3>Excel 搜索:<br/>选择要上传的电子表格文件:</h3><br/><br/><h:form id="excelInputForm" enctype="multipart/form-data"名称=上传表格"方法=发布">Telstra电子表格文件:<br/><h:输入文件id="telstraCustomers"名称=telstra 文件"大小=40"value="#{controller.telstraCustomersFile}"必需=真"onchange="enableDisableSubmitBtn()"/>密码(如果适用):<h:inputSecretid="telstraSpreadsheetPassword"value="#{controller.telstraPass}"label="密码(如果适用)"/><br/><br/>终止电子表格文件:<br/><h:输入文件id="终止"名称=终止文件"大小=40"value="#{controller.terminateesFile}"必需=真"onchange="enableDisableSubmitBtn()"/>密码(如果适用):<h:inputSecretid="termineesSpreadsheetPassword"value="#{controller.termineesPass}"label="密码(如果适用)"/><br/><br/><h:命令按钮id="提交"值="上传并继续"类型=提交"action="#{controller.perform}"/></h:form>

</h:body>

web.xml

<display-name>MainApplication</display-name><欢迎文件列表><welcome-file>index.xhtml</welcome-file></welcome-file-list><上下文参数><说明>状态保存方法:客户端"或服务器"(=默认).参见 JSF 规范 2.5.2<param-name>javax.faces.STATE_SAVING_METHOD</param-name><param-value>客户端</param-value></context-param><上下文参数><param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name><param-value>resources.application</param-value></context-param><小服务程序><servlet-name>Faces Servlet</servlet-name><servlet-class>javax.faces.webapp.FacesServlet</servlet-class><启动时加载>1</启动时加载></servlet><servlet-mapping><servlet-name>Faces Servlet</servlet-name><url-pattern>*.xhtml</url-pattern></servlet-mapping><安全约束><display-name>HTTPS 重定向安全约束</display-name><web-resource-collection><web-resource-name>MainApplication</web-resource-name><url-pattern>/*</url-pattern></web-resource-collection><用户数据约束><运输保证>机密</运输保证></user-data-constraint></安全约束></web-app>

Server.xml:

<!-- 启用功能--><功能管理器><feature>localConnector-1.0</feature><feature>websocket-1.1</feature><feature>appSecurity-2.0</feature><特征>cdi-1.2<特征><功能>jsp-2.3</功能></featureManager><basicRegistry id="基本"><用户名=管理员"密码=****"/></basicRegistry><管理员角色><user>admin</user></管理员角色><远程文件访问><writeDir>${server.config.dir}</writeDir></remoteFileAccess><!-- 要从远程客户端访问此服务器,请向以下元素添加主机属性,例如主机="*" --><httpEndpoint host="localhost" httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint"/><!-- 自动展开WAR文件和EAR文件--><applicationManager autoExpand="true"/><applicationMonitor updateTrigger="mbean"/><keyStore id="defaultKeyStore" 密码="****"/><webApplication id="MainApplication" location="MainApplication.war" name="MainApplication"/></服务器>

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>Termination-Checklist-Maven</groupId><artifactId>MainApplication</artifactId><version>0.0.1-SNAPSHOT</version><包装>战争</包装><name>MainApplication</name><url>http://maven.apache.org</url><属性><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></属性><pluginRepositories><!-- 配置 WASdev 存储库 --><插件库><id>WASdev</id><name>WASdev 存储库</name><url>http://public.dhe.ibm.com/ibmdl/export/pub/software/websphere/wasdev/maven/repository/</url><layout>默认</layout><快照><启用>假</启用></快照><发布><启用>真</启用></发布></pluginRepository></pluginRepositories><构建><插件><插件><artifactId>maven-war-plugin</artifactId><version>3.0.0</version><配置><webXml>WebContent/WEB-INF/web.xml</webXml></配置></插件><插件><groupId>com.ibm.websphere.wlp.maven.plugins</groupId><artifactId>liberty-maven-plugin</artifactId><version>1.0</version><配置><serverHome>/Applications/WebProfile</serverHome></配置></插件></插件></build><存储库><!--其他存储库(如果有)--><存储库><id>project.local</id><名称>项目</名称><url>file:${project.basedir}/repo</url></repository></repositories><依赖项><依赖><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><范围>测试</范围></依赖><依赖><groupId>org.glassfish</groupId><artifactId>javax.faces</artifactId><version>2.3.0</version></依赖><依赖><groupId>javax.enterprise</groupId><artifactId>cdi-api</artifactId><version>1.2</version><范围>提供<范围></依赖><依赖><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.16-beta2</version></依赖><依赖><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.16-beta2</version></依赖><依赖><groupId>org.glassfish</groupId><artifactId>javax.json</artifactId><version>1.0.4</version></依赖><依赖><groupId>javax.json</groupId><artifactId>javax.json-api</artifactId><version>1.0</version></依赖><依赖><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>3.16-beta2</version></依赖><依赖><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version></依赖></依赖项></项目>

faces-config.xml:

</faces-config>

我的 WEB-INF 文件夹中还有一个空的 beans.xml 文件,其中包含我的 web.xml 和 faces-config.xml.

据我所知,我所做的一切都是正确的,我没有 JSF 或 CDI 的竞争实现,也没有使用 JSF 包中的任何范围.我完全不知道为什么这不起作用......

解决方案

正如 Gas 所指出的,问题是 WAS 中的内置 CDI 特性不能与外部 JSF 库一起使用.

参见 我在 IBM 支持页面上的帖子 证实了这一点.

使用 JSF-2.2 仍然使用 Web 套接字的一种方法是使用 @Gas 指出的 Javax.websocket 包.然而,这个 Web 套接字实现的局限性在于,将包含在 SessionScoped ManagedBean 中的数据发送到该会话的客户端远没有像我希望的那样容易使用 JSF-2.3 中的 f:websocket做.

Having recently upgraded to JSF 2.3 from 2.2, I noticed that @ManagedBean was deprecated, and after some research found that I should be using CDI-1.2 managed beans and the @Named annotation.

However after changing over to @Named, the JSF pages can't find the managed bean:

javax.servlet.ServletException: /index.xhtml @38,38 value="#{controller.telstraPass}": Target Unreachable, identifier 'controller' resolved to null

I am using Maven, eclipse and WebSphere Application Server liberty v16.0.0.4 No Idea what I am doing wrong, here are the relevant files:

Controller.java:

package ManagedBeans;

import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import javax.servlet.http.Part;

import Main.FileHandler;
import Main.IBMEmployee;

@Named("controller")
@SessionScoped
public class Controller implements Serializable {

    private Part telstraCustomersFile;
    private Part terminateesFile;
    private String telstraPass;
    private String termineesPass;
    private String exception;
    private String exceptionTrace;
    private FileHandler fileHandler = new FileHandler();
    private IBMEmployee[] potentialMatches;

    public String perform()
    {
        try {
            fileHandler.process(telstraCustomersFile, terminateesFile, telstraPass, termineesPass);
            potentialMatches = fileHandler.potentialMatches;
        }
        catch (Exception ex) {
            StringWriter errors = new StringWriter();
            ex.printStackTrace(new PrintWriter(errors));
            exception = ex.toString();
            exceptionTrace = errors.toString();

            return ("errorPage.xhtml");
        }

        return ("searchExcel.xhtml");
    }

    public void setTelstraPass(String value) { telstraPass = value; }
    public String getTelstraPass() { return telstraPass; }
    public void setTermineesPass(String value) { termineesPass = value; }
    public String getTermineesPass() { return termineesPass; }
    public void setTelstraCustomersFile(Part file) { telstraCustomersFile = file; }
    public Part getTelstraCustomersFile() { return telstraCustomersFile; }
    public void setTerminateesFile(Part file) { terminateesFile = file; }
    public Part getTerminateesFile() { return terminateesFile; }
    public String getException() { return exception; }
    public String getExceptionTrace() { return exceptionTrace; }
    public IBMEmployee[] getExactMatches() { return fileHandler.exactMatches; }
    public IBMEmployee[] getPotentialMatches() { return potentialMatches; }
}

index.xhtml:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">   
      <h:head>
        <title>Termination Checklist</title>
        <h:outputScript name="js/scripts.js"/>      
        <h:outputStylesheet name="css/PageFormat.css"/>

        Created By Jerry Boyaji
        <h:graphicImage id="IBMLogo" name="images/IBM-logo.jpg"  width="101" height="48"/>
    </h:head>
    <h:body onload="enableDisableSubmitBtn()">
        <div id="ContentFrame">
            <h1>Corporate Account Termination Application (CATA)</h1>
            <h3>Excel Search: <br/> Select your spreadsheet files to upload:</h3>
            <br/>
            <br/>
            <h:form id="excelInputForm" enctype="multipart/form-data" 
                name="UploadForm" 
                method="Post">

                Telstra Spreadsheet File: 

                <br/>
                <h:inputFile
                    id="telstraCustomers" 
                    name="telstra file" 
                    size="40" 
                    value="#{controller.telstraCustomersFile}"
                    required="True"
                    onchange="enableDisableSubmitBtn()"/>

                Password if Applicable: 
                <h:inputSecret
                    id="telstraSpreadsheetPassword"
                    value="#{controller.telstraPass}"
                    label="Password if Applicable"/>

                <br/>
                <br/>

                Termination Spreadsheet File: 
                <br/>           
                <h:inputFile
                    id="terminatees" 
                    name="termination file" 
                    size="40"
                    value="#{controller.terminateesFile}"
                    required="True"
                    onchange="enableDisableSubmitBtn()" />

                Password if Applicable: 
                <h:inputSecret
                    id="termineesSpreadsheetPassword"
                    value="#{controller.termineesPass}"
                    label="Password if Applicable"/>    

                <br/>
                <br/>

                <h:commandButton 
                    id="submit"
                    value="Upload and Continue" 
                    type = "submit"
                    action="#{controller.perform}"/>

            </h:form>
        </div>
    </h:body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
    <display-name>MainApplication</display-name>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
    <context-param>
        <description>
        State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
    </context-param>
    <context-param>
        <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
        <param-value>resources.application</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>

    <security-constraint>
        <display-name>HTTPS Redirect Security Constraint</display-name>
        <web-resource-collection>
            <web-resource-name>MainApplication</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
</web-app>

Server.xml:

<server description="jerry's local server">

    <!-- Enable features -->
    <featureManager>
        <feature>localConnector-1.0</feature>
        <feature>websocket-1.1</feature>
        <feature>appSecurity-2.0</feature>
        <feature>cdi-1.2<feature>
        <feature>jsp-2.3</feature>
    </featureManager>

    <basicRegistry id="basic">
      <user name="admin" password="****"/>
   </basicRegistry>

   <administrator-role>
      <user>admin</user>
   </administrator-role>

    <remoteFileAccess>
        <writeDir>${server.config.dir}</writeDir>
    </remoteFileAccess>

    <!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" -->
    <httpEndpoint host="localhost" httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint"/>

    <!-- Automatically expand WAR files and EAR files -->
    <applicationManager autoExpand="true"/>

    <applicationMonitor updateTrigger="mbean"/>
    <keyStore id="defaultKeyStore" password="****"/>

    <webApplication id="MainApplication" location="MainApplication.war" name="MainApplication"/>
</server>

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>Termination-Checklist-Maven</groupId>
  <artifactId>MainApplication</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>MainApplication</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <pluginRepositories>
        <!-- Configure WASdev repository -->
        <pluginRepository>
            <id>WASdev</id>
            <name>WASdev Repository</name>
            <url>http://public.dhe.ibm.com/ibmdl/export/pub/software/websphere/wasdev/maven/repository/</url>
            <layout>default</layout>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <webXml>WebContent/WEB-INF/web.xml</webXml>
        </configuration>
      </plugin>
      <plugin>
            <groupId>com.ibm.websphere.wlp.maven.plugins</groupId>
            <artifactId>liberty-maven-plugin</artifactId> 
            <version>1.0</version>
            <configuration>
                <serverHome>/Applications/WebProfile</serverHome>
            </configuration>
      </plugin>
    </plugins>
  </build>

  <repositories>
    <!--other repositories if any-->
    <repository>
        <id>project.local</id>
        <name>project</name>
        <url>file:${project.basedir}/repo</url>
    </repository>
  </repositories>


  <dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.faces</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <version>1.2</version>
        <scope>provided<scope>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.16-beta2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.16-beta2</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.json</artifactId>
        <version>1.0.4</version>
    </dependency>
    <dependency>
        <groupId>javax.json</groupId>
        <artifactId>javax.json-api</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.16-beta2</version>
    </dependency>
        <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
  </dependencies>
</project>

faces-config.xml:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.3"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_3.xsd"> 
</faces-config>

I also have an empty beans.xml file in my WEB-INF folder with my web.xml and faces-config.xml.

As far as I can see I have done everything right, I have no competing implementations of JSF or CDI, and am not using any of the scopes from the JSF packages. I have absolutely no idea why this is not working...

解决方案

As Gas pointed out, the issue was that the inbuilt CDI feature in WAS does not work with an external JSF library.

See my post on IBM support page which confirms this.

A way to still make use of web sockets using JSF-2.2 is to use the Javax.websocket package as @Gas pointed out. However the limitation of this web socket implementation is that it is nowhere near as easy to send data contained in a SessionScoped ManagedBean to the client of that session only as it would have been using f:websocket from JSF-2.3 as I would have liked to do.

这篇关于JSF-2.3 找不到我的 @Named CDI-1.2 托管 bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆