在 JDevelper 和 Weblogic 中使用 @Named 注解

Using @Named annotation in JDevelper and Weblogic

我认为 与我的有关,但我不想向该项目添加另一个 jar。

我正在将 java hello1 example (JEE7)(您将在下面看到一些修改)从 netbeans 和 Glassfish 迁移到 JDeveloper 12.1.3 和 Weblogic Server 12.1.3.0.0。我这样做是因为我需要用 jsf 开始一个项目,我需要这些 IDE 和 Server。问题是我无法在支持 bean 中使用带有 @Named 注释的 CDI。

添加到我的项目中的库是 "JSP Runtime"、JSF 2.1 和 JSTL 1.2

JDeveloper 建议我 "Configure Project for CDI" 所以它在 WEB-INF 目录下添加了一个新库 "Contexts and Dependendcy Injection" 和 beans.xml 文件。

beans.xml

<?xml version = '1.0' encoding = 'windows-1252'?>
<beans 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/beans_1_0.xsd"
       xmlns="http://java.sun.com/xml/ns/javaee"></beans>

当我 运行 项目时,响应为空 "Hello" 并且未显示名称 属性,但没有错误。

如果我删除 "Contexts and Dependendcy Injection" 库和 beans.xml 文件,则会显示以下错误:

Error 500--Internal Server Error

javax.el.PropertyNotFoundException: //C:/Users/edbuitrago/.jdeveloper/system12.1.3.0.41.140521.1008/o.j2ee/drs/Batch/ViewControllerWebApp.war/index.xhtml @8,49 value="#{batch.name}": Target Unreachable, identifier 'batch' resolved to null
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
    at javax.faces.component.UIInput.validate(UIInput.java:960)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
    at javax.faces.component.UIInput.processValidators(UIInput.java:698)
    at javax.faces.component.UIForm.processValidators(UIForm.java:253)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1220)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1220)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1164)
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:346)
    at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
    at oracle.security.jps.ee.http.JpsAbsFilter.run(JpsAbsFilter.java:137)
    at java.security.AccessController.doPrivileged(Native Method)
    at oracle.security.jps.util.JpsSubject.doAsPrivileged(JpsSubject.java:315)
    at oracle.security.jps.ee.util.JpsPlatformUtil.runJaasMode(JpsPlatformUtil.java:460)
    at oracle.security.jps.ee.http.JpsAbsFilter.runJaasMode(JpsAbsFilter.java:120)
    at oracle.security.jps.ee.http.JpsAbsFilter.doFilter(JpsAbsFilter.java:217)
    at oracle.security.jps.ee.http.JpsFilter.doFilter(JpsFilter.java:81)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
    at oracle.dms.servlet.DMSServletFilter.doFilter(DMSServletFilter.java:220)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
    at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3436)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3402)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
    at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
    at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2285)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2201)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2179)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1572)
    at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:255)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:311)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:263)

如果我使用@ManagedBean 注释而不是@Named 注释,一切正常。任何建议将不胜感激,提前致谢。

这是我的代码

index.xhtml

<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html">
    <h:head></h:head>
    <h:body>
        <h:form>
            <h2>Hello, my name is Duke. What's yours?</h2>
            <h:inputText value="#{batch.name}"/>
            <h:commandButton value="Send" action="response"/>
        </h:form>
    </h:body>
</html>

response.xhtml

<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html">
    <h:head></h:head>
    <h:body>
        <h2>Hello #{batch.name}</h2>
    </h:body>
</html>

Batch.java

package view.backing;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import javax.inject.Named;

@Named
@RequestScoped
public class Batch {
    private String name;

    public Batch(){

        }
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

}

web.xml

<?xml version = '1.0' encoding = 'windows-1252'?>
<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">
  <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>/*</url-pattern>
  </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

项目结构

ViewController
    |-src
    |   |-view
    |        |-backing
    |                |-Batch.java
    |-public_html
    |          |-index.xhtml
    |          |-response.xhtml
    |          |-WEB-INF
    |                  |-web.xml

I was using the wrong import. In the java ee 7 tutorial you can see an example (example)